UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#203178#3545. gamesnow_trace1001768ms32516kbC++111.5kb2024-02-20 09:41:252024-02-20 13:43:12

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 10000000000000000;
int n,m,k;int a[1000005],b[1000005],c[1000005],v1[500005],v2[500005];
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m >> k;
	for(int i =1;i<=n;i++)cin >> a[i];
	for(int i = 1;i<=m;i++)cin >> b[i];
	sort(a+1,a+1+n),sort(b+1,b+1+m);
	int need1 = 0,need2 = 0;
	for(int i = 1;i<=n;i++)need1+=(a[i]-a[1])/k;
	for(int i = 1;i<=m;i++)need2+=(b[i]-b[1])/k;
	for(int i = 1;i<=n;i++)a[i]-=(a[i]-a[1])/k*k;
	for(int i = 1;i<=m;i++)b[i]-=(b[i]-b[1])/k*k;
	sort(a+1,a+1+n),sort(b+1,b+1+m);
	if(need1>need2){
	//	cout << 111 << endl;
		swap(need1,need2),swap(a,b);swap(n,m);
	}
	need2-=need1;need2%=n;memcpy(c,a,sizeof(c));
	for(int i= 1;i<=need2;i++)a[i] = c[n-need2+i];
	for(int i =need2+1;i<=n;i++)a[i] = c[i-need2]+k;
	for(int i = 1;i<=n;i++)a[i+n] = a[i]+k;for(int i = 1;i<=m;i++)b[i+m] = b[i]+k;
	for(int i = 1;i<=n;i++)v1[i] = a[i+n-1]-a[i];
	for(int i = 1;i<=m;i++)v2[i] = b[i+m-1]-b[i];
//	for(int i= 1;i<=n;i++)cout << a[i] << " ";cout << endl;
//	for(int i = 1;i<=m;i++)cout << b[i] << " ";cout << endl;
	int g = __gcd(n,m);int ans = inf;
	for(int i = 1;i<=g;i++){
		int mn1 = inf,mn2 = inf;
		for(int j = i;j<=n;j+=g)mn1 = min(mn1,v1[j]);
		for(int j = i;j<=m;j+=g)mn2 = min(mn2,v2[j]);
		ans = min(ans,mn1+mn2);
	}cout << ans << endl;
	return 0;
}
/*
这我哪会啊?
显然两边极差都可以变成<k,然后做循环移位,然后可以划分出若干等价类
感觉就这样?
不懂。
*/

Details

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

Test #1:

score: 5
Accepted
time: 4ms
memory: 24708kb

input:

2 2 1000000
948364364 463781920
331555568 90472908

output:

500216

result:

ok single line: '500216'

Test #2:

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

input:

2 2 999999
170194963 639025544
941031700 349446447

output:

754794

result:

ok single line: '754794'

Test #3:

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

input:

2 2 999999
745738604 245622179
348420894 341088700

output:

784723

result:

ok single line: '784723'

Test #4:

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

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: 4ms
memory: 9080kb

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: 4ms
memory: 9088kb

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: 5ms
memory: 9140kb

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: 24720kb

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: 24708kb

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: 96ms
memory: 20804kb

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: 113ms
memory: 20804kb

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: 127ms
memory: 20800kb

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: 175ms
memory: 32516kb

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: 196ms
memory: 32516kb

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: 180ms
memory: 32512kb

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: 193ms
memory: 32516kb

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: 175ms
memory: 32516kb

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: 189ms
memory: 32512kb

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: 186ms
memory: 32512kb

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: 121ms
memory: 23148kb

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