답안 #110126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
110126 2019-05-09T14:02:54 Z popovicirobert Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 39032 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;
int ans;

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

bool type;

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

}

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);

    scanf("%d%d%s" ,&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 - 1)); conf++) {
                int cur = (conf & ((1 << bit) - 1)) + (1 << bit) + ((conf >> bit) << (bit + 1));
                dp[i][cur] += dp[i][cur ^ (1 << bit)];
            }
        }
    }

    while(q > 0) {
        q--;

        static char cur[20];
        scanf("%s" ,cur);
        reverse(cur, cur + l);

        int numq = 0, num0 = 0, num1 = 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 cnf = 0;
        ans = 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);
        }
        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);
                }
            }

            type = 0;
            bkt(0, cnf, 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);
                }
            }

            type = 1;
            bkt(0, cnf, 0);
        }

        printf("%d\n" ,ans);

    }

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

Compilation message

snake_escaping.cpp: In function 'void bktQ(int, int)':
snake_escaping.cpp:16: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)':
snake_escaping.cpp:28: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:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%s" ,&l,&q,str);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s" ,cur);
         ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 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 2 ms 384 KB Output is correct
7 Correct 2 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
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 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 2 ms 384 KB Output is correct
7 Correct 2 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 447 ms 4660 KB Output is correct
12 Correct 379 ms 4472 KB Output is correct
13 Correct 412 ms 3796 KB Output is correct
14 Correct 387 ms 3832 KB Output is correct
15 Correct 390 ms 4728 KB Output is correct
16 Correct 563 ms 3940 KB Output is correct
17 Correct 503 ms 3956 KB Output is correct
18 Correct 289 ms 5724 KB Output is correct
19 Correct 270 ms 2812 KB Output is correct
20 Correct 413 ms 4344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 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 2 ms 384 KB Output is correct
7 Correct 2 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 447 ms 4660 KB Output is correct
12 Correct 379 ms 4472 KB Output is correct
13 Correct 412 ms 3796 KB Output is correct
14 Correct 387 ms 3832 KB Output is correct
15 Correct 390 ms 4728 KB Output is correct
16 Correct 563 ms 3940 KB Output is correct
17 Correct 503 ms 3956 KB Output is correct
18 Correct 289 ms 5724 KB Output is correct
19 Correct 270 ms 2812 KB Output is correct
20 Correct 413 ms 4344 KB Output is correct
21 Correct 519 ms 4916 KB Output is correct
22 Correct 441 ms 4984 KB Output is correct
23 Correct 515 ms 4076 KB Output is correct
24 Correct 517 ms 4016 KB Output is correct
25 Correct 480 ms 5628 KB Output is correct
26 Correct 598 ms 4060 KB Output is correct
27 Correct 557 ms 3996 KB Output is correct
28 Correct 400 ms 6536 KB Output is correct
29 Correct 447 ms 2680 KB Output is correct
30 Correct 464 ms 4728 KB Output is correct
31 Correct 623 ms 4564 KB Output is correct
32 Correct 607 ms 4680 KB Output is correct
33 Correct 460 ms 3468 KB Output is correct
34 Correct 558 ms 3756 KB Output is correct
35 Correct 549 ms 4176 KB Output is correct
36 Correct 229 ms 2652 KB Output is correct
37 Correct 432 ms 4472 KB Output is correct
38 Correct 314 ms 2508 KB Output is correct
39 Correct 430 ms 3576 KB Output is correct
40 Correct 648 ms 3576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 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 2 ms 384 KB Output is correct
7 Correct 2 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 94 ms 9876 KB Output is correct
12 Correct 87 ms 10000 KB Output is correct
13 Correct 101 ms 9820 KB Output is correct
14 Correct 169 ms 9848 KB Output is correct
15 Correct 172 ms 9976 KB Output is correct
16 Correct 175 ms 9848 KB Output is correct
17 Correct 162 ms 9848 KB Output is correct
18 Correct 100 ms 10204 KB Output is correct
19 Correct 92 ms 9736 KB Output is correct
20 Correct 97 ms 9876 KB Output is correct
21 Correct 131 ms 9976 KB Output is correct
22 Correct 193 ms 10004 KB Output is correct
23 Correct 97 ms 9992 KB Output is correct
24 Correct 161 ms 9908 KB Output is correct
25 Correct 142 ms 9848 KB Output is correct
26 Correct 84 ms 9728 KB Output is correct
27 Correct 109 ms 10012 KB Output is correct
28 Correct 108 ms 9848 KB Output is correct
29 Correct 100 ms 9852 KB Output is correct
30 Correct 111 ms 9956 KB Output is correct
31 Correct 96 ms 9900 KB Output is correct
32 Correct 187 ms 9976 KB Output is correct
33 Correct 143 ms 9848 KB Output is correct
34 Correct 76 ms 9728 KB Output is correct
35 Correct 134 ms 9876 KB Output is correct
36 Correct 131 ms 9848 KB Output is correct
37 Correct 136 ms 9948 KB Output is correct
38 Correct 152 ms 9976 KB Output is correct
39 Correct 129 ms 9812 KB Output is correct
40 Correct 142 ms 9848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 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 2 ms 384 KB Output is correct
7 Correct 2 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 447 ms 4660 KB Output is correct
12 Correct 379 ms 4472 KB Output is correct
13 Correct 412 ms 3796 KB Output is correct
14 Correct 387 ms 3832 KB Output is correct
15 Correct 390 ms 4728 KB Output is correct
16 Correct 563 ms 3940 KB Output is correct
17 Correct 503 ms 3956 KB Output is correct
18 Correct 289 ms 5724 KB Output is correct
19 Correct 270 ms 2812 KB Output is correct
20 Correct 413 ms 4344 KB Output is correct
21 Correct 519 ms 4916 KB Output is correct
22 Correct 441 ms 4984 KB Output is correct
23 Correct 515 ms 4076 KB Output is correct
24 Correct 517 ms 4016 KB Output is correct
25 Correct 480 ms 5628 KB Output is correct
26 Correct 598 ms 4060 KB Output is correct
27 Correct 557 ms 3996 KB Output is correct
28 Correct 400 ms 6536 KB Output is correct
29 Correct 447 ms 2680 KB Output is correct
30 Correct 464 ms 4728 KB Output is correct
31 Correct 623 ms 4564 KB Output is correct
32 Correct 607 ms 4680 KB Output is correct
33 Correct 460 ms 3468 KB Output is correct
34 Correct 558 ms 3756 KB Output is correct
35 Correct 549 ms 4176 KB Output is correct
36 Correct 229 ms 2652 KB Output is correct
37 Correct 432 ms 4472 KB Output is correct
38 Correct 314 ms 2508 KB Output is correct
39 Correct 430 ms 3576 KB Output is correct
40 Correct 648 ms 3576 KB Output is correct
41 Correct 94 ms 9876 KB Output is correct
42 Correct 87 ms 10000 KB Output is correct
43 Correct 101 ms 9820 KB Output is correct
44 Correct 169 ms 9848 KB Output is correct
45 Correct 172 ms 9976 KB Output is correct
46 Correct 175 ms 9848 KB Output is correct
47 Correct 162 ms 9848 KB Output is correct
48 Correct 100 ms 10204 KB Output is correct
49 Correct 92 ms 9736 KB Output is correct
50 Correct 97 ms 9876 KB Output is correct
51 Correct 131 ms 9976 KB Output is correct
52 Correct 193 ms 10004 KB Output is correct
53 Correct 97 ms 9992 KB Output is correct
54 Correct 161 ms 9908 KB Output is correct
55 Correct 142 ms 9848 KB Output is correct
56 Correct 84 ms 9728 KB Output is correct
57 Correct 109 ms 10012 KB Output is correct
58 Correct 108 ms 9848 KB Output is correct
59 Correct 100 ms 9852 KB Output is correct
60 Correct 111 ms 9956 KB Output is correct
61 Correct 96 ms 9900 KB Output is correct
62 Correct 187 ms 9976 KB Output is correct
63 Correct 143 ms 9848 KB Output is correct
64 Correct 76 ms 9728 KB Output is correct
65 Correct 134 ms 9876 KB Output is correct
66 Correct 131 ms 9848 KB Output is correct
67 Correct 136 ms 9948 KB Output is correct
68 Correct 152 ms 9976 KB Output is correct
69 Correct 129 ms 9812 KB Output is correct
70 Correct 142 ms 9848 KB Output is correct
71 Correct 812 ms 14788 KB Output is correct
72 Correct 834 ms 14812 KB Output is correct
73 Correct 851 ms 13524 KB Output is correct
74 Correct 1943 ms 14800 KB Output is correct
75 Correct 904 ms 37092 KB Output is correct
76 Correct 1930 ms 35584 KB Output is correct
77 Correct 1599 ms 35392 KB Output is correct
78 Correct 473 ms 39032 KB Output is correct
79 Correct 592 ms 33300 KB Output is correct
80 Correct 834 ms 36216 KB Output is correct
81 Correct 1421 ms 36192 KB Output is correct
82 Execution timed out 2040 ms 34184 KB Time limit exceeded
83 Halted 0 ms 0 KB -