結果

問題 No.2114 01 Matching
ユーザー 👑 kmjpkmjp
提出日時 2022-10-31 23:19:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,923 bytes
コンパイル時間 2,784 ms
コンパイル使用メモリ 229,288 KB
実行使用メモリ 81,360 KB
最終ジャッジ日時 2023-09-22 20:15:29
合計ジャッジ時間 17,753 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
35,688 KB
testcase_01 AC 13 ms
35,720 KB
testcase_02 AC 268 ms
41,800 KB
testcase_03 AC 51 ms
44,324 KB
testcase_04 AC 86 ms
49,368 KB
testcase_05 WA -
testcase_06 AC 214 ms
41,692 KB
testcase_07 AC 101 ms
39,276 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 270 ms
41,332 KB
testcase_11 AC 235 ms
81,360 KB
testcase_12 AC 230 ms
80,976 KB
testcase_13 WA -
testcase_14 AC 421 ms
44,660 KB
testcase_15 AC 419 ms
44,812 KB
testcase_16 AC 445 ms
44,728 KB
testcase_17 AC 216 ms
41,544 KB
testcase_18 AC 227 ms
40,340 KB
testcase_19 WA -
testcase_20 AC 233 ms
81,044 KB
testcase_21 AC 356 ms
43,944 KB
testcase_22 WA -
testcase_23 AC 287 ms
40,852 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 230 ms
80,980 KB
testcase_27 AC 231 ms
80,980 KB
testcase_28 AC 132 ms
59,844 KB
testcase_29 AC 229 ms
81,044 KB
testcase_30 AC 100 ms
39,980 KB
testcase_31 AC 229 ms
81,284 KB
testcase_32 WA -
testcase_33 AC 273 ms
41,916 KB
testcase_34 AC 73 ms
39,548 KB
testcase_35 AC 117 ms
54,440 KB
testcase_36 AC 236 ms
81,108 KB
testcase_37 WA -
testcase_38 AC 102 ms
39,404 KB
testcase_39 WA -
testcase_40 AC 125 ms
53,916 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 217 ms
41,872 KB
testcase_44 AC 414 ms
44,568 KB
testcase_45 AC 219 ms
80,972 KB
testcase_46 WA -
testcase_47 AC 253 ms
81,048 KB
testcase_48 AC 228 ms
81,108 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 98 ms
39,572 KB
testcase_52 AC 416 ms
44,828 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<<62;
		if(x<=l && r<=y) return ma[k];
		return val[k]+min(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}
	
	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) {
			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+N);
	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