QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196626#6420. Certain Scientific RailgunpanhuachaoAC ✓116ms13328kbC++205.0kb2023-10-01 20:34:402023-10-01 20:34:41

Judging History

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

  • [2023-10-01 20:34:41]
  • 评测
  • 测评结果:AC
  • 用时:116ms
  • 内存:13328kb
  • [2023-10-01 20:34:40]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
using namespace std;
const ll inf = 1e12;

ll n; pll a[100010];
set<pll> s1, s2; ll s3;
ll max0, min0, max1[100010], min1[100010];

ll cost(ll l0, ll l1, ll r0, ll r1){
    return max(l1, r1) - min(l0, r0) + abs(max(l1, r1));
}
ll solve(int revx, int revy){
    // printf("revx=%d, revy=%d:\n", revx, revy);
    for(int i = 1; i <= n; i++)
        a[i].first *= revx, a[i].second *= revy;
    sort(a+1, a+n+1);
    // for(int i = 1; i <= n; i++)
        // printf("(%-3lld %-3lld)", a[i].first, a[i].second);
    // printf("\n");
    max1[n] = -inf; min1[n] = inf;
    for(int i = n-1; i >= 1; i--){
        max1[i] = max(max1[i+1], a[i+1].second);
        min1[i] = min(min1[i+1], a[i+1].second);
    }
    for(int i = n-1; i >= 1; i--)
        if(a[i].first == a[i+1].first)
            max1[i] = max1[i+1], min1[i] = min1[i+1];
    // for(int i = 1; i <= n; i++)
        // printf("(%-3lld %-3lld)", min1[i], max1[i]);
    // printf("\n");
    
    ll ans = inf;
    max0 = -inf, min0 = inf;
    for(int i = n; i >= 1 && a[i].first > 0; i--)
        max0 = max(max0, a[i].second), min0 = min(min0, a[i].second);
    for(int i = 1; i <= n && a[i].first <= 0; i++){
        if(max0 == -inf && i == 1)
            ans = min(ans, -a[i].first);
        else
            ans = min(ans, -a[i].first + max0 - min0 + min(abs(max0), abs(min0)));
        min0 = min(min0, a[i].second);
        max0 = max(max0, a[i].second);
    }
    ans = min(ans, max0 - min0 + min(abs(max0), abs(min0)));
    // printf("ans=%lld\n", ans);

    int tp = -1;
    s1.clear(); s2.clear(); s3 = inf;
    max0 = -inf, min0 = inf;
    ll t1 = n, t2 = n;
    for(int i = n; i >= 1 && a[i].first > 0; i--)
        s1.insert(make_pair(max1[i]-min1[i]+abs(max1[i])+a[i].first, i));
    a[0].first = -inf;
    for(int i = 1; i <= n && a[i].first < 0; i++){
        if(a[i].first == a[i-1].first){
            max0 = max(a[i].second, max0);
            min0 = min(a[i].second, min0);
            continue;
        }

        while(t2 > t1 && (max1[t2] <= max0 && min1[t2] >= min0)){
            ll t = (tp? max1[t2]+abs(max1[t2])+a[t2].first: -min1[t2]+a[t2].first);
            set<pll>::iterator it = s2.find(make_pair(t, t2));
            assert(it != s2.end());
            s3 = min(s3, a[t2].first);
            s2.erase(it);
            t2--;
        }
        if(t1 == t2) tp = -1;

        while(a[t1].first > 0 && (max1[t1] <= max0 || min1[t1] >= min0)){
            set<pll>::iterator it = s1.find(make_pair(max1[t1]-min1[t1]+abs(max1[t1])+a[t1].first, t1));
            assert(it != s1.end());
            if(max1[t1] <= max0 && min1[t1] < min0){
                // printf("# %lld %lld %d\n", t1, t2, tp);
                assert(tp != 1);
                s2.insert(make_pair(-min1[t1]+a[t1].first, t1)), tp = 0;
            }
            if(max1[t1] > max0 && min1[t1] >= min0){
                // printf("# %lld %lld %d\n", t1, t2, tp);
                assert(tp != 0);
                s2.insert(make_pair(max1[t1]+abs(max1[t1])+a[t1].first, t1)), tp = 1;
            }
            if(max1[t1] <= max0 && min1[t1] >= min0){
                assert(t1 == t2);
                s3 = min(s3, a[t2].first);
                t2--;
            }
            s1.erase(it);
            t1--;
        }
        // printf("%d %lld %lld %lld %lld\n", i, t1, t2, min0, max0);
        
        if(a[t1].first > 0){
            ans = min(ans, 2*(-a[i].first) + s1.begin()->first);
            // printf("1* %d %lld %lld\n", i, ans, s1.begin()->second);
        }

        if(t2 > t1){
            assert(tp != -1);
            if(tp) ans = min(ans, 2*(-a[i].first) - min0 + (s2.begin()->first));
              else ans = min(ans, 2*(-a[i].first) + max0 + abs(max0) + (s2.begin()->first));
            // printf("2* %d %lld %lld %d\n", i, ans, s2.begin()->second, tp);
        }

        if(max0 != -inf)
            ans = min(ans, 2*(-a[i].first) + max0 - min0 + abs(max0) + s3);
        else
            ans = min(ans, 2*(-a[i].first) + s3);
        // printf("3* %d %lld %lld\n", i, ans, s3);
        
        max0 = max(a[i].second, max0);
        min0 = min(a[i].second, min0);
    }

    // printf("ans=%lld\n", ans);
    for(int i = 1; i <= n; i++)
        a[i].first *= revx, a[i].second *= revy;
    return ans;
}

