Submission #404388

# Submission time Handle Problem Language Result Execution time Memory
404388 2021-05-14T10:15:44 Z fvogel499 Bootfall (IZhO17_bootfall) C++14
100 / 100
642 ms 31812 KB
// analysis by Francois Vogel

// first we check if when Tina stands out, two teams can be created using knapsack DP over bitset. if no : ouput 0 and return. if yes : proceed to the next step

// for every possible configuration of a player "recording", we find all possible values of Tima's strength using knapsack DP over bitset.
// to be a final Tima's possible strength, this strength must be possible for EVERY possible configuration, meaning we can just do a bitwise AND operation

// to optimize, i created startToEnd and endToStart arrays so that when calculating the knapsack in the loop, I actually only traverse at most half of the values

#include <iostream>
#include <bitset>

using namespace std;

const int siz = 250001; // consider something like n = 500 (maximum val) and ai = 500 (max val) for every i, then the biggest possible value for Tima's strength would be at most 500*500 because if it is more then Tima's strength will be bigger than the sum of the strength of all the other players, and thus no team can be created

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    int n;
    cin >> n;

    int raw [n];
    for (int i = 0; i < n; i++) cin >> raw[i];

    bitset<siz> check(0);
    check[0] = 1;
    int checkSum = 0;
    for (int i = 0; i < n; i++) {
        check |= check<<raw[i];
        checkSum += raw[i];
    }
    if (checkSum%2 == 1 or check[checkSum/2] == 0) {
        cout << 0;
        return 0;
    }

    bitset<siz> startToEnd [n];
    startToEnd[0] = bitset<siz>(0);
    startToEnd[0][0] = 1;
    for (int i = 1; i < n; i++) {
        startToEnd[i] = startToEnd[i-1]|(startToEnd[i-1]<<raw[i-1]);
    }

    bitset<siz> endToStart [n];
    endToStart[n-1] = bitset<siz>(0);
    endToStart[n-1][0] = 1;
    for (int i = n-2; i >= 0; i--) {
        endToStart[i] = endToStart[i+1]|(endToStart[i+1]<<raw[i+1]);
    }

    bitset<siz> even;
    bitset<siz> uneven;
    even.set();
    uneven.set();
    even[0] = 0; // 0 is not a possible value for Tima's strength
    bool evenKilled = false;
    bool unevenKilled = false;
    bitset<siz> cur;
    int locSum;
    for (int i = 0; i < n; i++) {
        if (i > n/2) {
            cur = startToEnd[i];
            for (int j = i+1; j < n; j++) cur |= cur<<raw[j];
        }
        else {
            cur = endToStart[i];
            for (int j = i-1; j >= 0; j--) cur |= cur<<raw[j];
        }
        locSum = checkSum-raw[i];
        // let's say j is a possible tina's strength
        // then locSum+j % 2 == 0, so either only even values or only uneven values work
        if (locSum%2 == 0) {
            even &= cur>>(locSum/2); // if Tima's strength is 0 (virtual value as this case is actually not possible), it only works if cur[(locSum+0)/2] = 1 <--> cur[locSum] = 1, so we can simply bitshift
            if (!unevenKilled) { // unevenKilled simply tells us that it is already impossible that uneven values work so that we don't have to set them all to 0 again
                uneven &= 0; // set all bits to 0
                unevenKilled = true;
            }
        }
        else {
            uneven &= cur>>(locSum/2+1); // same logic as for locSum%2 == 0 case just that since integer division is performed and the first value taken into consideration for uneven values is 1, we must add 1 to the shift
            if (!evenKilled) { // evenKilled simply tells us that it is already impossible that uneven values work so that we don't have to set them all to 0 again
                even &= 0; // set all bits to 0
                evenKilled = true;
            }
        }
    }

    cout << even.count()+uneven.count() << endl;
    for (int i = 0; i < siz; i++) {
        if (even[i]) cout << i*2 << " ";
        if (uneven[i]) cout << i*2+1 << " ";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 844 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 1100 KB Output is correct
6 Correct 3 ms 972 KB Output is correct
7 Correct 2 ms 844 KB Output is correct
8 Correct 2 ms 1228 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 844 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 1100 KB Output is correct
6 Correct 3 ms 972 KB Output is correct
7 Correct 2 ms 844 KB Output is correct
8 Correct 2 ms 1228 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 5 ms 2252 KB Output is correct
11 Correct 5 ms 2252 KB Output is correct
12 Correct 5 ms 2252 KB Output is correct
13 Correct 4 ms 2076 KB Output is correct
14 Correct 5 ms 2200 KB Output is correct
15 Correct 5 ms 2124 KB Output is correct
16 Correct 6 ms 2324 KB Output is correct
17 Correct 3 ms 1484 KB Output is correct
18 Correct 5 ms 1868 KB Output is correct
19 Correct 4 ms 1996 KB Output is correct
20 Correct 7 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 844 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 1100 KB Output is correct
6 Correct 3 ms 972 KB Output is correct
7 Correct 2 ms 844 KB Output is correct
8 Correct 2 ms 1228 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 5 ms 2252 KB Output is correct
11 Correct 5 ms 2252 KB Output is correct
12 Correct 5 ms 2252 KB Output is correct
13 Correct 4 ms 2076 KB Output is correct
14 Correct 5 ms 2200 KB Output is correct
15 Correct 5 ms 2124 KB Output is correct
16 Correct 6 ms 2324 KB Output is correct
17 Correct 3 ms 1484 KB Output is correct
18 Correct 5 ms 1868 KB Output is correct
19 Correct 4 ms 1996 KB Output is correct
20 Correct 7 ms 2252 KB Output is correct
21 Correct 15 ms 4404 KB Output is correct
22 Correct 19 ms 5016 KB Output is correct
23 Correct 10 ms 3548 KB Output is correct
24 Correct 27 ms 6228 KB Output is correct
25 Correct 28 ms 6216 KB Output is correct
26 Correct 30 ms 6596 KB Output is correct
27 Correct 50 ms 6596 KB Output is correct
28 Correct 36 ms 6584 KB Output is correct
29 Correct 37 ms 6596 KB Output is correct
30 Correct 30 ms 6608 KB Output is correct
31 Correct 30 ms 6588 KB Output is correct
32 Correct 30 ms 6596 KB Output is correct
33 Correct 30 ms 6488 KB Output is correct
34 Correct 30 ms 6600 KB Output is correct
35 Correct 31 ms 6476 KB Output is correct
36 Correct 53 ms 6596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 844 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 1100 KB Output is correct
6 Correct 3 ms 972 KB Output is correct
7 Correct 2 ms 844 KB Output is correct
8 Correct 2 ms 1228 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 5 ms 2252 KB Output is correct
11 Correct 5 ms 2252 KB Output is correct
12 Correct 5 ms 2252 KB Output is correct
13 Correct 4 ms 2076 KB Output is correct
14 Correct 5 ms 2200 KB Output is correct
15 Correct 5 ms 2124 KB Output is correct
16 Correct 6 ms 2324 KB Output is correct
17 Correct 3 ms 1484 KB Output is correct
18 Correct 5 ms 1868 KB Output is correct
19 Correct 4 ms 1996 KB Output is correct
20 Correct 7 ms 2252 KB Output is correct
21 Correct 15 ms 4404 KB Output is correct
22 Correct 19 ms 5016 KB Output is correct
23 Correct 10 ms 3548 KB Output is correct
24 Correct 27 ms 6228 KB Output is correct
25 Correct 28 ms 6216 KB Output is correct
26 Correct 30 ms 6596 KB Output is correct
27 Correct 50 ms 6596 KB Output is correct
28 Correct 36 ms 6584 KB Output is correct
29 Correct 37 ms 6596 KB Output is correct
30 Correct 30 ms 6608 KB Output is correct
31 Correct 30 ms 6588 KB Output is correct
32 Correct 30 ms 6596 KB Output is correct
33 Correct 30 ms 6488 KB Output is correct
34 Correct 30 ms 6600 KB Output is correct
35 Correct 31 ms 6476 KB Output is correct
36 Correct 53 ms 6596 KB Output is correct
37 Correct 117 ms 12700 KB Output is correct
38 Correct 108 ms 12716 KB Output is correct
39 Correct 167 ms 15636 KB Output is correct
40 Correct 190 ms 16984 KB Output is correct
41 Correct 181 ms 16972 KB Output is correct
42 Correct 159 ms 15760 KB Output is correct
43 Correct 154 ms 15520 KB Output is correct
44 Correct 158 ms 15684 KB Output is correct
45 Correct 159 ms 15896 KB Output is correct
46 Correct 184 ms 16988 KB Output is correct
47 Correct 172 ms 15804 KB Output is correct
48 Correct 156 ms 15812 KB Output is correct
49 Correct 156 ms 15744 KB Output is correct
50 Correct 184 ms 15756 KB Output is correct
51 Correct 5 ms 460 KB Output is correct
52 Correct 181 ms 17056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 844 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 1100 KB Output is correct
6 Correct 3 ms 972 KB Output is correct
7 Correct 2 ms 844 KB Output is correct
8 Correct 2 ms 1228 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 5 ms 2252 KB Output is correct
11 Correct 5 ms 2252 KB Output is correct
12 Correct 5 ms 2252 KB Output is correct
13 Correct 4 ms 2076 KB Output is correct
14 Correct 5 ms 2200 KB Output is correct
15 Correct 5 ms 2124 KB Output is correct
16 Correct 6 ms 2324 KB Output is correct
17 Correct 3 ms 1484 KB Output is correct
18 Correct 5 ms 1868 KB Output is correct
19 Correct 4 ms 1996 KB Output is correct
20 Correct 7 ms 2252 KB Output is correct
21 Correct 15 ms 4404 KB Output is correct
22 Correct 19 ms 5016 KB Output is correct
23 Correct 10 ms 3548 KB Output is correct
24 Correct 27 ms 6228 KB Output is correct
25 Correct 28 ms 6216 KB Output is correct
26 Correct 30 ms 6596 KB Output is correct
27 Correct 50 ms 6596 KB Output is correct
28 Correct 36 ms 6584 KB Output is correct
29 Correct 37 ms 6596 KB Output is correct
30 Correct 30 ms 6608 KB Output is correct
31 Correct 30 ms 6588 KB Output is correct
32 Correct 30 ms 6596 KB Output is correct
33 Correct 30 ms 6488 KB Output is correct
34 Correct 30 ms 6600 KB Output is correct
35 Correct 31 ms 6476 KB Output is correct
36 Correct 53 ms 6596 KB Output is correct
37 Correct 117 ms 12700 KB Output is correct
38 Correct 108 ms 12716 KB Output is correct
39 Correct 167 ms 15636 KB Output is correct
40 Correct 190 ms 16984 KB Output is correct
41 Correct 181 ms 16972 KB Output is correct
42 Correct 159 ms 15760 KB Output is correct
43 Correct 154 ms 15520 KB Output is correct
44 Correct 158 ms 15684 KB Output is correct
45 Correct 159 ms 15896 KB Output is correct
46 Correct 184 ms 16988 KB Output is correct
47 Correct 172 ms 15804 KB Output is correct
48 Correct 156 ms 15812 KB Output is correct
49 Correct 156 ms 15744 KB Output is correct
50 Correct 184 ms 15756 KB Output is correct
51 Correct 5 ms 460 KB Output is correct
52 Correct 181 ms 17056 KB Output is correct
53 Correct 241 ms 18948 KB Output is correct
54 Correct 308 ms 22008 KB Output is correct
55 Correct 303 ms 22132 KB Output is correct
56 Correct 236 ms 19308 KB Output is correct
57 Correct 328 ms 22132 KB Output is correct
58 Correct 333 ms 21956 KB Output is correct
59 Correct 321 ms 22004 KB Output is correct
60 Correct 4 ms 460 KB Output is correct
61 Correct 300 ms 22004 KB Output is correct
62 Correct 255 ms 20204 KB Output is correct
63 Correct 267 ms 20296 KB Output is correct
64 Correct 255 ms 19928 KB Output is correct
65 Correct 242 ms 19796 KB Output is correct
66 Correct 239 ms 19440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 844 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 1100 KB Output is correct
6 Correct 3 ms 972 KB Output is correct
7 Correct 2 ms 844 KB Output is correct
8 Correct 2 ms 1228 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 5 ms 2252 KB Output is correct
11 Correct 5 ms 2252 KB Output is correct
12 Correct 5 ms 2252 KB Output is correct
13 Correct 4 ms 2076 KB Output is correct
14 Correct 5 ms 2200 KB Output is correct
15 Correct 5 ms 2124 KB Output is correct
16 Correct 6 ms 2324 KB Output is correct
17 Correct 3 ms 1484 KB Output is correct
18 Correct 5 ms 1868 KB Output is correct
19 Correct 4 ms 1996 KB Output is correct
20 Correct 7 ms 2252 KB Output is correct
21 Correct 15 ms 4404 KB Output is correct
22 Correct 19 ms 5016 KB Output is correct
23 Correct 10 ms 3548 KB Output is correct
24 Correct 27 ms 6228 KB Output is correct
25 Correct 28 ms 6216 KB Output is correct
26 Correct 30 ms 6596 KB Output is correct
27 Correct 50 ms 6596 KB Output is correct
28 Correct 36 ms 6584 KB Output is correct
29 Correct 37 ms 6596 KB Output is correct
30 Correct 30 ms 6608 KB Output is correct
31 Correct 30 ms 6588 KB Output is correct
32 Correct 30 ms 6596 KB Output is correct
33 Correct 30 ms 6488 KB Output is correct
34 Correct 30 ms 6600 KB Output is correct
35 Correct 31 ms 6476 KB Output is correct
36 Correct 53 ms 6596 KB Output is correct
37 Correct 117 ms 12700 KB Output is correct
38 Correct 108 ms 12716 KB Output is correct
39 Correct 167 ms 15636 KB Output is correct
40 Correct 190 ms 16984 KB Output is correct
41 Correct 181 ms 16972 KB Output is correct
42 Correct 159 ms 15760 KB Output is correct
43 Correct 154 ms 15520 KB Output is correct
44 Correct 158 ms 15684 KB Output is correct
45 Correct 159 ms 15896 KB Output is correct
46 Correct 184 ms 16988 KB Output is correct
47 Correct 172 ms 15804 KB Output is correct
48 Correct 156 ms 15812 KB Output is correct
49 Correct 156 ms 15744 KB Output is correct
50 Correct 184 ms 15756 KB Output is correct
51 Correct 5 ms 460 KB Output is correct
52 Correct 181 ms 17056 KB Output is correct
53 Correct 241 ms 18948 KB Output is correct
54 Correct 308 ms 22008 KB Output is correct
55 Correct 303 ms 22132 KB Output is correct
56 Correct 236 ms 19308 KB Output is correct
57 Correct 328 ms 22132 KB Output is correct
58 Correct 333 ms 21956 KB Output is correct
59 Correct 321 ms 22004 KB Output is correct
60 Correct 4 ms 460 KB Output is correct
61 Correct 300 ms 22004 KB Output is correct
62 Correct 255 ms 20204 KB Output is correct
63 Correct 267 ms 20296 KB Output is correct
64 Correct 255 ms 19928 KB Output is correct
65 Correct 242 ms 19796 KB Output is correct
66 Correct 239 ms 19440 KB Output is correct
67 Correct 384 ms 25064 KB Output is correct
68 Correct 5 ms 488 KB Output is correct
69 Correct 642 ms 31700 KB Output is correct
70 Correct 608 ms 31432 KB Output is correct
71 Correct 615 ms 31436 KB Output is correct
72 Correct 576 ms 31168 KB Output is correct
73 Correct 577 ms 30996 KB Output is correct
74 Correct 582 ms 31568 KB Output is correct
75 Correct 585 ms 31448 KB Output is correct
76 Correct 590 ms 31700 KB Output is correct
77 Correct 585 ms 31812 KB Output is correct