QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#130036 | #4239. MST | SolitaryDream# | WA | 49ms | 5744kb | C++17 | 1.3kb | 2023-07-23 14:35:31 | 2023-07-23 14:35:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+1e3+7;
using pii=pair<int,int>;
int T,n,x[N];
int fa[N];
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&x[i]),fa[i]=i;
long long ans=0;
x[0]=1e9;
while(1)
{
bool ok=1;
for(int i=1;i<=n;i++)
if(find(i)!=find(1))
ok=0;
if(ok)
break;
vector<pii>e;
int mn=0,smn=0;
for(int i=n;i>=1;i--)
{
int f=find(i);
if(mn!=0)
{
if(find(mn)!=f)
e.push_back({i,mn});
else if(smn!=0)
e.push_back({i,smn});
}
int c=i;
if(x[c]<x[mn])
swap(mn,c);
if(x[c]<x[smn])
swap(smn,c);
if(find(smn)==find(mn))
swap(c,smn);
}
vector<int>me(n+1,1e9);
vector<pii>cho(n+1);
for(auto [a,b]:e)
{
int fx=find(a),fy=find(b);
if(me[fx]>x[b]-x[a])
me[fx]=x[b]-x[a],cho[fx]={a,b};
if(me[fy]>x[b]-x[a])
me[fy]=x[b]-x[a],cho[fy]={a,b};
}
for(int i=1;i<=n;i++)
{
if(me[i]>1e8)
continue;
auto [a,b]=cho[i];
int fx=find(a),fy=find(b);
if(fx==fy)
continue;
fa[fx]=fy;
ans+=x[b]-x[a];
}
}
printf("%lld\n",ans);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5744kb
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: 49ms
memory: 3772kb
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: 45ms
memory: 3704kb
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: -100
Wrong Answer
time: 39ms
memory: 3700kb
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:
-31537 314713 -9970 64002 398840 -94705 -200264 9433 -14069 172651 -286147 276565 -538432 76522 348200 -788400 -139085 -668356 -30025 -892127 -174159 132420 217221 -78679 -306280 -837781 -119004 -124846 36563 321809 -11225 -436944 -309052 -538331 -279352 -595584 -360199 -390572 84683 -125272 -200262...
result:
wrong answer 1st numbers differ - expected: '-361162', found: '-31537'