void solve0(){
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++)
        scanf("%lld%lld", &a[i].first, &a[i].second);
    ll ans = inf;
    ans = min(ans, solve(1, 1));
    ans = min(ans, solve(1, -1));
    ans = min(ans, solve(-1, 1));
    ans = min(ans, solve(-1, -1));
    printf("%lld\n", ans);
    return;
}

int main(){
    int t; scanf("%d", &t);
    while(t--) solve0();
    return 0;
}

/*
5
-1 -4
0 3
-1 1
-4 1
2 -4

5
-1 4
-2 -1
3 0
-1 0
4 4

5
4 -4
4 1
1 1
-1 4
-2 4

10
11 -1
0 -15
1 -3
13 -11
14 -5
13 -7
-14 -3
-11 -6
-1 -10
12 3

4
-4 -2
-1 -1
3 -2
2 -3
*/

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3780kb

input:

3
2
0 1
1 0
4
1 1
-3 -3
4 -4
-2 2
4
1 100
3 100
-100 1
3 -100

output:

0
8
4

result:

ok 3 number(s): "0 8 4"

Test #2:

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

input:

120
4
11 11
-22 -22
33 -33
-44 44
4
-11 11
22 -22
-33 -33
44 44
4
-11 -11
22 22
-33 33
44 -44
4
11 -11
-22 22
33 33
-44 -44
4
-11 11
22 -22
33 33
-44 -44
4
11 11
-22 -22
-33 33
44 -44
4
11 -11
-22 22
-33 -33
44 44
4
-11 -11
22 22
33 -33
-44 44
4
1 1
-2 -2
3 -3
-4 4
4
-1 1
2 -2
-3 -3
4 4
4
-1 -1
2 2
...

output:

99
99
99
99
99
99
99
99
9
9
9
9
9
9
9
9
5
5
5
5
5
5
5
5
4
4
4
4
4
4
4
4
0
0
0
0
0
0
0
0
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
12
12
12
12
12
12
12
12
0
0
0
0
0
0
0
0
11
11
11
11
11
11
11
11
111
111
111
111
111
111
111
111
300
300
300
300
300
300
300
300
5
5
5
5
5
5
5
5
2
2
2
2
2
2
2
2
48
48
48
48
48
48
48...

result:

ok 120 numbers

Test #3:

score: 0
Accepted
time: 58ms
memory: 13328kb

input:

1
100000
259716243 240441467
199457754 301066970
412772262 87809313
139833960 359731944
453667163 46926477
204936243 294432582
361538994 138967777
315714786 184515556
274799986 224851893
315004381 184713646
104778725 394583171
100521423 399036944
392072619 107037839
19992953 480118328
166443446 3325...

output:

500573654

result:

ok 1 number(s): "500573654"

Test #4:

score: 0
Accepted
time: 112ms
memory: 10120kb

input:

1
100000
231900334 268958119
8094073 491651466
181235767 318579810
152422229 348402615
453103138 47722205
169802544 329762126
66615606 433110853
63974760 435613334
412296501 86899836
380626267 120285438
234254409 265425647
343489254 155604658
456112096 43577922
389402607 110812358
361862073 13733807...

output:

500924008

result:

ok 1 number(s): "500924008"

Test #5:

score: 0
Accepted
time: 116ms
memory: 10096kb

input:

1
100000
243687982 257271514
406262214 92746823
115474686 384860427
179141355 321775276
111057575 388052482
85754748 413966937
89015940 411624061
97395736 402451385
394966118 105231558
118633450 381544518
121485690 377952946
93095255 406155875
285623743 214726445
230422095 268632132
137612706 361757...

output:

1001565829

result:

ok 1 number(s): "1001565829"

Test #6:

score: 0
Accepted
time: 106ms
memory: 10132kb

input:

1
100000
450661745 49199859
46152367 453651505
490854754 8343231
273253564 227017249
177014519 322480838
295759745 203932936
261849726 238301630
64867210 434153377
105275689 393750931
321581727 178232552
494756932 6104054
31280896 468013695
46903507 453060294
162085346 337538866
239622896 259681652
...

output:

1001452257

result:

ok 1 number(s): "1001452257"

Test #7:

score: 0
Accepted
time: 79ms
memory: 10116kb

input:

1
100000
150929109 349489222
6420724 493823770
464662613 35982721
402411960 97813587
40230561 460488339
470554995 30114056
344276320 154962447
219702967 280132033
104632512 395604723
479497355 19601658
117766907 383020273
36133778 463836986
175546685 324588426
43749701 456053065
130176612 369176811
...

output:

1501257683

result:

ok 1 number(s): "1501257683"

Test #8:

score: 0
Accepted
time: 38ms
memory: 6720kb

input:

8
10000
108715119 390304225
45853335 454319274
399590321 101245208
273660814 225432152
97670975 403201669
476219330 23861282
58911490 440808774
128367407 370649954
306462809 192987122
396996806 103458329
129292472 370369176
135445802 364903545
9556524 491441067
272140862 228185209
394219336 10628059...

output:

499504970
499504970
499504970
499504970
499504970
499504970
499504970
499504970

result:

ok 8 numbers

Test #9:

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

input:

8
10000
88475214 411685526
295971508 203464188
475025201 25141393
312127126 188194692
19542945 480366130
488061278 12628967
186934840 313365863
481957571 18601403
358812248 140919718
22594424 477659644
217700756 281415099
165264320 334364787
117366326 382623925
104117966 396054687
249226386 25018520...

output:

1000322231
1000322231
1000322231
1000322231
1000322231
1000322231
1000322231
1000322231

result:

ok 8 numbers

Test #10:

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

input:

8
10000
269878538 230854020
456626682 43333438
434144544 66494813
232368812 267733469
68096662 431017351
17852539 482545915
87803855 412171111
265856716 234461260
115315829 384621424
31746172 467793722
373607866 126486181
23907446 476867982
333063328 166736814
343578833 156172707
244551435 255629961...

output:

1000479661
1000479661
1000479661
1000479661
1000479661
1000479661
1000479661
1000479661

result:

ok 8 numbers

Test #11:

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

input:

8
10000
159480657 340897530
349562243 150040650
173755554 326392525
282856769 217394893
392793822 107352227
491621792 7387423
440310768 58951882
187152471 313516759
227502759 272030922
48462295 450696921
183559088 317228169
365794915 133411085
345365867 154485210
493284111 6477090
397676402 10174717...

output:

1000582139
1000582139
1000582139
1000582139
1000582139
1000582139
1000582139
1000582139

result:

ok 8 numbers

Test #12:

score: 0
Accepted
time: 47ms
memory: 6204kb

input:

8
10000
445062805 54324634
73636583 427209121
94242291 406610072
426842404 73694287
124540787 376414432
56028719 443696776
390646024 108939638
322649687 177349434
398037737 102667192
130048845 369861315
51589239 448100026
11585337 488662930
291289277 208605416
314642705 186222797
405227467 95703222
...

