Submission #110100

# Submission time Handle Problem Language Result Execution time Memory
110100 2019-05-09T13:02:37 Z popovicirobert Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 36492 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

int dp[2][1 << 20];
string str;

void bktQ(int p, vector <int> &bit, int cnf, int &ans) {
    if(p == bit.size()) {
        ans += str[cnf] - '0';
    }
    else {
        bktQ(p + 1, bit, cnf, ans);
        bktQ(p + 1, bit, cnf + (1 << bit[p]), ans);
    }
}

void bkt(int p, vector <int> &bit, int cnf, int &ans, int cnt, bool t) {
    if(p == bit.size()) {
        if(cnt & 1) {
            ans -= dp[t][cnf];
        }
        else {
            ans += dp[t][cnf];
        }
    }
    else {
        bkt(p + 1, bit, cnf, ans, cnt, t);
        bkt(p + 1, bit, cnf ^ (1 << bit[p]), ans, cnt + 1, t);
    }

}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, l, q;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> l >> q >> str;

    for(int conf = 0; conf < (1 << l); conf++) {
        dp[0][conf ^ ((1 << l) - 1)] = str[conf] - '0';
        dp[1][conf] = str[conf] - '0';
    }

    for(i = 0; i < 2; i++) {
        for(int bit = 0; bit < l; bit++) {
            for(int conf = 0; conf < (1 << l); conf++) {
                if(conf & (1 << bit)) {
                    dp[i][conf] += dp[i][conf ^ (1 << bit)];
                }
            }
        }
    }

    while(q > 0) {
        q--;

        string cur;
        cin >> cur;
        reverse(cur.begin(), cur.end());

        int numq = 0, num0 = 0, num1 = 0;
        int val = 0;
        for(i = 0; i < l; i++) {
            if(cur[i] == '0') {
                num0++;
            }
            if(cur[i] == '1') {
                num1++;
            }
            if(cur[i] == '?') {
                numq++;
            }
        }

        int mn = min(numq, min(num0, num1));
        vector <int> bit;
        int ans = 0, cnf = 0;

        if(numq == mn) {

            for(i = 0; i < l; i++) {
                if(cur[i] == '?') {
                    bit.push_back(i);
                }
                else {
                    cnf += (1 << i) * (cur[i] == '1');
                }
            }

            bktQ(0, bit, cnf, ans);
        }
        else if(num0 == mn) {

            for(i = 0; i < l; i++) {
                if(cur[i] == '0') {
                    bit.push_back(i);
                }
                if(cur[i] != '1') {
                    cnf += (1 << i);
                }
            }

            bkt(0, bit, cnf, ans, 0, 0);
        }
        else if(num1 == mn) {

             for(i = 0; i < l; i++) {
                if(cur[i] == '1') {
                    bit.push_back(i);
                }
                if(cur[i] != '0') {
                    cnf += (1 << i);
                }
            }

            bkt(0, bit, cnf, ans, 0, 1);
        }

        cout << ans << "\n";

    }

    //cin.close();
    //cout.close();
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'void bktQ(int, std::vector<int>&, int, int&)':
snake_escaping.cpp:14:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(p == bit.size()) {
        ~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'void bkt(int, std::vector<int>&, int, int&, int, bool)':
snake_escaping.cpp:24:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(p == bit.size()) {
        ~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:71:13: warning: unused variable 'val' [-Wunused-variable]
         int val = 0;
             ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 383 ms 4700 KB Output is correct
12 Correct 366 ms 4496 KB Output is correct
13 Correct 483 ms 3728 KB Output is correct
14 Correct 535 ms 3864 KB Output is correct
15 Correct 431 ms 4728 KB Output is correct
16 Correct 581 ms 4056 KB Output is correct
17 Correct 449 ms 3832 KB Output is correct
18 Correct 220 ms 5852 KB Output is correct
19 Correct 241 ms 2872 KB Output is correct
20 Correct 375 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 383 ms 4700 KB Output is correct
12 Correct 366 ms 4496 KB Output is correct
13 Correct 483 ms 3728 KB Output is correct
14 Correct 535 ms 3864 KB Output is correct
15 Correct 431 ms 4728 KB Output is correct
16 Correct 581 ms 4056 KB Output is correct
17 Correct 449 ms 3832 KB Output is correct
18 Correct 220 ms 5852 KB Output is correct
19 Correct 241 ms 2872 KB Output is correct
20 Correct 375 ms 4472 KB Output is correct
21 Correct 356 ms 4944 KB Output is correct
22 Correct 416 ms 5008 KB Output is correct
23 Correct 522 ms 3900 KB Output is correct
24 Correct 627 ms 3804 KB Output is correct
25 Correct 474 ms 5632 KB Output is correct
26 Correct 630 ms 4216 KB Output is correct
27 Correct 579 ms 4216 KB Output is correct
28 Correct 247 ms 6736 KB Output is correct
29 Correct 257 ms 2720 KB Output is correct
30 Correct 437 ms 4968 KB Output is correct
31 Correct 580 ms 4816 KB Output is correct
32 Correct 682 ms 4836 KB Output is correct
33 Correct 449 ms 3588 KB Output is correct
34 Correct 593 ms 3832 KB Output is correct
35 Correct 645 ms 4088 KB Output is correct
36 Correct 285 ms 2680 KB Output is correct
37 Correct 392 ms 4984 KB Output is correct
38 Correct 275 ms 2808 KB Output is correct
39 Correct 588 ms 3952 KB Output is correct
40 Correct 648 ms 3984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 100 ms 10492 KB Output is correct
12 Correct 91 ms 12072 KB Output is correct
13 Correct 121 ms 12048 KB Output is correct
14 Correct 202 ms 12052 KB Output is correct
15 Correct 123 ms 12056 KB Output is correct
16 Correct 189 ms 11992 KB Output is correct
17 Correct 155 ms 11928 KB Output is correct
18 Correct 79 ms 12148 KB Output is correct
19 Correct 101 ms 11928 KB Output is correct
20 Correct 100 ms 12024 KB Output is correct
21 Correct 149 ms 12056 KB Output is correct
22 Correct 220 ms 12056 KB Output is correct
23 Correct 108 ms 12128 KB Output is correct
24 Correct 143 ms 11928 KB Output is correct
25 Correct 148 ms 12056 KB Output is correct
26 Correct 103 ms 11932 KB Output is correct
27 Correct 92 ms 12056 KB Output is correct
28 Correct 83 ms 11916 KB Output is correct
29 Correct 129 ms 11892 KB Output is correct
30 Correct 118 ms 11960 KB Output is correct
31 Correct 88 ms 11932 KB Output is correct
32 Correct 163 ms 12056 KB Output is correct
33 Correct 133 ms 11916 KB Output is correct
34 Correct 74 ms 11920 KB Output is correct
35 Correct 137 ms 11964 KB Output is correct
36 Correct 133 ms 12056 KB Output is correct
37 Correct 127 ms 12060 KB Output is correct
38 Correct 149 ms 11928 KB Output is correct
39 Correct 137 ms 12180 KB Output is correct
40 Correct 121 ms 11928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 383 ms 4700 KB Output is correct
12 Correct 366 ms 4496 KB Output is correct
13 Correct 483 ms 3728 KB Output is correct
14 Correct 535 ms 3864 KB Output is correct
15 Correct 431 ms 4728 KB Output is correct
16 Correct 581 ms 4056 KB Output is correct
17 Correct 449 ms 3832 KB Output is correct
18 Correct 220 ms 5852 KB Output is correct
19 Correct 241 ms 2872 KB Output is correct
20 Correct 375 ms 4472 KB Output is correct
21 Correct 356 ms 4944 KB Output is correct
22 Correct 416 ms 5008 KB Output is correct
23 Correct 522 ms 3900 KB Output is correct
24 Correct 627 ms 3804 KB Output is correct
25 Correct 474 ms 5632 KB Output is correct
26 Correct 630 ms 4216 KB Output is correct
27 Correct 579 ms 4216 KB Output is correct
28 Correct 247 ms 6736 KB Output is correct
29 Correct 257 ms 2720 KB Output is correct
30 Correct 437 ms 4968 KB Output is correct
31 Correct 580 ms 4816 KB Output is correct
32 Correct 682 ms 4836 KB Output is correct
33 Correct 449 ms 3588 KB Output is correct
34 Correct 593 ms 3832 KB Output is correct
35 Correct 645 ms 4088 KB Output is correct
36 Correct 285 ms 2680 KB Output is correct
37 Correct 392 ms 4984 KB Output is correct
38 Correct 275 ms 2808 KB Output is correct
39 Correct 588 ms 3952 KB Output is correct
40 Correct 648 ms 3984 KB Output is correct
41 Correct 100 ms 10492 KB Output is correct
42 Correct 91 ms 12072 KB Output is correct
43 Correct 121 ms 12048 KB Output is correct
44 Correct 202 ms 12052 KB Output is correct
45 Correct 123 ms 12056 KB Output is correct
46 Correct 189 ms 11992 KB Output is correct
47 Correct 155 ms 11928 KB Output is correct
48 Correct 79 ms 12148 KB Output is correct
49 Correct 101 ms 11928 KB Output is correct
50 Correct 100 ms 12024 KB Output is correct
51 Correct 149 ms 12056 KB Output is correct
52 Correct 220 ms 12056 KB Output is correct
53 Correct 108 ms 12128 KB Output is correct
54 Correct 143 ms 11928 KB Output is correct
55 Correct 148 ms 12056 KB Output is correct
56 Correct 103 ms 11932 KB Output is correct
57 Correct 92 ms 12056 KB Output is correct
58 Correct 83 ms 11916 KB Output is correct
59 Correct 129 ms 11892 KB Output is correct
60 Correct 118 ms 11960 KB Output is correct
61 Correct 88 ms 11932 KB Output is correct
62 Correct 163 ms 12056 KB Output is correct
63 Correct 133 ms 11916 KB Output is correct
64 Correct 74 ms 11920 KB Output is correct
65 Correct 137 ms 11964 KB Output is correct
66 Correct 133 ms 12056 KB Output is correct
67 Correct 127 ms 12060 KB Output is correct
68 Correct 149 ms 11928 KB Output is correct
69 Correct 137 ms 12180 KB Output is correct
70 Correct 121 ms 11928 KB Output is correct
71 Correct 832 ms 36492 KB Output is correct
72 Correct 807 ms 36472 KB Output is correct
73 Correct 1295 ms 35100 KB Output is correct
74 Execution timed out 2058 ms 27672 KB Time limit exceeded
75 Halted 0 ms 0 KB -