UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#203176#3545. gameshr1001775ms16636kbC++11913b2024-02-20 09:13:352024-02-20 13:43:01

answer

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
	cin.tie(0)->sync_with_stdio(0);
	int n,m;
	ll k;
	cin>>n>>m>>k;
	vector<ll> a(n),b(m);
	for(ll&x:a) cin>>x;
	for(ll&x:b) cin>>x;
	sort(begin(a),end(a));
	sort(begin(b),end(b));
	ll sa=0,sb=0;
	for(int i=1;i<n;i++){
		ll t=(a[i]-a[0])/k;
		a[i]-=t*k;sa+=t;
	}
	for(int i=1;i<m;i++){
		ll t=(b[i]-b[0])/k;
		b[i]-=t*k;sb+=t;
	}
	sort(begin(a),end(a));
	sort(begin(b),end(b));
	if(sa<sb) swap(n,m),swap(a,b),swap(sa,sb);
	ll p=sa-sb;
	for(int i=0;i<m;i++){
		ll t=p/m+(i<p%m);
		b[m-i-1]-=t*k;
	}
	sort(begin(b),end(b));
	int g=__gcd(n,m);
	vector<ll> ma(g,k),mb(g,k);
	for(int i=0;i<n;i++) ma[i%g]=min(ma[i%g],a[n-i-1]-a[(n-i)%n]+(i>0)*k);
	for(int i=0;i<m;i++) mb[i%g]=min(mb[i%g],b[m-i-1]-b[(m-i)%m]+(i>0)*k);
	ll ans=k*2;
	for(int i=0;i<g;i++) ans=min(ans,ma[i]+mb[i]);
	cout<<ans;
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 1272kb

input:

2 2 1000000
948364364 463781920
331555568 90472908

output:

500216

result:

ok single line: '500216'

Test #2:

score: 5
Accepted
time: 0ms
memory: 1272kb

input:

2 2 999999
170194963 639025544
941031700 349446447

output:

754794

result:

ok single line: '754794'

Test #3:

score: 5
Accepted
time: 0ms
memory: 1268kb

input:

2 2 999999
745738604 245622179
348420894 341088700

output:

784723

result:

ok single line: '784723'

Test #4:

score: 5
Accepted
time: 0ms
memory: 1268kb

input:

15 5 17
12 10 13 1 3 1 9 2 11 6 13 7 14 1 19
8 1 18 4 17

output:

24

result:

ok single line: '24'

Test #5:

score: 5
Accepted
time: 0ms
memory: 1260kb

input:

16 8 17
4 16 9 12 16 5 8 16 14 7 12 7 14 5 19 9
8 15 10 8 14 18 12 20

output:

26

result:

ok single line: '26'

Test #6:

score: 5
Accepted
time: 0ms
memory: 1268kb

input:

19 20 500
5 17 1 3 16 17 2 4 5 5 8 17 18 4 19 16 6 3 6
18 18 7 3 3 2 11 20 2 15 18 5 1 8 3 13 14 9 1...

output:

37

result:

ok single line: '37'

Test #7:

score: 5
Accepted
time: 0ms
memory: 1288kb

input:

1000 999 888
491 229 618 639 918 607 879 627 40 450 609 102 970 122 55 717 663 195 53 340 843 110 55...

output:

1762

result:

ok single line: '1762'

Test #8:

score: 5
Accepted
time: 0ms
memory: 1280kb

input:

1000 100 888
462 65 621 497 985 149 716 505 779 724 917 685 478 828 939 297 673 878 624 800 351 108 ...

output:

1726

result:

ok single line: '1726'

Test #9:

score: 5
Accepted
time: 0ms
memory: 1276kb

input:

250 150 888
241 25 902 805 720 233 55 178 519 323 454 392 176 695 656 580 419 482 195 563 366 272 89...

output:

1730

result:

ok single line: '1730'

Test #10:

score: 5
Accepted
time: 93ms
memory: 5024kb

input:

1 500000 1000000000
747323127
963718212 806914589 212315576 835495436 183593413 939946203 376794181 ...

output:

999970751

result:

ok single line: '999970751'

Test #11:

score: 5
Accepted
time: 110ms
memory: 5020kb

input:

1 500000 100000000
4630340
368982591 319293019 73434682 151733468 679506573 811288048 303409690 8487...

output:

99997334

result:

ok single line: '99997334'

Test #12:

score: 5
Accepted
time: 119ms
memory: 5024kb

input:

1 500000 10000
468438890
347976444 571474658 289413154 883809085 190073689 924017413 702822268 63217...

output:

9999

result:

ok single line: '9999'

Test #13:

score: 5
Accepted
time: 192ms
memory: 16636kb

input:

500000 500000 71835378
7314070 4369749 848977 1317877 61094 6878144 111873 5880895 7440332 66981 488...

output:

19999744

result:

ok single line: '19999744'

Test #14:

score: 5
Accepted
time: 195ms
memory: 16636kb

input:

500000 500000 81835378
8428698 4888471 9643254 22055 505377 3347194 92679 42774 44252 6371168 12254 ...

output:

19999909

result:

ok single line: '19999909'

Test #15:

score: 5
Accepted
time: 189ms
memory: 16636kb

input:

500000 500000 91835378
7965302 66856 80768 95513 2515538 4045455 7535144 61772 30566 8063 173097 373...

output:

19999959

result:

ok single line: '19999959'

Test #16:

score: 5
Accepted
time: 197ms
memory: 8980kb

input:

500000 499999 91835378
7275977 54627 25295 8965 891981 732726 61503 33415 4674120 9806792 2644978 83...

output:

19999967

result:

ok single line: '19999967'

Test #17:

score: 5
Accepted
time: 191ms
memory: 8984kb

input:

500000 499995 91835378
79162 2846539 5451959 88097 9816274 14148 9133389 6742823 16061 9541679 61528...

output:

19999928

result:

ok single line: '19999928'

Test #18:

score: 5
Accepted
time: 186ms
memory: 8980kb

input:

500000 499990 91835378
10984 2655064 2566021 58742 74530 7720343 5793997 9200881 8488895 46364 57440...

output:

19999961

result:

ok single line: '19999961'

Test #19:

score: 5
Accepted
time: 191ms
memory: 8980kb

input:

500000 499980 91835378
6181126 4067356 47734 2325031 55266 2003629 303826 34060 17462 51832 96852 66...

output:

19999938

result:

ok single line: '19999938'

Test #20:

score: 5
Accepted
time: 112ms
memory: 7400kb

input:

500000 100000 71835378
509467 1551641 7950332 9369102 8460686 21976 8383224 4311353 6200608 9367026 ...

output:

19999758

result:

ok single line: '19999758'

Extra Test:

score: 0
Extra Test Passed