QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#507707#7993. 哈密顿huaxiamengjinAC ✓69ms15808kbC++141.4kb2024-08-06 20:23:512024-08-06 20:23:51

Judging History

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

  • [2024-08-06 20:23:51]
  • 评测
  • 测评结果:AC
  • 用时:69ms
  • 内存:15808kb
  • [2024-08-06 20:23:51]
  • 提交

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];
bool cmp(pair<int,int> x,pair<int,int> y){
	return x.first>y.first;
}
int ct[200200],pre[200200],suf[200200],aa[200200],bb[200200];
signed main(){
	cin>>n;
	for (int i=1,x,y;i<=n;i++)
	cin>>x>>y,a[i]={x,i},b[i]={y,i},aa[i]=x,bb[i]=y;
	sort(a+1,a+n+1,cmp);
	sort(b+1,b+n+1,cmp);
	int ta=1,tb=1;
	for (int i=1;i<=n;i++){
		if(tb<=n&&(ta>n||a[ta].first<b[tb].first))ans+=b[tb].first,ct[b[tb].second]--,tb++;
		else ans+=a[ta].first,ct[a[ta].second]++,ta++;
	}
	bool fl=1;
	for (int i=1;i<=n;i++)
	if(ct[i]==0)fl=0;
	if(fl==1&&ta<=n&&tb<=n){
		for (int i=1;i<=n;i++){
			pre[i]=pre[i-1];
			if(ct[i]>0)pre[i]=max(pre[i],bb[i]);
			if(ct[i]<0)pre[i]=max(pre[i],aa[i]);
		}
		for (int i=n;i>=1;i--){
			suf[i]=suf[i+1];
			if(ct[i]>0)suf[i]=max(suf[i],bb[i]);
			if(ct[i]<0)suf[i]=max(suf[i],aa[i]);
		}
		int tmp=-1e9;
		for (int i=1;i<=n;i++)
		if(ct[i]>0)tmp=max(tmp,max(pre[i-1],suf[i+1])-aa[i]);
		else if(ct[i]<0)tmp=max(tmp,max(pre[i-1],suf[i+1])-bb[i]);
		ans+=2*tmp;
	}
	for (int i=ta;i<=n;i++)ans-=a[i].first;
	for (int i=tb;i<=n;i++)ans-=b[i].first;
	int sum=0;
	for (int i=1;i<=n;i++)
	sum+=aa[i]-bb[i];
	ans=max(ans,sum);sum=0;
	for (int i=1;i<=n;i++)
	sum+=bb[i]-aa[i];
	cout<<max(sum,ans)<<"\n";
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 9780kb

input:

3
1 10
8 2
4 5

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 0ms
memory: 11880kb

input:

2
732734236 669531729
368612323 916696032

output:

484881202

result:

ok single line: '484881202'

Test #3:

score: 0
Accepted
time: 62ms
memory: 14604kb

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: 63ms
memory: 14244kb

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: 66ms
memory: 14624kb

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: 61ms
memory: 14640kb

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: 0
Accepted
time: 62ms
memory: 14548kb

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:

48212364519824

result:

ok single line: '48212364519824'

Test #8:

score: 0
Accepted
time: 61ms
memory: 14728kb

input:

91037
326787671 595514434
390694388 871968389
158927546 528620585
409465971 702548500
344910793 636126720
281023782 874898118
11439504 818259011
236508763 545046853
477677678 958867203
302706097 621338311
463199330 835427658
86609075 510670761
420713990 880480770
395156646 579396547
7488975 56384181...

output:

45419010766385

result:

ok single line: '45419010766385'

Test #9:

score: 0
Accepted
time: 67ms
memory: 14640kb

input:

98794
80383727 782796858
137203645 579335675
144425342 811413600
424179404 717502012
910964 875811082
45573824 851384137
464971206 810885972
116742543 999381272
201236005 713855876
257642532 856800308
187899391 774041510
111616203 577225579
442396049 514145473
278843534 670375805
2374052 617021553
3...

output:

49496018572021

result:

ok single line: '49496018572021'

Test #10:

score: 0
Accepted
time: 62ms
memory: 15752kb

input:

99501
498333490 806137098
35925096 850756491
330762085 991137593
485721051 690662947
97287567 718810550
204374526 954732752
280350902 745389794
261096345 804353570
232497375 734787637
192075946 654396123
459149799 534354129
441495626 952937432
11656899 614357926
98839184 565976186
187889698 87856883...

output:

49818210067811

result:

ok single line: '49818210067811'

Test #11:

score: 0
Accepted
time: 61ms
memory: 15664kb

input:

94028
575887384 75153129
870611096 92953794
734249592 90675
730728093 133270382
981688181 435238135
874752667 175861433
868884981 81364177
928674103 114809058
836879982 445715337
751185351 488807493
709154049 171575832
757173939 212090259
875117842 160432150
819644050 88940283
758121004 276790622
78...

output:

47030556934490

result:

ok single line: '47030556934490'

Test #12:

score: 0
Accepted
time: 54ms
memory: 14176kb

input:

91418
536353001 216266478
838540775 440508546
572275435 453672474
568397395 423845924
960623944 169546069
667240041 292467280
668951541 12533287
519656538 286934079
811971468 112714141
606717014 394418532
813904109 52418576
608904365 226757010
872012649 477806204
762185174 13492566
750948794 1218542...

output:

45736914061688

result:

ok single line: '45736914061688'

Test #13:

score: 0
Accepted
time: 1ms
memory: 11896kb

input:

2
140458601 663037556
591576577 440865614

output:

371867992

result:

ok single line: '371867992'

Test #14:

score: 0
Accepted
time: 63ms
memory: 15808kb

input:

95454
892086933 487735960
893224613 462657318
855351796 257242495
506906329 359225481
920780676 67880402
822959564 91352364
871278422 83285970
942168103 308976648
761212830 484407530
670906719 342590296
863109987 243063920
714680580 310049340
558196518 205463845
897351337 232820743
977038743 3597834...

output:

47679596819382

result:

ok single line: '47679596819382'

Test #15:

score: 0
Accepted
time: 69ms
memory: 15704kb

input:

98594
76143234 818807357
45026957 677425022
395188520 639985807
806785643 335294300
443503843 988576190
179750597 644553071
397333457 583276866
40429254 627472374
907309987 232676624
465423406 596532409
563124081 191872969
656115184 129553291
262006836 502487125
413089431 578190279
925666285 2818736...

output:

49277921182522

result:

ok single line: '49277921182522'

Test #16:

score: 0
Accepted
time: 60ms
memory: 15472kb

input:

91122
786589865 127656076
614755926 422729931
384119686 710113770
904124383 246006743
530902456 391959660
952938544 92229163
139137244 694758446
883093261 359223277
778896477 175578843
693246672 24026360
325204430 547464427
553220339 226951341
860555325 80810696
44032978 723239254
474693476 90285856...

output:

45529105511617

result:

ok single line: '45529105511617'

Test #17:

score: 0
Accepted
time: 61ms
memory: 15752kb

input:

91839
194810983 578876203
948659341 420946108
595421721 168531626
957384846 134591284
108489289 769399462
789963921 301424358
426046587 729160381
566718126 451163408
393246520 646693638
811471214 437317251
142008707 882262117
253173755 905995483
89175918 605371982
809662077 114801141
533200887 27775...

output:

46063618451751

result:

ok single line: '46063618451751'

Test #18:

score: 0
Accepted
time: 63ms
memory: 15664kb

input:

94387
309338555 674188079
455807233 717107907
476348222 833127182
822048679 194897833
997992651 160564685
278521530 991726513
924886849 139326163
291042651 950850510
349448449 752586711
313392201 769771344
262685155 744714381
776420965 292614646
581184895 101666717
587580105 453727594
439858436 9884...

output:

47214775174589

result:

ok single line: '47214775174589'

Test #19:

score: 0
Accepted
time: 1ms
memory: 11800kb

input:

2
912495944 921496906
684231244 705599779

output:

444161827

result:

ok single line: '444161827'

Test #20:

score: 0
Accepted
time: 1ms
memory: 11800kb

input:

2
164265056 638905221
377861347 374775793

output:

471554611

result:

ok single line: '471554611'

Test #21:

score: 0
Accepted
time: 1ms
memory: 11748kb

input:

2
378468855 99775891
282775253 82393517

output:

479074700

result:

ok single line: '479074700'

Test #22:

score: 0
Accepted
time: 1ms
memory: 11896kb

input:

2
803421378 80661720
25286098 235676316

output:

623120684

result:

ok single line: '623120684'

Test #23:

score: 0
Accepted
time: 1ms
memory: 11828kb

input:

3
879355347 345209287
16022057 28202913
356307025 531195111

output:

1377423226

result:

ok single line: '1377423226'

Test #24:

score: 0
Accepted
time: 0ms
memory: 11888kb

input:

3
777512132 830453907
377527049 118678525
723716592 246062742

output:

1589414315

result:

ok single line: '1589414315'

Test #25:

score: 0
Accepted
time: 2ms
memory: 11896kb

input:

3
81845571 960425152
351393001 427966772
170489038 736977642

output:

1521641956

result:

ok single line: '1521641956'