QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#549115#4239. MSTship2077TL 19ms8052kbC++231.6kb2024-09-06 09:14:262024-09-06 09:14:27

Judging History

This is the latest submission verdict.

  • [2024-09-06 09:14:27]
  • Judged
  • Verdict: TL
  • Time: 19ms
  • Memory: 8052kb
  • [2024-09-06 09:14:26]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int M=3e5+5,inf=0x3f3f3f3f;
int n,cnt,a[M],f[M],tmp[M],pos[M];
bool vis[M];
int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
	return x*f;
}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void solve(){
	n=cnt=read();long long ans=0;
	for (int i=1;i<=n;i++) a[i]=read(),f[i]=i;
	while (cnt>1){
		for (int i=1;i<=n;i++) tmp[i]=inf,vis[i]=find(i)==i;
		int max1=a[1],max2=-inf,pos1=find(1),pos2=0;
		for (int i=2;i<=n;i++){
			const int bel=find(i);
			if (pos1==bel) { if (a[i]-max2<tmp[bel]) tmp[bel]=a[i]-max2,pos[bel]=pos2; }
			else { if (a[i]-max1<tmp[bel]) tmp[bel]=a[i]-max1,pos[bel]=pos1; }
			if (a[i]>max1){
				if (bel==pos1) max1=a[i];
				else max2=max1,pos2=pos1,max1=a[i],pos1=bel;
			}
			else if (a[i]>max2&&bel!=pos2)
				max2=a[i],pos2=bel;
		}
		int min1=a[n],min2=inf;pos1=find(n);pos2=0;
		for (int i=n-1;i;i--){
			const int bel=find(i);
			if (pos1==bel) { if (min2-a[i]<tmp[bel]) tmp[bel]=min2-a[i],pos[bel]=pos2; }
			else { if (min1-a[i]<tmp[bel]) tmp[bel]=min1-a[i],pos[bel]=pos1; }
			if (a[i]<min1){
				if (bel==pos1) min1=a[i];
				else min2=min1,pos2=pos1,min1=a[i],pos1=bel;
			}
			else if (a[i]<min2&&bel!=pos2)
				min2=a[i],pos2=bel;
		}
		for (int i=1;i<=n;i++)
			if (vis[i]){
				if (find(i)==find(pos[i])) continue;
				ans+=tmp[i];f[find(i)]=find(pos[i]);cnt--;
			}
	}
	printf("%lld\n",ans);
}
int main(){int T=read(); while (T--) solve(); return 0;}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5912kb

input:

2
5
1 2 3 4 5
3
10 45 10

output:

4
-35

result:

ok 2 number(s): "4 -35"

Test #2:

score: 0
Accepted
time: 19ms
memory: 5816kb

input:

300000
1
-167586
1
45048
1
-218274
1
-39107
1
-15880
1
33014
1
217559
1
-208936
1
-260570
1
-83353
1
-39868
1
-253159
1
-26640
1
-114610
1
-244464
1
-7217
1
-196817
1
168691
1
146930
1
284612
1
-93130
1
-186071
1
-31746
1
37800
1
-255791
1
-237603
1
81359
1
201796
1
106965
1
-8371
1
-85871
1
-270622...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 300000 numbers

Test #3:

score: 0
Accepted
time: 16ms
memory: 8052kb

input:

150000
2
-13258 -22375
2
-54993 -139278
2
-190454 190904
2
-167856 -255749
2
-4278 -217960
2
-267607 -152069
2
232717 199468
2
-154968 -171972
2
-166963 -274452
2
78459 -235523
2
-243695 163707
2
37498 -80301
2
196137 -251909
2
-77332 -82651
2
236076 250081
2
-139194 8035
2
256129 185906
2
72175 -17...

output:

-9117
-84285
381358
-87893
-213682
115538
-33249
-17004
-107489
-313982
407402
-117799
-448046
-5319
14005
147229
-70223
-242776
531933
251314
-377509
-258596
-210071
480133
-27044
110616
213370
6298
-370703
-118888
-103653
-346212
54848
-39115
-22654
-90525
-90204
345598
-96264
854
-433346
-187189
...

result:

ok 150000 numbers

Test #4:

score: 0
Accepted
time: 18ms
memory: 5912kb

input:

100000
3
99677 -229948 68140
3
-54211 -95409 260502
3
151289 -78245 141319
3
44080 -120949 108082
3
-146874 -144493 251966
3
198672 5102 103967
3
165781 237071 101294
3
-236562 72879 -77125
3
233711 286742 253192
3
15202 98416 187853
3
-12416 -267937 -283250
3
-137083 -73845 139482
3
260645 202901 -...

output:

-361162
273515
-239504
-101027
398840
-288275
-200264
9433
-14069
172651
-526355
276565
-538432
76522
348200
-788400
-362322
-1000601
-30025
-892127
-281085
-99512
-13346
-78679
-306280
-837781
-490044
-506530
-193674
321809
-11225
-436944
-718672
-538331
-279352
-595584
-709202
-390572
84683
-30428...

result:

ok 100000 numbers

Test #5:

score: -100
Time Limit Exceeded

input:

30000
10
-74325 266684 -279788 109467 225321 169570 213034 -278478 -243351 -202901
10
106422 -19496 -145263 -278344 183459 212003 264371 -267695 201093 239470
10
-216261 -203734 -129177 75781 137334 -272383 -182861 278026 64019 240839
10
281057 -213075 -7684 -280036 -209516 112973 203601 -123747 164...

output:


result: