QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#556027#7993. 哈密顿wangjinghengWA 56ms19544kbC++201.6kb2024-09-10 14:18:372024-09-10 14:18:38

Judging History

你现在查看的是最新测评结果

  • [2024-09-10 14:18:38]
  • 评测
  • 测评结果:WA
  • 用时:56ms
  • 内存:19544kb
  • [2024-09-10 14:18:37]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100100;
pair<int,int> a[N],b[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;
	for(int i=1;i<=n;i++){
		fa[i]=i;
		int x;cin>>x;
		a[i]={x,i};
		cin>>x;
		b[i]={x,i};
	}
	sort(a+1,a+n+1);
	sort(b+1,b+n+1);
	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();
		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,x22=u22-u11;
				if(find(v11)==find(v22)) x22=-1e13;
				int minx=max(x12,x21);minx=max(minx,x22);
				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);
				}
				else{
					ans+=x22*2;
					merge(v11,v22);
					sa.erase(pp);sb.erase(qq);
				}
		}
	}
	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: 5704kb

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: 51ms
memory: 19544kb

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: 52ms
memory: 19532kb

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: 56ms
memory: 19444kb

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: 51ms
memory: 18612kb

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: 51ms
memory: 19536kb

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:

48212364510070

result:

wrong answer 1st lines differ - expected: '48212364519824', found: '48212364510070'