Submission #1023355

# Submission time Handle Problem Language Result Execution time Memory
1023355 2024-07-14T16:44:46 Z snpmrnhlol Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
674 ms 42180 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 20;
int cost[1<<N],cost0[1<<N],cost1[1<<N];
vector <int> pos;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,q;
    cin>>n>>q;
    for(int i = 0;i < (1<<n);i++){
        char ch;
        cin>>ch;
        ch-='0';
        cost[i]+=ch;
        cost0[i]+=ch;
        cost1[i]+=ch;
    }
    for(int i = 0;i < n;i++){
        for(int j = 0;j < (1<<i);j++){
            for(int k = 0;k < (1<<n);k+=(1<<(i + 1))){
                cost0[(1<<i) + j + k]+=cost0[j + k];
                ///0 - 0
                ///1 - 0 and 1
            }
        }
    }
    for(int i = 0;i < n;i++){
        for(int j = 0;j < (1<<i);j++){
            for(int k = 0;k < (1<<n);k+=(1<<(i + 1))){
                cost1[j + k]+=cost1[j + k + (1<<i)];
                ///0 - 0 and 1
                ///1 - 1
            }
        }
    }
    for(int i = 0;i < q;i++){
        int cnt[3];
        cnt[0] = cnt[1] = cnt[2] = 0;
        string x;
        cin>>x;
        for(int j = 0;j < x.size();j++){
            if(x[j] == '0'){
                cnt[0]++;
            }else if(x[j] == '1'){
                cnt[1]++;
            }else{
                cnt[2]++;
            }
        }
        pos.clear();
        if(cnt[0] <= min(cnt[1],cnt[2])){
            ///0 count low
            ///use cost1
            for(int k = 0;k < n;k++){
                if(x[k] == '0'){
                    pos.push_back(n - k - 1);
                }
            }
            int nr = 0;
            for(int k = 0;k < n;k++){
                if(x[k] == '1'){
                    nr = nr*2 + 1;
                }else{
                    nr = nr*2;
                }
            }
            int ans = 0;
            for(int k = 0;k < (1<<pos.size());k++){
                if(k%2 == 0)ans+=cost1[nr];
                else ans-=cost1[nr];
                if(k != (1<<pos.size()) - 1)nr^=(1<<pos[__builtin_ctz(k + 1)]);
            }
            cout<<ans<<'\n';
        }else if(cnt[1] <= min(cnt[2],cnt[0])){
            ///1 count low
            ///use cost0
            for(int k = 0;k < n;k++){
                if(x[k] == '1'){
                    pos.push_back(n - k - 1);
                }
            }
            int nr = 0;
            for(int k = 0;k < n;k++){
                if(x[k] == '0'){
                    nr = nr*2;
                }else{
                    nr = nr*2 + 1;
                }
            }
            int ans = 0;
            for(int k = 0;k < (1<<pos.size());k++){
                if(k%2 == 0)ans+=cost0[nr];
                else ans-=cost0[nr];
                if(k != (1<<pos.size()) - 1)nr^=(1<<pos[__builtin_ctz(k + 1)]);
            }
            cout<<ans<<'\n';
        }else{
            ///? count low
            ///use cost
            for(int k = 0;k < n;k++){
                if(x[k] == '?'){
                    pos.push_back(n - k - 1);
                }
            }
            int nr = 0;
            for(int k = 0;k < n;k++){
                if(x[k] == '1'){
                    nr = nr*2 + 1;
                }else{
                    nr = nr*2;
                }
            }
            int ans = 0;
            for(int k = 0;k < (1<<pos.size());k++){
                ans+=cost[nr];
                //cout<<nr<<' '<<(1<<__builtin_ctz(k + 1))<<'\n';
                if(k != (1<<pos.size()) - 1)nr^=(1<<pos[__builtin_ctz(k + 1)]);
            }
            cout<<ans<<'\n';
        }
    }
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:42:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         for(int j = 0;j < x.size();j++){
      |                       ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 472 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 472 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 146 ms 14420 KB Output is correct
12 Correct 147 ms 13904 KB Output is correct
13 Correct 187 ms 13180 KB Output is correct
14 Correct 178 ms 13392 KB Output is correct
15 Correct 154 ms 14420 KB Output is correct
16 Correct 187 ms 13448 KB Output is correct
17 Correct 193 ms 13472 KB Output is correct
18 Correct 122 ms 15440 KB Output is correct
19 Correct 139 ms 12372 KB Output is correct
20 Correct 157 ms 12480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 472 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 146 ms 14420 KB Output is correct
12 Correct 147 ms 13904 KB Output is correct
13 Correct 187 ms 13180 KB Output is correct
14 Correct 178 ms 13392 KB Output is correct
15 Correct 154 ms 14420 KB Output is correct
16 Correct 187 ms 13448 KB Output is correct
17 Correct 193 ms 13472 KB Output is correct
18 Correct 122 ms 15440 KB Output is correct
19 Correct 139 ms 12372 KB Output is correct
20 Correct 157 ms 12480 KB Output is correct
21 Correct 166 ms 15104 KB Output is correct
22 Correct 188 ms 15448 KB Output is correct
23 Correct 229 ms 14400 KB Output is correct
24 Correct 244 ms 14428 KB Output is correct
25 Correct 187 ms 16212 KB Output is correct
26 Correct 249 ms 14760 KB Output is correct
27 Correct 236 ms 14676 KB Output is correct
28 Correct 128 ms 17120 KB Output is correct
29 Correct 174 ms 13208 KB Output is correct
30 Correct 190 ms 15440 KB Output is correct
31 Correct 214 ms 15164 KB Output is correct
32 Correct 234 ms 15288 KB Output is correct
33 Correct 215 ms 14164 KB Output is correct
34 Correct 236 ms 14160 KB Output is correct
35 Correct 234 ms 16460 KB Output is correct
36 Correct 120 ms 14928 KB Output is correct
37 Correct 157 ms 16880 KB Output is correct
38 Correct 171 ms 14928 KB Output is correct
39 Correct 212 ms 16208 KB Output is correct
40 Correct 231 ms 15960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 472 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 191 ms 14932 KB Output is correct
12 Correct 193 ms 15028 KB Output is correct
13 Correct 197 ms 14792 KB Output is correct
14 Correct 204 ms 14932 KB Output is correct
15 Correct 196 ms 14932 KB Output is correct
16 Correct 205 ms 14860 KB Output is correct
17 Correct 200 ms 14872 KB Output is correct
18 Correct 195 ms 15188 KB Output is correct
19 Correct 193 ms 14676 KB Output is correct
20 Correct 193 ms 14928 KB Output is correct
21 Correct 199 ms 14928 KB Output is correct
22 Correct 207 ms 14932 KB Output is correct
23 Correct 195 ms 14776 KB Output is correct
24 Correct 211 ms 14776 KB Output is correct
25 Correct 199 ms 14932 KB Output is correct
26 Correct 187 ms 14672 KB Output is correct
27 Correct 196 ms 15028 KB Output is correct
28 Correct 194 ms 14780 KB Output is correct
29 Correct 196 ms 14932 KB Output is correct
30 Correct 207 ms 15032 KB Output is correct
31 Correct 195 ms 14788 KB Output is correct
32 Correct 206 ms 14780 KB Output is correct
33 Correct 207 ms 14932 KB Output is correct
34 Correct 187 ms 14676 KB Output is correct
35 Correct 198 ms 14932 KB Output is correct
36 Correct 200 ms 14928 KB Output is correct
37 Correct 201 ms 14788 KB Output is correct
38 Correct 201 ms 14932 KB Output is correct
39 Correct 200 ms 14928 KB Output is correct
40 Correct 201 ms 14932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 472 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 146 ms 14420 KB Output is correct
12 Correct 147 ms 13904 KB Output is correct
13 Correct 187 ms 13180 KB Output is correct
14 Correct 178 ms 13392 KB Output is correct
15 Correct 154 ms 14420 KB Output is correct
16 Correct 187 ms 13448 KB Output is correct
17 Correct 193 ms 13472 KB Output is correct
18 Correct 122 ms 15440 KB Output is correct
19 Correct 139 ms 12372 KB Output is correct
20 Correct 157 ms 12480 KB Output is correct
21 Correct 166 ms 15104 KB Output is correct
22 Correct 188 ms 15448 KB Output is correct
23 Correct 229 ms 14400 KB Output is correct
24 Correct 244 ms 14428 KB Output is correct
25 Correct 187 ms 16212 KB Output is correct
26 Correct 249 ms 14760 KB Output is correct
27 Correct 236 ms 14676 KB Output is correct
28 Correct 128 ms 17120 KB Output is correct
29 Correct 174 ms 13208 KB Output is correct
30 Correct 190 ms 15440 KB Output is correct
31 Correct 214 ms 15164 KB Output is correct
32 Correct 234 ms 15288 KB Output is correct
33 Correct 215 ms 14164 KB Output is correct
34 Correct 236 ms 14160 KB Output is correct
35 Correct 234 ms 16460 KB Output is correct
36 Correct 120 ms 14928 KB Output is correct
37 Correct 157 ms 16880 KB Output is correct
38 Correct 171 ms 14928 KB Output is correct
39 Correct 212 ms 16208 KB Output is correct
40 Correct 231 ms 15960 KB Output is correct
41 Correct 191 ms 14932 KB Output is correct
42 Correct 193 ms 15028 KB Output is correct
43 Correct 197 ms 14792 KB Output is correct
44 Correct 204 ms 14932 KB Output is correct
45 Correct 196 ms 14932 KB Output is correct
46 Correct 205 ms 14860 KB Output is correct
47 Correct 200 ms 14872 KB Output is correct
48 Correct 195 ms 15188 KB Output is correct
49 Correct 193 ms 14676 KB Output is correct
50 Correct 193 ms 14928 KB Output is correct
51 Correct 199 ms 14928 KB Output is correct
52 Correct 207 ms 14932 KB Output is correct
53 Correct 195 ms 14776 KB Output is correct
54 Correct 211 ms 14776 KB Output is correct
55 Correct 199 ms 14932 KB Output is correct
56 Correct 187 ms 14672 KB Output is correct
57 Correct 196 ms 15028 KB Output is correct
58 Correct 194 ms 14780 KB Output is correct
59 Correct 196 ms 14932 KB Output is correct
60 Correct 207 ms 15032 KB Output is correct
61 Correct 195 ms 14788 KB Output is correct
62 Correct 206 ms 14780 KB Output is correct
63 Correct 207 ms 14932 KB Output is correct
64 Correct 187 ms 14676 KB Output is correct
65 Correct 198 ms 14932 KB Output is correct
66 Correct 200 ms 14928 KB Output is correct
67 Correct 201 ms 14788 KB Output is correct
68 Correct 201 ms 14932 KB Output is correct
69 Correct 200 ms 14928 KB Output is correct
70 Correct 201 ms 14932 KB Output is correct
71 Correct 433 ms 39248 KB Output is correct
72 Correct 429 ms 39252 KB Output is correct
73 Correct 530 ms 37820 KB Output is correct
74 Correct 674 ms 38224 KB Output is correct
75 Correct 455 ms 40276 KB Output is correct
76 Correct 655 ms 38796 KB Output is correct
77 Correct 598 ms 38328 KB Output is correct
78 Correct 336 ms 42180 KB Output is correct
79 Correct 426 ms 36176 KB Output is correct
80 Correct 426 ms 39252 KB Output is correct
81 Correct 517 ms 39252 KB Output is correct
82 Correct 645 ms 38228 KB Output is correct
83 Correct 451 ms 37460 KB Output is correct
84 Correct 628 ms 38224 KB Output is correct
85 Correct 597 ms 38484 KB Output is correct
86 Correct 339 ms 36312 KB Output is correct
87 Correct 413 ms 39252 KB Output is correct
88 Correct 421 ms 36292 KB Output is correct
89 Correct 554 ms 37972 KB Output is correct
90 Correct 639 ms 38224 KB Output is correct
91 Correct 460 ms 37460 KB Output is correct
92 Correct 635 ms 38452 KB Output is correct
93 Correct 595 ms 38336 KB Output is correct
94 Correct 356 ms 36248 KB Output is correct
95 Correct 531 ms 38324 KB Output is correct
96 Correct 528 ms 38224 KB Output is correct
97 Correct 527 ms 38280 KB Output is correct
98 Correct 543 ms 38340 KB Output is correct
99 Correct 537 ms 38200 KB Output is correct
100 Correct 551 ms 38332 KB Output is correct