QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#507498 | #7993. 哈密顿 | huaxiamengjin | WA | 105ms | 16036kb | C++14 | 1.8kb | 2024-08-06 18:24:41 | 2024-08-06 18:24:43 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
struct node{
int x,y,z;
}c[400400];
int id,ans;
int fa[200200];
int used[200200][2];
pair<int,int>a[200200],b[200200];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >qa,qb;
bool cmp(node x,node y){
return x.x>y.x;
}
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void bing(int x,int y){
x=find(x),y=find(y);
fa[x]=y;
}
signed main(){
cin>>n;
for (int i=1,x,y;i<=n;i++)
cin>>x>>y,c[++id]=(node){x,i,0},c[++id]=(node){y,i,1},
qa.push({x,i}),qb.push({y,i}),fa[i]=fa[i+n]=i;
sort(c+1,c+id+1,cmp);
for (int i=1;i<=id;i++)
if(used[c[i].y][c[i].z]==0){
if(c[i].z==1){
vector<pair<int,int> >t;
while(qa.size()){
pair<int,int>tmp=qa.top();
if(used[tmp.second][0]==0){
if(find(tmp.second)==find(c[i].y+n))t.push_back(tmp);
else {
used[tmp.second][0]=1;used[c[i].y][c[i].z]=1;
// cout<<c[i].x<<" "<<tmp.first<<"****\n";
ans+=abs(c[i].x-tmp.first);
bing(tmp.second,c[i].y+n);qa.pop();
break;
}
}
qa.pop();
}
for (auto i:t)qa.push(i);
}else{
vector<pair<int,int> >t;
while(qb.size()){
pair<int,int>tmp=qb.top();
if(used[tmp.second][1]==0){
if(find(tmp.second+n)==find(c[i].y))t.push_back(tmp);
else {
// cout<<c[i].x<<" "<<tmp.first<<"\n";
used[tmp.second][1]=1;used[c[i].y][c[i].z]=1;
ans+=abs(c[i].x-tmp.first);
bing(tmp.second+n,c[i].y);qb.pop();
break;
}
}
qb.pop();
}
for (auto i:t)qb.push(i);
}
}
int xa=0,xb=0;
for (int i=1;i<=id;i++){
if(c[i].z==0&&used[c[i].y][0]==0)xa=c[i].x;
if(c[i].z==1&&used[c[i].y][1]==0)xb=c[i].x;
}
// cout<<xa<<"*&&&&"<<xb<<"\n";
ans+=abs(xa-xb);
cout<<ans<<"\n";
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5664kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5640kb
input:
2 732734236 669531729 368612323 916696032
output:
484881202
result:
ok single line: '484881202'
Test #3:
score: 0
Accepted
time: 105ms
memory: 16036kb
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: -100
Wrong Answer
time: 102ms
memory: 15900kb
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:
48406205343320
result:
wrong answer 1st lines differ - expected: '48406205356958', found: '48406205343320'