UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196560#2476. 隙间妖怪的式神wosile100543ms29720kbC++754b2023-10-28 10:33:562023-10-28 12:05:56

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2000005],pre[2000005];
int read(){
	int x=0;
	char c=getchar();
	while(c<'0' || c>'9')c=getchar();
	while(c>='0' && c<='9'){
		x=x*10+c-48;
		c=getchar();
	}
	return x;
}
int n;
ll m;
int main(){
	n=read();m=read();
	for(int i=1;i<=n;i++)a[i]=read();
	sort(a+1,a+n+1);
	for(int i=n+1;i<=n+n;i++)a[i]=a[i-n]+m;
	for(int i=1;i<=n+n;i++)pre[i]=pre[i-1]+a[i];
	int L=1,R=n,mid=(n+1)/2;
	int lenl=mid-L+1,lenr=R-mid;
	ll ans=0x3f3f3f3f3f3f3f3f;
	while(R<=n+n){
		ans=min(ans,(a[mid]+a[mid]-lenl+1)*lenl/2-(pre[mid]-pre[L-1])+pre[R]-pre[mid]-(a[mid]+1+a[mid]+lenr)*lenr/2);
		L++;R++;mid++;
	}
	printf("%lld",ans);
	return 0;
	//quod erat demonstrandum
}

Details

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

Test #1:

score: 10
Accepted
time: 0ms
memory: 1164kb

input:

12 50
30 6 21 48 43 1 31 18 3 32 37 12

output:

104

result:

ok single line: '104'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1168kb

input:

13 50
16 6 23 27 8 20 40 47 36 13 48 15 18

output:

85

result:

ok single line: '85'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1164kb

input:

7 50
43 31 27 2 38 7 26

output:

56

result:

ok single line: '56'

Test #4:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

1352 3000
661 387 1173 1226 2825 852 1458 358 328 2620 269 2590 845 1182 1042 421 926 2163 2224 1933...

output:

545816

result:

ok single line: '545816'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

1368 3000
635 404 2417 2263 1459 1414 1821 2380 2356 1880 489 213 41 1839 1196 439 748 2548 776 2475...

output:

534736

result:

ok single line: '534736'

Test #6:

score: 10
Accepted
time: 0ms
memory: 1248kb

input:

2698 3000
1880 2445 1980 1459 2075 92 1079 1883 993 1710 1938 2015 52 255 872 1081 2920 2611 2471 25...

output:

192416

result:

ok single line: '192416'

Test #7:

score: 10
Accepted
time: 137ms
memory: 29716kb

input:

913743 1000000000
908626452 450856467 440516260 577459640 579677068 623667143 146112603 762467446 12...

output:

213770807528435

result:

ok single line: '213770807528435'

Test #8:

score: 10
Accepted
time: 141ms
memory: 29720kb

input:

913769 1000000000
505034837 316491879 396140520 883841461 41756397 635181982 188691933 12537990 6579...

output:

213721581879336

result:

ok single line: '213721581879336'

Test #9:

score: 10
Accepted
time: 142ms
memory: 29716kb

input:

913786 1000000000
118560062 37767132 39522994 404200499 108209161 642403333 752183117 632006224 4555...

output:

213880847967574

result:

ok single line: '213880847967574'

Test #10:

score: 10
Accepted
time: 123ms
memory: 29720kb

input:

913809 1000000000
436534711 114895846 866464205 806510512 845455358 652519777 252528158 313951134 38...

output:

213730703953361

result:

ok single line: '213730703953361'

Extra Test:

score: 0
Extra Test Passed