답안 #110123

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
110123 2019-05-09T13:57:19 Z popovicirobert Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 14768 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);

    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); conf++) {
                if(conf & (1 << bit)) {
                    dp[i][conf] += dp[i][conf ^ (1 << bit)];
                }
            }
        }
    }

    while(q > 0) {
        q--;

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

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

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

    }

    //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;
             ^~~
snake_escaping.cpp:47: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:68: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 2 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 3 ms 356 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 2 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 3 ms 356 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 313 ms 5356 KB Output is correct
12 Correct 390 ms 5240 KB Output is correct
13 Correct 416 ms 4328 KB Output is correct
14 Correct 406 ms 4600 KB Output is correct
15 Correct 417 ms 5624 KB Output is correct
16 Correct 446 ms 4720 KB Output is correct
17 Correct 411 ms 4600 KB Output is correct
18 Correct 248 ms 6480 KB Output is correct
19 Correct 282 ms 3444 KB Output is correct
20 Correct 358 ms 5152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 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 3 ms 356 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 313 ms 5356 KB Output is correct
12 Correct 390 ms 5240 KB Output is correct
13 Correct 416 ms 4328 KB Output is correct
14 Correct 406 ms 4600 KB Output is correct
15 Correct 417 ms 5624 KB Output is correct
16 Correct 446 ms 4720 KB Output is correct
17 Correct 411 ms 4600 KB Output is correct
18 Correct 248 ms 6480 KB Output is correct
19 Correct 282 ms 3444 KB Output is correct
20 Correct 358 ms 5152 KB Output is correct
21 Correct 412 ms 5512 KB Output is correct
22 Correct 455 ms 5752 KB Output is correct
23 Correct 513 ms 4620 KB Output is correct
24 Correct 544 ms 4600 KB Output is correct
25 Correct 497 ms 6464 KB Output is correct
26 Correct 562 ms 5168 KB Output is correct
27 Correct 598 ms 5344 KB Output is correct
28 Correct 303 ms 7648 KB Output is correct
29 Correct 371 ms 3728 KB Output is correct
30 Correct 477 ms 6136 KB Output is correct
31 Correct 535 ms 6068 KB Output is correct
32 Correct 608 ms 5752 KB Output is correct
33 Correct 471 ms 4728 KB Output is correct
34 Correct 567 ms 4744 KB Output is correct
35 Correct 532 ms 5312 KB Output is correct
36 Correct 272 ms 3704 KB Output is correct
37 Correct 428 ms 5840 KB Output is correct
38 Correct 357 ms 3704 KB Output is correct
39 Correct 455 ms 4984 KB Output is correct
40 Correct 476 ms 4808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 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 3 ms 356 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 10576 KB Output is correct
12 Correct 95 ms 10588 KB Output is correct
13 Correct 120 ms 10604 KB Output is correct
14 Correct 170 ms 10488 KB Output is correct
15 Correct 93 ms 10616 KB Output is correct
16 Correct 165 ms 10620 KB Output is correct
17 Correct 127 ms 10488 KB Output is correct
18 Correct 85 ms 10732 KB Output is correct
19 Correct 91 ms 10368 KB Output is correct
20 Correct 101 ms 10552 KB Output is correct
21 Correct 127 ms 10608 KB Output is correct
22 Correct 208 ms 10540 KB Output is correct
23 Correct 98 ms 10488 KB Output is correct
24 Correct 135 ms 10516 KB Output is correct
25 Correct 150 ms 10536 KB Output is correct
26 Correct 71 ms 10372 KB Output is correct
27 Correct 93 ms 10500 KB Output is correct
28 Correct 92 ms 10420 KB Output is correct
29 Correct 111 ms 10488 KB Output is correct
30 Correct 129 ms 10616 KB Output is correct
31 Correct 96 ms 10488 KB Output is correct
32 Correct 163 ms 10616 KB Output is correct
33 Correct 157 ms 10620 KB Output is correct
34 Correct 84 ms 10360 KB Output is correct
35 Correct 127 ms 10504 KB Output is correct
36 Correct 130 ms 10524 KB Output is correct
37 Correct 132 ms 10616 KB Output is correct
38 Correct 126 ms 10464 KB Output is correct
39 Correct 136 ms 10488 KB Output is correct
40 Correct 124 ms 10360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 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 3 ms 356 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 313 ms 5356 KB Output is correct
12 Correct 390 ms 5240 KB Output is correct
13 Correct 416 ms 4328 KB Output is correct
14 Correct 406 ms 4600 KB Output is correct
15 Correct 417 ms 5624 KB Output is correct
16 Correct 446 ms 4720 KB Output is correct
17 Correct 411 ms 4600 KB Output is correct
18 Correct 248 ms 6480 KB Output is correct
19 Correct 282 ms 3444 KB Output is correct
20 Correct 358 ms 5152 KB Output is correct
21 Correct 412 ms 5512 KB Output is correct
22 Correct 455 ms 5752 KB Output is correct
23 Correct 513 ms 4620 KB Output is correct
24 Correct 544 ms 4600 KB Output is correct
25 Correct 497 ms 6464 KB Output is correct
26 Correct 562 ms 5168 KB Output is correct
27 Correct 598 ms 5344 KB Output is correct
28 Correct 303 ms 7648 KB Output is correct
29 Correct 371 ms 3728 KB Output is correct
30 Correct 477 ms 6136 KB Output is correct
31 Correct 535 ms 6068 KB Output is correct
32 Correct 608 ms 5752 KB Output is correct
33 Correct 471 ms 4728 KB Output is correct
34 Correct 567 ms 4744 KB Output is correct
35 Correct 532 ms 5312 KB Output is correct
36 Correct 272 ms 3704 KB Output is correct
37 Correct 428 ms 5840 KB Output is correct
38 Correct 357 ms 3704 KB Output is correct
39 Correct 455 ms 4984 KB Output is correct
40 Correct 476 ms 4808 KB Output is correct
41 Correct 100 ms 10576 KB Output is correct
42 Correct 95 ms 10588 KB Output is correct
43 Correct 120 ms 10604 KB Output is correct
44 Correct 170 ms 10488 KB Output is correct
45 Correct 93 ms 10616 KB Output is correct
46 Correct 165 ms 10620 KB Output is correct
47 Correct 127 ms 10488 KB Output is correct
48 Correct 85 ms 10732 KB Output is correct
49 Correct 91 ms 10368 KB Output is correct
50 Correct 101 ms 10552 KB Output is correct
51 Correct 127 ms 10608 KB Output is correct
52 Correct 208 ms 10540 KB Output is correct
53 Correct 98 ms 10488 KB Output is correct
54 Correct 135 ms 10516 KB Output is correct
55 Correct 150 ms 10536 KB Output is correct
56 Correct 71 ms 10372 KB Output is correct
57 Correct 93 ms 10500 KB Output is correct
58 Correct 92 ms 10420 KB Output is correct
59 Correct 111 ms 10488 KB Output is correct
60 Correct 129 ms 10616 KB Output is correct
61 Correct 96 ms 10488 KB Output is correct
62 Correct 163 ms 10616 KB Output is correct
63 Correct 157 ms 10620 KB Output is correct
64 Correct 84 ms 10360 KB Output is correct
65 Correct 127 ms 10504 KB Output is correct
66 Correct 130 ms 10524 KB Output is correct
67 Correct 132 ms 10616 KB Output is correct
68 Correct 126 ms 10464 KB Output is correct
69 Correct 136 ms 10488 KB Output is correct
70 Correct 124 ms 10360 KB Output is correct
71 Correct 743 ms 14684 KB Output is correct
72 Correct 772 ms 14768 KB Output is correct
73 Correct 1187 ms 13364 KB Output is correct
74 Execution timed out 2059 ms 14504 KB Time limit exceeded
75 Halted 0 ms 0 KB -