CSES - Aalto Competitive Programming 2024 - wk2 - Wed - Results
Submission details
Task:Old legos
Sender:aalto2024b_002
Submission time:2024-09-11 17:21:58 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#30.00 sdetails
#40.00 sdetails
#50.00 sdetails
#60.00 sdetails
#70.00 sdetails
#80.00 sdetails
#90.00 sdetails
#100.00 sdetails
#110.01 sdetails
#120.00 sdetails
#130.00 sdetails
#140.00 sdetails
#150.00 sdetails
#160.00 sdetails
#170.00 sdetails
#180.00 sdetails
#190.00 sdetails
#200.00 sdetails
#210.00 sdetails
#220.00 sdetails
#230.00 sdetails
#240.00 sdetails
#250.00 sdetails
#260.00 sdetails
#270.00 sdetails
#280.00 sdetails
#290.00 sdetails
#300.00 sdetails
#310.00 sdetails
#320.00 sdetails
#330.00 sdetails
#340.00 sdetails
#350.00 sdetails
#360.00 sdetails
#370.00 sdetails
#380.00 sdetails
#390.00 sdetails
#400.00 sdetails
#410.00 sdetails
#420.00 sdetails
#430.00 sdetails
#440.00 sdetails
#450.00 sdetails
#460.00 sdetails
#470.00 sdetails
#480.00 sdetails
#490.00 sdetails
#500.01 sdetails
#510.01 sdetails
#520.01 sdetails
#530.01 sdetails
#540.01 sdetails
#550.01 sdetails
#560.01 sdetails
#570.01 sdetails
#580.01 sdetails
#590.01 sdetails
#600.24 sdetails
#610.26 sdetails
#620.26 sdetails
#630.24 sdetails
#640.24 sdetails
#650.26 sdetails
#660.24 sdetails
#670.26 sdetails
#680.25 sdetails
#690.26 sdetails

Compiler report

input/code.cpp: In function 'int main(int, char**)':
input/code.cpp:122:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     for (int i = 0; i < UM.size(); ++i) {
      |                     ~~^~~~~~~~~~~

Code

#include <algorithm>
#include <iostream>
#include <limits>
#include <utility>
#include <vector>

/**
Uolevi and Maija are visiting their parents. While the blueberry pie is cooking
in the oven, they go explore the attic. They find their old legos and decide to
build castles from them. There are n legos in a box numbered 1,2,\dots,n, the
i-th of which has u_i nostalgic value to Uolevi and m_i to Maija. The siblings
then take turns in picking legos from the box and placing them to their castles.
Naturally, both try to maximize the value difference between their own castle
and their opponents castle. Formally, the legos will be divided into two sets U
and M at the end of the process. Uolevi's castle has value x = \sum_{i \in
U}{u_i} and Maija's castle has value y = \sum_{i \in M}{m_i}. Maija tries to
minimize x-y while Uolevi tries to maximize it. Calculate x-y if Uolevi gets to
pick the first lego and both pick the legos optimally.

Input
First line contain a single integer n.
The 𝑖-th of the next n lines contains two integers u_i and m_𝑖.

Output
Output a single integer x-y.
Constraints

1 \leq n \leq 10^5
1 \leq u_i, m_i \leq 10^9

Example 1
Input:
5
3 1
9 3
6 3
2 10
1 7

Output:
1

Explanation:
The sequence of picks would be:
Uolevi picks lego 2
Maija picks lego 4
Uolevi picks lego 3
Maija picks lego 5
Uolevi picks lego 1
Example 2
Input:
2
10 2
1 1000

Output:
-1
 */

using namespace std;

const int INF = numeric_limits<int>::max();

template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<T> &obj) {
    bool first = true;
    for (const auto &o : obj) {
        if (first) {
            cout << o;
            first = false;
        } else {
            cout << ' ' << o;
        }
    }
    cout << endl;
    return os;
}

