UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214304#3852. 最小生成树drdilyor3087ms6968kbC++11915b2024-11-17 12:18:562024-11-17 13:06:08

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
void ts(){cout<<"IAKIOI\n";}
inline int read(){
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
int n;
int a[300005];
int f[300005];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
struct edg{
	int w,u,v;
}p[1000005];
bool cmp(edg a,edg b){return a.w<b.w;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)f[i]=i;
	int tot=0;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(i<j&&a[i]>a[j])p[++tot]={a[i]-a[j],i,j};
		}
	}
	sort(p+1,p+tot+1,cmp);
	int res=0;
	for(int i=1;i<=tot;i++){
		if(find(p[i].u)!=find(p[i].v)){
			res+=p[i].w,f[find(p[i].u)]=find(p[i].v);
		}
	}
	cout<<res<<"\n";
	return 0;
}
//look at my code
//my code is amazing

详细

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

Test #1:

score: 10
Accepted
time: 32ms
memory: 6840kb

input:

998
659 323 324 144 530 13 857 736 881 855 393 72 149 462 387 255 304 4 92 574 232 391 300 464 686 5...

output:

1801

result:

ok "1801"

Test #2:

score: 10
Accepted
time: 25ms
memory: 6968kb

input:

997
246 134 37 17 635 794 921 370 559 660 333 488 216 897 593 313 572 888 809 581 363 32 929 372 805...

output:

1824

result:

ok "1824"

Test #3:

score: 10
Accepted
time: 30ms
memory: 6860kb

input:

999
171 504 913 186 852 6 138 267 105 725 460 829 514 595 805 663 295 730 873 349 126 924 329 43 571...

output:

1838

result:

ok "1838"

Test #4:

score: 0
Runtime Error

input:

9996
5565 6310 3171 5034 3009 4627 1230 5305 4286 2812 4097 1669 3804 6661 9685 4031 2404 7681 4483 ...

output:


result:


Test #5:

score: 0
Runtime Error

input:

10000
2704 1827 9652 8229 8227 6104 349 3655 660 4045 9328 5512 8584 3254 9143 4556 2716 3574 5858 2...

output:


result:


Test #6:

score: 0
Runtime Error

input:

10000
23950783 781965025 16565420 291572110 128841529 363438218 359663422 727333852 367524122 226010...

output:


result:


Test #7:

score: 0
Runtime Error

input:

9999
452311202 701672009 791523862 178026433 144716116 184608716 59592754 30267781 818635776 8222542...

output:


result:


Test #8:

score: 0
Runtime Error

input:

300000
270756303 34017269 277560979 609142340 79149252 153098611 740386564 339460652 18451431 900438...

output:


result:


Test #9:

score: 0
Runtime Error

input:

299997
455780537 859019556 200709560 794471117 332485096 205293298 187001196 716562830 121596348 390...

output:


result:


Test #10:

score: 0
Runtime Error

input:

299996
51219018 155510262 688466426 766639015 395742109 862028671 109658585 526973 462033724 7922265...

output:


result: