QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#556213 | #7993. 哈密顿 | wangjingheng | WA | 2ms | 15912kb | C++20 | 1.5kb | 2024-09-10 15:56:43 | 2024-09-10 15:56:45 |
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);
int u2=c[n-1][0],u1=c[n][0],v1=c[n+1][0],v2=c[n+2][0];
if(c[n][1]!=c[n+1][1]){
res-=2*(v1-u1);
}
else{
int minx=1e13;
minx=min(v1-u2,v2-u1);
res-=2*minx;
}
cout<<res<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 15912kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 15876kb
input:
2 732734236 669531729 368612323 916696032
output:
116957610
result:
wrong answer 1st lines differ - expected: '484881202', found: '116957610'