output:

1497536843
1497536843
1497536843
1497536843
1497536843
1497536843
1497536843
1497536843

result:

ok 8 numbers

Test #13:

score: 0
Accepted
time: 23ms
memory: 6008kb

input:

5000
16
29 -33
-5 -42
3 -2
-21 -32
11 43
-12 28
50 10
-31 47
49 -11
43 -45
-30 33
-34 -40
18 49
-50 8
31 8
-6 -31
14
34 6
31 -7
48 16
-18 23
44 -35
7 -49
-12 -48
15 3
7 -15
9 18
-21 -8
24 32
-3 44
5 -12
17
16 20
4 46
18 -38
-19 -43
-36 9
-28 -38
38 9
35 40
27 -31
37 -24
-28 -3
3 -11
16 41
4 -43
-48 ...

output:

126
90
126
54
86
118
125
8
116
123
94
63
94
46
100
103
82
88
91
108
116
111
38
88
109
121
11
16
133
40
52
139
126
44
133
24
70
101
122
105
86
52
32
122
44
30
87
35
95
99
78
109
133
64
82
80
111
22
123
108
92
144
104
62
60
102
67
49
22
98
58
119
109
138
75
71
133
90
88
91
59
110
119
136
9
94
85
105
6...

result:

ok 5000 numbers

Test #14:

score: 0
Accepted
time: 64ms
memory: 10048kb

input:

1
100000
141603120 358395957
91949299 408051533
280127622 219872540
269839621 230159940
398246595 101753077
348822087 151177633
348419099 151581884
296902611 203098137
338097907 161901334
372622396 127377407
394835486 105164842
447472828 52526264
400180018 99819426
413592564 86408119
360494425 13950...

output:

1349927669

result:

ok 1 number(s): "1349927669"

Test #15:

score: 0
Accepted
time: 81ms
memory: 10608kb

input:

1
90000
443555563 56444294
83262571 416738313
281645457 218355326
445801717 54198908
83627758 416372853
211139494 288860857
361318442 138681703
212318967 287681202
149020856 350978578
123082307 376916773
191763673 308236472
263766243 236233924
339734272 160266716
376958675 123040885
164258118 335742...

output:

899981686

result:

ok 1 number(s): "899981686"

Test #16:

score: 0
Accepted
time: 113ms
memory: 10052kb

input:

1
100000
391869133 108131089
153296049 346704832
435824119 64176139
402512179 97488625
108905299 391095683
287197881 212802334
303152943 196846093
337713912 162286279
249180885 250819612
178060316 321940429
393979501 106020779
66320276 433680592
116087834 383911295
65460918 434538801
304863521 19513...

output:

449995391

result:

ok 1 number(s): "449995391"

Test #17:

score: 0
Accepted
time: 110ms
memory: 10084kb

input:

1
100000
443245093 56754965
154391186 345608928
423601982 76397561
391610115 108390868
118474673 381525118
240893979 259106413
221239315 278759789
419622607 80377653
254528678 245471355
296991874 203008329
423257726 76742206
52387171 447612751
394711133 105288839
311036506 188963231
299331944 200667...

output:

449997854

result:

ok 1 number(s): "449997854"

Test #18:

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

input:

1
100000
384625660 115374409
225960109 274039535
178741365 321258128
170591713 329407412
308060861 191938282
220134970 279865276
213943533 286056786
103518048 396481052
207461712 292537600
252876866 247123499
351804867 148195958
64016058 435984211
52872452 447126785
86817782 413183092
220795830 2792...

output:

449994089

result:

ok 1 number(s): "449994089"

Test #19:

score: 0
Accepted
time: 65ms
memory: 13244kb

input:

1
100000
77035514 422964491
178817513 321182493
158098643 341901347
288748161 211251852
376028292 123971728
195670056 304329924
148630742 351369259
101439376 398560643
50630609 449369410
78580086 421419917
446984671 53015338
426980710 73019271
272161740 227838245
322523473 177476546
445486159 545138...

output:

449991329

result:

ok 1 number(s): "449991329"