std::ostream &operator<<(std::ostream &os, const vector<pair<int, int>> &obj) {
    for (const auto &o : obj) {
        cout << o.first << ' ' << o.second << endl;
    }
    cout << endl;
    return os;
}

// vector<int> U;
// vector<int> M;
// vector<int> x_y;

int main(int argc, char **argv) {
    int n;
    cin >> n;

    // U = vector<int>(n);
    // M = vector<int>(n);
    // x_y = vector<int>(n);

    auto UM = vector<pair<int, int>>(n);

    for (int i = 0; i < n; ++i) {
        cin >> UM[i].first;
        cin >> UM[i].second;
        // cin >> U[i];
        // cin >> M[i];
        // x_y[i] = U[i] - M[i];
    }

    // sort(U.begin(), U.end());
    // sort(M.begin(), M.end());
    // sort(x_y.begin(), x_y.end());
    sort(UM.begin(), UM.end(), [](pair<int, int> a, pair<int, int> b) {
        return abs(a.first - a.second) > abs(b.first - b.second);
    });

    cout << endl;
    cout << "UM: " << endl << UM << endl;
    // cout << x_y << endl;
    // int m = 0, u = x_y.size() - 1;
    char turn{'U'};
    int x_y = 0;
    for (int i = 0; i < UM.size(); ++i) {

        if (turn == 'U') {
            x_y += UM[i].first;
            turn = 'M';
        } else {
            x_y -= UM[i].second;
            turn = 'U';
        }
    }
    // while () {
    //     switch (turn) {
    //     case 'U':
    //         choice = x_y[u--];
    //         d += choice;
    //         break;
    //     case 'M':
    //         choice = x_y[m++];
    //         d -= choice;
    //         break;
    //     }
    //     cout << choice << endl;
    // }
    cout << x_y << endl;
}

Test details

Test 1

Verdict:

input
1
9 2

correct output
9

user output

UM: 
9 2


...

Test 2

Verdict:

input
1
10 4

correct output
10

user output

UM: 
10 4


...

Test 3

Verdict:

input
2
8 1
3 5

correct output
3

user output

UM: 
8 1
3 5

...

Test 4

Verdict:

input
2
2 9
3 10

correct output
-6

user output

UM: 
2 9
3 10

...

Test 5

Verdict:

input
3
7 2
6 2
2 3

correct output
7

user output

UM: 
7 2
6 2
2 3
...

Test 6

Verdict:

input
3
3 2
7 9
3 8

correct output
2

user output

UM: 
3 8
7 9
3 2
...

Test 7

Verdict:

input
4
5 10
9 9
1 10
8 8

correct output
-4

user output

UM: 
1 10
5 10
9 9
...

Test 8

Verdict:

input
4
3 2
7 9
3 8
4 6

correct output
1

user output

UM: 
3 8
7 9
4 6
...

Test 9

Verdict:

input
4
5 3
8 1
9 1
3 3

correct output
10

user output

UM: 
9 1
8 1
5 3
...

Test 10

Verdict:

input
5
3 9
3 6
5 2
2 5
...

correct output
5

user output

UM: 
9 1
3 9
3 6
...

Test 11

Verdict:

input
5
10 8
10 1
2 4
10 2
...

correct output
17

user output

UM: 
10 1
10 2
10 8
...

Test 12

Verdict:

input
5
2 1
10 6
10 5
5 5
...

correct output
8

user output

UM: 
10 5
10 6
2 1
...

Test 13

Verdict:

input
5
8 9
2 6
3 5
1 1
...

correct output
3

user output

UM: 
2 6
3 5
8 9
...

Test 14

Verdict:

input
5
6 1
9 3
3 6
2 10
...

correct output
1

user output

UM: 
2 10
9 3
1 7
...

Test 15

Verdict:

input
5
1 9
9 3
4 10
10 5
...

correct output
1

user output

UM: 
1 9
9 3
4 10
...

Test 16

Verdict:

input
5
4 1
1 1
1 10
1 6
...

correct output
-4

user output

UM: 
1 10
1 6
2 6
...

Test 17

Verdict:

input
5
3 8
4 5
10 8
5 10
...

correct output
1

user output

UM: 
3 8
5 10
10 8
...

Test 18

Verdict:

input
5
10 3
4 2
3 2
7 5
...

correct output
11

user output

UM: 
10 3
5 2
4 2
...

Test 19

Verdict:

input
5
4 6
5 5
1 2
4 2
...

correct output
1

user output

UM: 
4 6
4 2
1 3
...

Test 20

Verdict:

input
10
3 9
3 6
5 2
2 5
...

correct output
-5

user output

UM: 
9 1
3 9
3 9
...

Test 21

Verdict:

input
10
10 8
10 1
2 4
10 2
...

correct output
20

user output

UM: 
10 1
10 2
10 6
...

Test 22

Verdict:

input
11
198730372 27838076
590195590 467423861
520495379 451366491
344173378 354694313
...

correct output
1075637330

user output

UM: 
733393578 144504113
878564254 568161994
520953672 286503607
...
Truncated

Test 23

Verdict:

input
10
8 9
2 6
3 5
1 1
...

correct output
-3

user output

UM: 
2 7
2 6
1 5
...

Test 24

Verdict:

input
10
6 1
9 3
3 6
2 10
...

correct output
-2

user output

UM: 
2 10
9 3
1 7
...

Test 25

Verdict:

input
11
59249204 934941692
892631472 221963002
390559518 986350949
524427523 96444602
...

correct output
1389122128

user output

UM: 
59249204 934941692
867885853 86695278
892631472 221963002
...
Truncated

Test 26

Verdict:

input
10
4 1
1 1
1 10
1 6
...

correct output
-6

user output

UM: 
1 10
3 10
1 6
...

Test 27

Verdict:

input
11
244103474 837431431
342493822 470738321
776814822 489180570
330726191 578205540
...

correct output
-124259424

user output

UM: 
244103474 837431431
17083619 536744746
776814822 489180570
...
Truncated

Test 28

Verdict:

input
10
10 3
4 2
3 2
7 5
...

correct output
4

user output

UM: 
10 3
3 9
6 1
...

Test 29

Verdict:

input
11
391337048 538883744
535937150 532332526
8099343 143698367
339543270 152590624
...

correct output
246913369

user output

UM: 
338989816 943345633
941909102 449369743
531131241 179074744
...
Truncated

Test 30

Verdict:

input
101
3 906523441
3 585063857
454895875 2
2 469855690
...

correct output
-1950121670

user output

UM: 
2 984323780
2 969157730
2 968889409
...
Truncated

Test 31

Verdict:

input
100
773442532 122816
137572579 324627123
157577940 253498609
99147813 425825313
...

correct output
2484533534

user output

UM: 
9672262 951273082
63611899 942898335
890779831 41934756
...
Truncated

Test 32

Verdict:

input
100
198730372 27838076
590195590 467423861
520495379 451366491
344173378 354694313
...

correct output
1162250085

user output

UM: 
943067755 27435119
818116107 49047416
782219311 13977261
...
Truncated

Test 33

Verdict:

input
100
760367935 901888417
130275571 548496961
3 469291685
20130523 1
...

correct output
-513884705

user output

UM: 
3 987692601
2 985727231
2 935306493
...
Truncated

Test 34

Verdict:

input
100
587586158 1
918715995 3
3 641621091
151896000 241061404
...

correct output
4449680753

user output

UM: 
997942797 2
2 995058761
971379429 2
...
Truncated

Test 35

Verdict:

input
100
59249204 934941692
892631472 221963002
390559518 986350949
524427523 96444602
...

correct output
2289597915

user output

UM: 
993902126 6157975
59249204 934941692
956369237 91380830
...
Truncated

Test 36

Verdict:

