QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#556198 | #7993. 哈密顿 | wangjingheng | WA | 0ms | 15892kb | C++20 | 1.7kb | 2024-09-10 15:47:18 | 2024-09-10 15:47:19 |
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];
array<int,3> c[N],w[N];
set<pair<int,int> >sa,sb;
int fa[N],vin[N],vout[N],vis[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,i,0ll};
cin>>x;
b[i]={x,i};
c[++t]={x,i,1ll};
}
sort(c+1,c+t+1);
int res=0;
for(int i=1;i<=t/2;i++){
res-=c[i][0];
}
for(int i=t/2+1;i<=t;i++) res+=c[i][0];
int ans=0,cnt=n;
for(int i=1;i<=n;i++){
if(c[i][2]==1){
vin[c[i][1]]=1;
}
}
for(int i=n+1;i<=n+n;i++){
if(c[i][2]==0){
vout[c[i][1]]=1;
}
}
cnt=0;
int ok=0,okk=0;
for(int i=1;i<=n;i++){
if(vin[i]+vout[i]==0) continue;
if(vin[i]==1&&vout[i]==0) ok=1;
if(vin[i]==0&&vout[i]==1) okk=1;
if(vin[i]+vout[i]==2) cnt++,vis[i]=1;
}
if(ok&&okk||cnt==n){
cout<<res<<"\n";return;
}
int tt=0,ll=1e13,rr=0;
for(int i=1;i<=n;i++){
if(vis[i]){
w[++tt]={a[i].first,a[i].second,0};w[++tt]={b[i].first,b[i].second,1};
}
}
sort(w+1,w+tt+1);
if(cnt==1){
res-=2*(w[2][0]-w[1][0]);
cout<<res<<"\n";return;
}
for(int i=1;i<tt;i++){
if(w[i][2]==0&&w[i+1][2]==1){
int u2=w[i-1][0],u1=w[i][0],v1=w[i+1][0],v2=w[i+2][0];
if(w[i][1]!=w[i+1][1]){
res-=2*(v1-u1);
}
else{
int minx=1e13;
minx=min(v1-u2,v2-u1);
res-=2*minx;
}
break;
}
}
cout<<res<<"\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: 0
Wrong Answer
time: 0ms
memory: 15892kb
input:
3 1 10 8 2 4 5
output:
4
result:
wrong answer 1st lines differ - expected: '10', found: '4'