Submission #110121

# Submission time Handle Problem Language Result Execution time Memory
110121 2019-05-09T13:52:39 Z popovicirobert Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 21504 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];
char str[1 << 20];
vector <int> bit;

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

void bkt(int p, 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, cnf, ans, cnt, t);
        bkt(p + 1, 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));
        bit.clear();
        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, 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, 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, cnf, ans, 0, 1);
        }

        cout << ans << "\n";

    }

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

Compilation message

snake_escaping.cpp: In function 'void bktQ(int, int, int&)':
snake_escaping.cpp:15:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(p == bit.size()) {
        ~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'void bkt(int, int, int&, int, bool)':
snake_escaping.cpp:25: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:72:13: warning: unused variable 'val' [-Wunused-variable]
         int val = 0;
             ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 245 ms 8088 KB Output is correct
12 Correct 338 ms 7904 KB Output is correct
13 Correct 355 ms 6892 KB Output is correct
14 Correct 418 ms 7120 KB Output is correct
15 Correct 333 ms 8096 KB Output is correct
16 Correct 423 ms 7416 KB Output is correct
17 Correct 392 ms 7240 KB Output is correct
18 Correct 219 ms 8924 KB Output is correct
19 Correct 225 ms 5880 KB Output is correct
20 Correct 315 ms 7620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 245 ms 8088 KB Output is correct
12 Correct 338 ms 7904 KB Output is correct
13 Correct 355 ms 6892 KB Output is correct
14 Correct 418 ms 7120 KB Output is correct
15 Correct 333 ms 8096 KB Output is correct
16 Correct 423 ms 7416 KB Output is correct
17 Correct 392 ms 7240 KB Output is correct
18 Correct 219 ms 8924 KB Output is correct
19 Correct 225 ms 5880 KB Output is correct
20 Correct 315 ms 7620 KB Output is correct
21 Correct 407 ms 7928 KB Output is correct
22 Correct 392 ms 8060 KB Output is correct
23 Correct 475 ms 6660 KB Output is correct
24 Correct 480 ms 6464 KB Output is correct
25 Correct 424 ms 8440 KB Output is correct
26 Correct 477 ms 7032 KB Output is correct
27 Correct 500 ms 6904 KB Output is correct
28 Correct 252 ms 9284 KB Output is correct
29 Correct 318 ms 5496 KB Output is correct
30 Correct 399 ms 7672 KB Output is correct
31 Correct 461 ms 7416 KB Output is correct
32 Correct 538 ms 7640 KB Output is correct
33 Correct 343 ms 6232 KB Output is correct
34 Correct 464 ms 6400 KB Output is correct
35 Correct 466 ms 6904 KB Output is correct
36 Correct 208 ms 5368 KB Output is correct
37 Correct 340 ms 7288 KB Output is correct
38 Correct 381 ms 5368 KB Output is correct
39 Correct 410 ms 6648 KB Output is correct
40 Correct 467 ms 6076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 97 ms 11836 KB Output is correct
12 Correct 108 ms 11896 KB Output is correct
13 Correct 109 ms 11788 KB Output is correct
14 Correct 169 ms 11768 KB Output is correct
15 Correct 86 ms 11896 KB Output is correct
16 Correct 195 ms 11768 KB Output is correct
17 Correct 129 ms 11896 KB Output is correct
18 Correct 94 ms 12108 KB Output is correct
19 Correct 103 ms 11832 KB Output is correct
20 Correct 111 ms 11996 KB Output is correct
21 Correct 127 ms 11896 KB Output is correct
22 Correct 215 ms 11896 KB Output is correct
23 Correct 98 ms 11896 KB Output is correct
24 Correct 142 ms 11896 KB Output is correct
25 Correct 147 ms 11868 KB Output is correct
26 Correct 82 ms 11768 KB Output is correct
27 Correct 97 ms 11896 KB Output is correct
28 Correct 92 ms 11640 KB Output is correct
29 Correct 115 ms 11924 KB Output is correct
30 Correct 153 ms 11812 KB Output is correct
31 Correct 97 ms 11768 KB Output is correct
32 Correct 185 ms 11808 KB Output is correct
33 Correct 164 ms 11776 KB Output is correct
34 Correct 84 ms 11776 KB Output is correct
35 Correct 138 ms 11768 KB Output is correct
36 Correct 128 ms 11768 KB Output is correct
37 Correct 128 ms 11872 KB Output is correct
38 Correct 131 ms 11772 KB Output is correct
39 Correct 122 ms 11904 KB Output is correct
40 Correct 163 ms 11896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 245 ms 8088 KB Output is correct
12 Correct 338 ms 7904 KB Output is correct
13 Correct 355 ms 6892 KB Output is correct
14 Correct 418 ms 7120 KB Output is correct
15 Correct 333 ms 8096 KB Output is correct
16 Correct 423 ms 7416 KB Output is correct
17 Correct 392 ms 7240 KB Output is correct
18 Correct 219 ms 8924 KB Output is correct
19 Correct 225 ms 5880 KB Output is correct
20 Correct 315 ms 7620 KB Output is correct
21 Correct 407 ms 7928 KB Output is correct
22 Correct 392 ms 8060 KB Output is correct
23 Correct 475 ms 6660 KB Output is correct
24 Correct 480 ms 6464 KB Output is correct
25 Correct 424 ms 8440 KB Output is correct
26 Correct 477 ms 7032 KB Output is correct
27 Correct 500 ms 6904 KB Output is correct
28 Correct 252 ms 9284 KB Output is correct
29 Correct 318 ms 5496 KB Output is correct
30 Correct 399 ms 7672 KB Output is correct
31 Correct 461 ms 7416 KB Output is correct
32 Correct 538 ms 7640 KB Output is correct
33 Correct 343 ms 6232 KB Output is correct
34 Correct 464 ms 6400 KB Output is correct
35 Correct 466 ms 6904 KB Output is correct
36 Correct 208 ms 5368 KB Output is correct
37 Correct 340 ms 7288 KB Output is correct
38 Correct 381 ms 5368 KB Output is correct
39 Correct 410 ms 6648 KB Output is correct
40 Correct 467 ms 6076 KB Output is correct
41 Correct 97 ms 11836 KB Output is correct
42 Correct 108 ms 11896 KB Output is correct
43 Correct 109 ms 11788 KB Output is correct
44 Correct 169 ms 11768 KB Output is correct
45 Correct 86 ms 11896 KB Output is correct
46 Correct 195 ms 11768 KB Output is correct
47 Correct 129 ms 11896 KB Output is correct
48 Correct 94 ms 12108 KB Output is correct
49 Correct 103 ms 11832 KB Output is correct
50 Correct 111 ms 11996 KB Output is correct
51 Correct 127 ms 11896 KB Output is correct
52 Correct 215 ms 11896 KB Output is correct
53 Correct 98 ms 11896 KB Output is correct
54 Correct 142 ms 11896 KB Output is correct
55 Correct 147 ms 11868 KB Output is correct
56 Correct 82 ms 11768 KB Output is correct
57 Correct 97 ms 11896 KB Output is correct
58 Correct 92 ms 11640 KB Output is correct
59 Correct 115 ms 11924 KB Output is correct
60 Correct 153 ms 11812 KB Output is correct
61 Correct 97 ms 11768 KB Output is correct
62 Correct 185 ms 11808 KB Output is correct
63 Correct 164 ms 11776 KB Output is correct
64 Correct 84 ms 11776 KB Output is correct
65 Correct 138 ms 11768 KB Output is correct
66 Correct 128 ms 11768 KB Output is correct
67 Correct 128 ms 11872 KB Output is correct
68 Correct 131 ms 11772 KB Output is correct
69 Correct 122 ms 11904 KB Output is correct
70 Correct 163 ms 11896 KB Output is correct
71 Correct 872 ms 14692 KB Output is correct
72 Correct 754 ms 14844 KB Output is correct
73 Correct 1110 ms 13412 KB Output is correct
74 Execution timed out 2063 ms 21504 KB Time limit exceeded
75 Halted 0 ms 0 KB -