QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#537117 | #7993. 哈密顿 | ccwss113 | AC ✓ | 376ms | 8688kb | C++20 | 2.0kb | 2024-08-29 21:06:11 | 2024-08-29 21:06:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100007], b[100007];
ll pre[100007], suf[100007];
priority_queue<int, vector<int>, greater<int>> q1;
priority_queue<ll, vector<ll>, greater<ll>> q2;
struct node
{
ll a;
ll b;
} c[100007];
bool operator<(node a, node b)
{
return a.a - a.b > b.a - b.b;
}
inline ll check(int n, int k) // 判断是否满足哈密顿
{
if (k == 0 || k * 2 > n)
{
return 0x8000000000000000ll;
}
ll cur = 0, ans = 0x8000000000000000ll;
while (!q1.empty())
q1.pop();
for (int i = 1; i <= n; i++)
{
cur += c[i].a;
q1.push(c[i].a);
if (i > k)
{
cur -= q1.top();
q1.pop();
}
pre[i] = cur;
}
cur = 0;
while (!q2.empty())
q2.pop();
for (int i = n; i >= 1; i--)
{
cur += c[i].b;
q2.push(c[i].b);
if (i + k <= n)
{
cur -= q2.top();
q2.pop();
}
suf[i] = cur;
}
for (int i = k; i + k <= n; i++)
{
ans = max(ans, pre[i] + suf[i + 1]);
}
return ans;
}
int main()
{
int n, l = 0, r, pos;
ll sum1 = 0, sum2 = 0;
cin >> n;
r = n / 2;
// 权值为max(api−bpimodn+1,bpimodn+1−api)
for (int i = 1; i <= n; i++)
{
int p, q;
cin >> a[i] >> b[i];
p = a[i] - b[i];
sum1 += p;
p = abs(p);
sum2 += p; // 权值和
q = a[i] + b[i];
c[i].a = q - p;
c[i].b = -(ll)q - p;
}
sort(c + 1, c + n + 1);
// 二分
while (l <= r)
{
int mid = (l + r) >> 1;
if (check(n, mid) >= check(n, mid + 1))
{
r = mid - 1;
pos = mid;
}
else
{
l = mid + 1;
}
}
cout << max(llabs(sum1), check(n, pos) + sum2);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5596kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5664kb
input:
2 732734236 669531729 368612323 916696032
output:
484881202
result:
ok single line: '484881202'
Test #3:
score: 0
Accepted
time: 376ms
memory: 8656kb
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: 361ms
memory: 8340kb
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: 373ms
memory: 8688kb
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: 324ms
memory: 8640kb
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: 346ms
memory: 8604kb
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: 162ms
memory: 8456kb
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: 178ms
memory: 8472kb
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: 176ms
memory: 8340kb
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: 171ms
memory: 8328kb
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: 167ms
memory: 8288kb
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: 7728kb
input:
2 140458601 663037556 591576577 440865614
output:
371867992
result:
ok single line: '371867992'
Test #14:
score: 0
Accepted
time: 174ms
memory: 8400kb
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: 179ms
memory: 8460kb
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: 162ms
memory: 8152kb
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: 163ms
memory: 8320kb
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: 167ms
memory: 8308kb
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: 5660kb
input:
2 912495944 921496906 684231244 705599779
output:
444161827
result:
ok single line: '444161827'
Test #20:
score: 0
Accepted
time: 0ms
memory: 5672kb
input:
2 164265056 638905221 377861347 374775793
output:
471554611
result:
ok single line: '471554611'
Test #21:
score: 0
Accepted
time: 0ms
memory: 7704kb
input:
2 378468855 99775891 282775253 82393517
output:
479074700
result:
ok single line: '479074700'
Test #22:
score: 0
Accepted
time: 1ms
memory: 5728kb
input:
2 803421378 80661720 25286098 235676316
output:
623120684
result:
ok single line: '623120684'
Test #23:
score: 0
Accepted
time: 1ms
memory: 5652kb
input:
3 879355347 345209287 16022057 28202913 356307025 531195111
output:
1377423226
result:
ok single line: '1377423226'
Test #24:
score: 0
Accepted
time: 1ms
memory: 5672kb
input:
3 777512132 830453907 377527049 118678525 723716592 246062742
output:
1589414315
result:
ok single line: '1589414315'
Test #25:
score: 0
Accepted
time: 1ms
memory: 5668kb
input:
3 81845571 960425152 351393001 427966772 170489038 736977642
output:
1521641956
result:
ok single line: '1521641956'