QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#556141 | #7993. 哈密顿 | wangjingheng | WA | 83ms | 26448kb | C++20 | 1.7kb | 2024-09-10 15:20:49 | 2024-09-10 15:20:50 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=200100;
pair<int,int> a[N],b[N],c[N];
set<pair<int,int> >sa,sb;
int fa[N];
int find(int x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void merge(int x,int y){
x=find(x),y=find(y);
if(x==y) return;
fa[x]=y;
}
void solve(){
int n;cin>>n;
int t=0;
for(int i=1;i<=n;i++){
fa[i]=i;
int x;cin>>x;
a[i]={x,i};
c[++t]={x,0};
cin>>x;
b[i]={x,i};
c[++t]={x,1};
}
sort(c+1,c+t+1);
int res=0;
for(int i=1;i<=t/2;i++){
res-=c[i].first;
}
int mx=1e13;
for(int i=t/2+1;i<=t;i++) res+=c[i].first;
for(int i=1;i<t;i++){
mx=min(mx,c[i+1].first-c[i].first);
}
if(mx==1e13) mx=0;
mx*=2;
// res-=mx;
int ans=0;
for(int i=1;i<=n;i++) ans+=a[i].first-b[i].first;
for(int i=1;i<=n;i++){
sa.insert(a[i]);
sb.insert(b[i]);
}
while(sa.size()&&sb.size()){
auto p=sa.begin();
auto [u1,v1]=*(p);
auto q=sb.end();q--;
auto [u2,v2]=*q;
if(u1>=u2) break;
if(find(v1)!=find(v2)){
ans+=2*(u2-u1);
merge(v1,v2);
sa.erase(p);sb.erase(q);
}
else if(find(v1)==find(v2)&&sa.size()==1){
ans+=2*(u2-u1);
merge(v1,v2);
sa.erase(p);sb.erase(q);
}
else{
auto pp=p;
pp++;
auto qq=q;
qq--;
auto [u11,v11]=*pp;
auto [u22,v22]=*qq;
int x12=u22-u1,x21=u2-u11;
int minx=max(x12,x21);
if(minx<0){
ans=res;
break;
}
if(x12==minx){
ans+=x12*2;
merge(v1,v22);
sa.erase(p);sb.erase(qq);
}
else if(x21==minx){
ans+=x21*2;
merge(v11,v2);
sa.erase(pp);sb.erase(q);
}
}
}
cout<<ans<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9696kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 1ms
memory: 7652kb
input:
2 732734236 669531729 368612323 916696032
output:
484881202
result:
ok single line: '484881202'
Test #3:
score: 0
Accepted
time: 71ms
memory: 24852kb
input:
96739 960257511 790393830 766983782 374475134 476482176 529858300 310917487 76906076 81191659 653180897 119832756 362638666 228783015 626981197 74837554 781854003 663345706 153150021 576244413 708232220 842559077 417230462 249522463 815253339 391663517 742521049 60507117 258429449 112143256 81408991...
output:
48459768018811
result:
ok single line: '48459768018811'
Test #4:
score: 0
Accepted
time: 78ms
memory: 24736kb
input:
96785 270111821 467151412 338110212 587493839 880232679 190826309 757074784 388758646 266705411 248083736 932249293 549449409 813345622 128324633 997551674 739367275 118482916 584132988 530659142 718566145 843527299 299368287 351399254 123105310 94363217 996225281 372809651 824469842 227112349 94127...
output:
48406205356958
result:
ok single line: '48406205356958'
Test #5:
score: 0
Accepted
time: 81ms
memory: 24816kb
input:
95819 593599221 692270415 966618937 134812164 440132242 805388141 904873362 529377140 758635067 696205546 760800727 989161190 987540482 498337722 998861034 142965841 153772097 807761700 274559068 988306570 582789805 25357702 672285126 901116830 522823375 37184847 684761653 343776558 745142497 150594...
output:
47840564997532
result:
ok single line: '47840564997532'
Test #6:
score: 0
Accepted
time: 58ms
memory: 24268kb
input:
90554 938804478 938811320 317142499 317149485 342875791 342879712 932975135 932975477 351554666 351570371 550654845 550656473 30714131 30726317 376522835 376526976 69261096 69263592 7393811 7400915 365372935 365376473 859045203 859053499 871017049 871021068 449046577 449046757 427167653 427174136 93...
output:
45267474329725
result:
ok single line: '45267474329725'
Test #7:
score: 0
Accepted
time: 75ms
memory: 24732kb
input:
96261 76198177 76202027 79123680 79124953 513370883 513377553 703603487 703607523 866389252 866390528 139712611 139719009 594522938 594532698 505132016 505134743 605883372 605883641 873915530 873917738 382722258 382729415 540540613 540542021 181157718 181159310 527796215 527806737 137531602 13753437...
output:
48212364519824
result:
ok single line: '48212364519824'
Test #8:
score: 0
Accepted
time: 72ms
memory: 23936kb
input:
91037 326787671 595514434 390694388 871968389 158927546 528620585 409465971 702548500 344910793 636126720 281023782 874898118 11439504 818259011 236508763 545046853 477677678 958867203 302706097 621338311 463199330 835427658 86609075 510670761 420713990 880480770 395156646 579396547 7488975 56384181...
output:
45419010766385
result:
ok single line: '45419010766385'
Test #9:
score: 0
Accepted
time: 73ms
memory: 25060kb
input:
98794 80383727 782796858 137203645 579335675 144425342 811413600 424179404 717502012 910964 875811082 45573824 851384137 464971206 810885972 116742543 999381272 201236005 713855876 257642532 856800308 187899391 774041510 111616203 577225579 442396049 514145473 278843534 670375805 2374052 617021553 3...
output:
49496018572021
result:
ok single line: '49496018572021'
Test #10:
score: 0
Accepted
time: 83ms
memory: 25044kb
input:
99501 498333490 806137098 35925096 850756491 330762085 991137593 485721051 690662947 97287567 718810550 204374526 954732752 280350902 745389794 261096345 804353570 232497375 734787637 192075946 654396123 459149799 534354129 441495626 952937432 11656899 614357926 98839184 565976186 187889698 87856883...
output:
49818210067811
result:
ok single line: '49818210067811'
Test #11:
score: 0
Accepted
time: 78ms
memory: 24668kb
input:
94028 575887384 75153129 870611096 92953794 734249592 90675 730728093 133270382 981688181 435238135 874752667 175861433 868884981 81364177 928674103 114809058 836879982 445715337 751185351 488807493 709154049 171575832 757173939 212090259 875117842 160432150 819644050 88940283 758121004 276790622 78...
output:
47030556934490
result:
ok single line: '47030556934490'
Test #12:
score: 0
Accepted
time: 76ms
memory: 25544kb
input:
91418 536353001 216266478 838540775 440508546 572275435 453672474 568397395 423845924 960623944 169546069 667240041 292467280 668951541 12533287 519656538 286934079 811971468 112714141 606717014 394418532 813904109 52418576 608904365 226757010 872012649 477806204 762185174 13492566 750948794 1218542...
output:
45736914061688
result:
ok single line: '45736914061688'
Test #13:
score: 0
Accepted
time: 1ms
memory: 7684kb
input:
2 140458601 663037556 591576577 440865614
output:
371867992
result:
ok single line: '371867992'
Test #14:
score: 0
Accepted
time: 75ms
memory: 24804kb
input:
95454 892086933 487735960 893224613 462657318 855351796 257242495 506906329 359225481 920780676 67880402 822959564 91352364 871278422 83285970 942168103 308976648 761212830 484407530 670906719 342590296 863109987 243063920 714680580 310049340 558196518 205463845 897351337 232820743 977038743 3597834...
output:
47679596819382
result:
ok single line: '47679596819382'
Test #15:
score: -100
Wrong Answer
time: 80ms
memory: 26448kb
input:
98594 76143234 818807357 45026957 677425022 395188520 639985807 806785643 335294300 443503843 988576190 179750597 644553071 397333457 583276866 40429254 627472374 907309987 232676624 465423406 596532409 563124081 191872969 656115184 129553291 262006836 502487125 413089431 578190279 925666285 2818736...
output:
49277921189578
result:
wrong answer 1st lines differ - expected: '49277921182522', found: '49277921189578'