結果

問題 No.2114 01 Matching
ユーザー 👑 kmjpkmjp
提出日時 2022-10-31 23:27:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 389 ms / 5,000 ms
コード長 2,979 bytes
コンパイル時間 4,093 ms
コンパイル使用メモリ 230,736 KB
実行使用メモリ 80,984 KB
最終ジャッジ日時 2023-08-16 02:52:44
合計ジャッジ時間 16,414 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
35,804 KB
testcase_01 AC 12 ms
35,836 KB
testcase_02 AC 234 ms
41,912 KB
testcase_03 AC 52 ms
46,704 KB
testcase_04 AC 66 ms
49,316 KB
testcase_05 AC 107 ms
39,304 KB
testcase_06 AC 184 ms
41,560 KB
testcase_07 AC 92 ms
39,304 KB
testcase_08 AC 329 ms
42,624 KB
testcase_09 AC 254 ms
39,536 KB
testcase_10 AC 238 ms
41,228 KB
testcase_11 AC 211 ms
80,936 KB
testcase_12 AC 213 ms
80,888 KB
testcase_13 AC 288 ms
44,520 KB
testcase_14 AC 366 ms
46,368 KB
testcase_15 AC 364 ms
45,576 KB
testcase_16 AC 389 ms
45,480 KB
testcase_17 AC 189 ms
41,504 KB
testcase_18 AC 203 ms
40,192 KB
testcase_19 AC 272 ms
43,852 KB
testcase_20 AC 216 ms
80,944 KB
testcase_21 AC 312 ms
45,020 KB
testcase_22 AC 202 ms
38,528 KB
testcase_23 AC 257 ms
40,924 KB
testcase_24 AC 92 ms
36,980 KB
testcase_25 AC 98 ms
38,892 KB
testcase_26 AC 211 ms
80,892 KB
testcase_27 AC 214 ms
80,888 KB
testcase_28 AC 123 ms
61,504 KB
testcase_29 AC 206 ms
80,884 KB
testcase_30 AC 88 ms
39,772 KB
testcase_31 AC 215 ms
80,984 KB
testcase_32 AC 335 ms
40,508 KB
testcase_33 AC 241 ms
41,740 KB
testcase_34 AC 67 ms
39,376 KB
testcase_35 AC 91 ms
54,392 KB
testcase_36 AC 214 ms
80,952 KB
testcase_37 AC 264 ms
39,784 KB
testcase_38 AC 89 ms
39,320 KB
testcase_39 AC 165 ms
38,960 KB
testcase_40 AC 85 ms
53,700 KB
testcase_41 AC 305 ms
39,540 KB
testcase_42 AC 245 ms
39,848 KB
testcase_43 AC 188 ms
41,728 KB
testcase_44 AC 369 ms
45,592 KB
testcase_45 AC 185 ms
80,948 KB
testcase_46 AC 120 ms
38,404 KB
testcase_47 AC 222 ms
80,968 KB
testcase_48 AC 207 ms
80,892 KB
testcase_49 AC 279 ms
44,096 KB
testcase_50 AC 131 ms
38,472 KB
testcase_51 AC 87 ms
39,408 KB
testcase_52 AC 359 ms
46,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M,K;
int B[202020],R[202020];
map<int,vector<int>> X,Y;

static ll const def=0;
template<class V,int NV> class SegTree_3 {
public:
	vector<V> val, ma;
	SegTree_3(){
		int i;
		val.resize(NV*2,0); ma.resize(NV*2,0);
	};
	
	V getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l || y<=x) return 1LL<<60;
		if(x<=l && r<=y) return ma[k];
		ll a=val[k]+min(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
		if(a>1LL<<60) a=1LL<<60;
		return a;
	}
	
	void update(int x,int y, V v,int l=0,int r=NV,int k=1) {
		if(l>=r||y<=x) return;
		if(x<=l && r<=y) {
			val[k]+=v;
			ma[k]+=v;
		}
		else if(l < y && x < r) {
			update(x,y,v,l,(l+r)/2,k*2);
			update(x,y,v,(l+r)/2,r,k*2+1);
			ma[k]=val[k]+min(ma[k*2],ma[k*2+1]);
		}
	}
	void reset(int x,int y, int l=0,int r=NV,int k=1) {
		if(l>=r||y<=x) return;
		if(x<=l && r<=y) {
			val[k]=ma[k]=0;
		}
		else if(l < y && x < r) {
			val[k]=ma[k]=0;
			reset(x,y,l,(l+r)/2,k*2);
			reset(x,y,(l+r)/2,r,k*2+1);
		}
	}
};
SegTree_3<ll,1<<20> st;

ll hoge(vector<int> X,vector<int> Y) {
	vector<pair<int,int>> V;
	FORR(x,X) V.push_back({x,0});
	FORR(x,Y) V.push_back({x,1});
	int cur=X.size();
	int N=X.size()+Y.size()+1;
	//初期化
	int i;
	FOR(i,N+1) st.reset(i,i+1);
	st.update(0,N+1,1LL<<55);
	st.update(cur,cur+1,-(1LL<<55));
	
	sort(ALL(V));
	FORR2(x,c,V) {
		if(c==0) { //青頂点追加
			
			//赤が過剰な場合
			st.update(0,cur,x);
			//青が過剰な場合
			st.update(cur,N,-x);
			cur--;
		}
		else { //赤頂点追加
			//無視する場合
			ll v=st.getval(cur,cur+1);
			//赤が過剰な場合
			st.update(0,cur+1,-x);
			//青が過剰な場合
			st.update(cur+1,N+1,x);
			ll nv=st.getval(cur+1,cur+2);
			if(v<nv) st.update(cur+1,cur+2,v-nv);
			cur++;
		}
	}
	return st.getval(cur,cur+1);

}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>K;
	FOR(i,N) cin>>B[i];
	FOR(i,M) cin>>R[i];
	sort(B,B+N);
	sort(R,R+M);
	if(N>M) {
		swap(N,M);
		swap(B,R);
	}
	FOR(i,N) X[B[i]%K].push_back(B[i]/K);
	FOR(i,M) Y[R[i]%K].push_back(R[i]/K);
	
	ll ret=0;
	FORR2(a,b,X) {
		if(Y[a].size()<b.size()) {
			cout<<-1<<endl;
			return;
		}
		ret+=hoge(b,Y[a]);
	}
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0