input
101
356460601 1
68992860 1
1 638932295
568887059 653343572
...

correct output
-1011275811

user output

UM: 
981609831 3
3 976563375
2 950545065
...
Truncated

Test 37

Verdict:

input
100
244103474 837431431
342493822 470738321
776814822 489180570
330726191 578205540
...

correct output
-3347612884

user output

UM: 
44638669 881786430
976169154 142989603
1532101 716465796
...
Truncated

Test 38

Verdict:

input
100
257096283 933290530
2 876668629
453495728 12239373
2 822553808
...

correct output
-5234322969

user output

UM: 
2 962228374
2 883455897
2 879325384
...
Truncated

Test 39

Verdict:

input
101
391337048 538883744
535937150 532332526
8099343 143698367
339543270 152590624
...

correct output
1057263569

user output

UM: 
973107823 50143781
966689311 120842251
872396361 40769142
...
Truncated

Test 40

Verdict:

input
200
3 906523441
3 585063857
454895875 2
2 469855690
...

correct output
-1859110273

user output

UM: 
2 984323780
2 969157730
2 968889409
...
Truncated

Test 41

Verdict:

input
201
773442532 122816
137572579 324627123
157577940 253498609
99147813 425825313
...

correct output
-1706556434

user output

UM: 
983124839 8592846
9672262 951273082
47837212 983987609
...
Truncated

Test 42

Verdict:

input
200
198730372 27838076
590195590 467423861
520495379 451366491
344173378 354694313
...

correct output
2881192575

user output

UM: 
977808916 56073724
943067755 27435119
32607663 940883942
...
Truncated

Test 43

Verdict:

input
200
760367935 901888417
130275571 548496961
3 469291685
20130523 1
...

correct output
367410203

user output

UM: 
3 987692601
2 985727231
970676003 1
...
Truncated

Test 44

Verdict:

input
201
587586158 1
918715995 3
3 641621091
151896000 241061404
...

correct output
6064184122

user output

UM: 
997942797 2
2 995058761
975840714 2
...
Truncated

Test 45

Verdict:

input
200
59249204 934941692
892631472 221963002
390559518 986350949
524427523 96444602
...

correct output
-541796892

user output

UM: 
993902126 6157975
905823204 13795513
81006463 959670342
...
Truncated

Test 46

Verdict:

input
200
356460601 1
68992860 1
1 638932295
568887059 653343572
...

correct output
-3818748427

user output

UM: 
3 993060037
1 990332859
981609831 3
...
Truncated

Test 47

Verdict:

input
201
244103474 837431431
342493822 470738321
776814822 489180570
330726191 578205540
...

correct output
1128073230

user output

UM: 
979953364 25665676
990637599 55576422
882282840 8822402
...
Truncated

Test 48

Verdict:

input
200
257096283 933290530
2 876668629
453495728 12239373
2 822553808
...

correct output
-4097764173

user output

UM: 
999884963 2
998664436 3
998224343 2
...
Truncated

Test 49

Verdict:

input
201
391337048 538883744
535937150 532332526
8099343 143698367
339543270 152590624
...

correct output
4209206317

user output

UM: 
965285179 366794
971387757 47481893
973107823 50143781
...
Truncated

Test 50

Verdict:

input
1001
3 906523441
3 585063857
454895875 2
2 469855690
...

correct output
797530744

user output

UM: 
999521604 3
2 999458991
999039870 3
...
Truncated

Test 51

Verdict:

input
1000
773442532 122816
137572579 324627123
157577940 253498609
99147813 425825313
...

correct output
-6859401550

user output

UM: 
983124839 8592846
985156296 32641422
9672262 951273082
...
Truncated

Test 52

Verdict:

input
1000
198730372 27838076
590195590 467423861
520495379 451366491
344173378 354694313
...

correct output
3341433705

user output

UM: 
997881081 25710421
11287375 960974035
969906864 31977910
...
Truncated

Test 53

Verdict:

