QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#556135 | #7993. 哈密顿 | wangjingheng | WA | 120ms | 26604kb | C++20 | 1.8kb | 2024-09-10 15:17:55 | 2024-09-10 15:17:55 |
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++){
if(c[i].second==0&&c[i+1].second==1){
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: 9772kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 0ms
memory: 9708kb
input:
2 732734236 669531729 368612323 916696032
output:
484881202
result:
ok single line: '484881202'
Test #3:
score: 0
Accepted
time: 116ms
memory: 26468kb
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: 120ms
memory: 24636kb
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: 119ms
memory: 24704kb
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: 94ms
memory: 23940kb
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: -100
Wrong Answer
time: 88ms
memory: 26604kb
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:
48212364519822
result:
wrong answer 1st lines differ - expected: '48212364519824', found: '48212364519822'