input
1000
760367935 901888417
130275571 548496961
3 469291685
20130523 1
...

correct output
3932941646

user output

UM: 
2 999247241
1 998231067
2 994295174
...
Truncated

Test 54

Verdict:

input
1000
587586158 1
918715995 3
3 641621091
151896000 241061404
...

correct output
12940904658

user output

UM: 
997942797 2
2 995058761
994517055 2
...
Truncated

Test 55

Verdict:

input
1000
59249204 934941692
892631472 221963002
390559518 986350949
524427523 96444602
...

correct output
-11353638361

user output

UM: 
993902126 6157975
976644788 1259454
971587116 14254335
...
Truncated

Test 56

Verdict:

input
1001
356460601 1
68992860 1
1 638932295
568887059 653343572
...

correct output
196162653

user output

UM: 
3 996941911
1 996759120
995054179 3
...
Truncated

Test 57

Verdict:

input
1000
244103474 837431431
342493822 470738321
776814822 489180570
330726191 578205540
...

correct output
8993059628

user output

UM: 
979953364 25665676
961102593 24477307
990637599 55576422
...
Truncated

Test 58

Verdict:

input
1000
257096283 933290530
2 876668629
453495728 12239373
2 822553808
...

correct output
-11284740290

user output

UM: 
999884963 2
998664436 3
3 998655833
...
Truncated

Test 59

Verdict:

input
1001
391337048 538883744
535937150 532332526
8099343 143698367
339543270 152590624
...

correct output
12730443353

user output

UM: 
996882955 7869707
987526072 14743974
965285179 366794
...
Truncated

Test 60

Verdict:

input
100000
3 906523441
3 585063857
454895875 2
2 469855690
...

correct output
105607728400

user output

UM: 
1 999993709
2 999979181
999934352 2
...
Truncated

Test 61

Verdict:

input
100000
773442532 122816
137572579 324627123
157577940 253498609
99147813 425825313
...

correct output
21174476635

user output

UM: 
997335746 1158469
999334215 5013809
2483692 995540008
...
Truncated

Test 62

Verdict:

input
100000
198730372 27838076
590195590 467423861
520495379 451366491
344173378 354694313
...

correct output
65624350916

user output

UM: 
997197588 2363681
999743804 5388557
2028311 996145502
...
Truncated

Test 63

Verdict:

input
100000
760367935 901888417
130275571 548496961
3 469291685
20130523 1
...

correct output
66836037029

user output

UM: 
3 999993592
3 999993295
3 999989245
...
Truncated

Test 64

Verdict:

input
100000
587586158 1
918715995 3
3 641621091
151896000 241061404
...

correct output
-87105533715

user output

UM: 
1 999995101
999988223 3
3 999963162
...
Truncated

Test 65

Verdict:

input
100000
59249204 934941692
892631472 221963002
390559518 986350949
524427523 96444602
...

correct output
-1093858395

user output

UM: 
1277537 999470353
1305166 999189404
126801 997024236
...
Truncated

Test 66

Verdict:

input
99999
356460601 1
68992860 1
1 638932295
568887059 653343572
...

correct output
-28178672820

user output

UM: 
999987233 3
1 999986133
999960823 3
...
Truncated

Test 67

Verdict:

input
99999
244103474 837431431
342493822 470738321
776814822 489180570
330726191 578205540
...

correct output
72715249868

user output

UM: 
618493 999791252
998483119 1911773
996656310 1988040
...
Truncated

Test 68

Verdict:

input
99999
257096283 933290530
2 876668629
453495728 12239373
2 822553808
...

correct output
-46790665125

user output

UM: 
1 999991195
2 999972019
3 999955141
...
Truncated

Test 69

Verdict:

input
99999
391337048 538883744
535937150 532332526
8099343 143698367
339543270 152590624
...

correct output
12190306919

user output

UM: 
1695388 996474706
1640878 996085730
6339656 996640748
...
Truncated