# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1023353 | 2024-07-14T16:42:40 Z | snpmrnhlol | Snake Escaping (JOI18_snake_escaping) | C++17 | 2000 ms | 20052 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(1){ ///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] == '?'){ 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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 1 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
8 | Correct | 0 ms | 348 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 | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 1 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
8 | Correct | 0 ms | 348 KB | Output is correct |
9 | Correct | 1 ms | 348 KB | Output is correct |
10 | Correct | 0 ms | 348 KB | Output is correct |
11 | Correct | 105 ms | 14032 KB | Output is correct |
12 | Correct | 227 ms | 13652 KB | Output is correct |
13 | Correct | 174 ms | 12880 KB | Output is correct |
14 | Correct | 159 ms | 13136 KB | Output is correct |
15 | Correct | 130 ms | 14120 KB | Output is correct |
16 | Correct | 139 ms | 13388 KB | Output is correct |
17 | Correct | 175 ms | 13260 KB | Output is correct |
18 | Correct | 143 ms | 15188 KB | Output is correct |
19 | Correct | 167 ms | 11992 KB | Output is correct |
20 | Correct | 106 ms | 14420 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 1 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
8 | Correct | 0 ms | 348 KB | Output is correct |
9 | Correct | 1 ms | 348 KB | Output is correct |
10 | Correct | 0 ms | 348 KB | Output is correct |
11 | Correct | 105 ms | 14032 KB | Output is correct |
12 | Correct | 227 ms | 13652 KB | Output is correct |
13 | Correct | 174 ms | 12880 KB | Output is correct |
14 | Correct | 159 ms | 13136 KB | Output is correct |
15 | Correct | 130 ms | 14120 KB | Output is correct |
16 | Correct | 139 ms | 13388 KB | Output is correct |
17 | Correct | 175 ms | 13260 KB | Output is correct |
18 | Correct | 143 ms | 15188 KB | Output is correct |
19 | Correct | 167 ms | 11992 KB | Output is correct |
20 | Correct | 106 ms | 14420 KB | Output is correct |
21 | Correct | 112 ms | 17700 KB | Output is correct |
22 | Correct | 399 ms | 17928 KB | Output is correct |
23 | Correct | 249 ms | 16212 KB | Output is correct |
24 | Correct | 187 ms | 15892 KB | Output is correct |
25 | Correct | 149 ms | 18012 KB | Output is correct |
26 | Correct | 165 ms | 16468 KB | Output is correct |
27 | Correct | 211 ms | 16536 KB | Output is correct |
28 | Correct | 124 ms | 20052 KB | Output is correct |
29 | Correct | 225 ms | 16228 KB | Output is correct |
30 | Correct | 111 ms | 18252 KB | Output is correct |
31 | Correct | 175 ms | 18216 KB | Output is correct |
32 | Correct | 163 ms | 18256 KB | Output is correct |
33 | Correct | 145 ms | 16980 KB | Output is correct |
34 | Correct | 213 ms | 17236 KB | Output is correct |
35 | Correct | 218 ms | 17600 KB | Output is correct |
36 | Correct | 109 ms | 16212 KB | Output is correct |
37 | Correct | 308 ms | 18256 KB | Output is correct |
38 | Correct | 333 ms | 16212 KB | Output is correct |
39 | Correct | 163 ms | 17236 KB | Output is correct |
40 | Correct | 139 ms | 17232 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 1 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
8 | Correct | 0 ms | 348 KB | Output is correct |
9 | Correct | 1 ms | 348 KB | Output is correct |
10 | Correct | 0 ms | 348 KB | Output is correct |
11 | Correct | 153 ms | 14928 KB | Output is correct |
12 | Correct | 575 ms | 15040 KB | Output is correct |
13 | Correct | 308 ms | 14796 KB | Output is correct |
14 | Correct | 201 ms | 14932 KB | Output is correct |
15 | Correct | 188 ms | 14932 KB | Output is correct |
16 | Correct | 191 ms | 14928 KB | Output is correct |
17 | Correct | 223 ms | 14796 KB | Output is correct |
18 | Correct | 180 ms | 15184 KB | Output is correct |
19 | Correct | 290 ms | 14788 KB | Output is correct |
20 | Correct | 185 ms | 14928 KB | Output is correct |
21 | Correct | 192 ms | 14864 KB | Output is correct |
22 | Correct | 195 ms | 14932 KB | Output is correct |
23 | Correct | 176 ms | 14928 KB | Output is correct |
24 | Correct | 215 ms | 14776 KB | Output is correct |
25 | Correct | 219 ms | 14928 KB | Output is correct |
26 | Correct | 174 ms | 14672 KB | Output is correct |
27 | Correct | 292 ms | 14928 KB | Output is correct |
28 | Correct | 602 ms | 14680 KB | Output is correct |
29 | Correct | 193 ms | 14932 KB | Output is correct |
30 | Correct | 202 ms | 14784 KB | Output is correct |
31 | Correct | 1158 ms | 14716 KB | Output is correct |
32 | Correct | 224 ms | 14932 KB | Output is correct |
33 | Correct | 223 ms | 14932 KB | Output is correct |
34 | Execution timed out | 2093 ms | 13912 KB | Time limit exceeded |
35 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 1 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
8 | Correct | 0 ms | 348 KB | Output is correct |
9 | Correct | 1 ms | 348 KB | Output is correct |
10 | Correct | 0 ms | 348 KB | Output is correct |
11 | Correct | 105 ms | 14032 KB | Output is correct |
12 | Correct | 227 ms | 13652 KB | Output is correct |
13 | Correct | 174 ms | 12880 KB | Output is correct |
14 | Correct | 159 ms | 13136 KB | Output is correct |
15 | Correct | 130 ms | 14120 KB | Output is correct |
16 | Correct | 139 ms | 13388 KB | Output is correct |
17 | Correct | 175 ms | 13260 KB | Output is correct |
18 | Correct | 143 ms | 15188 KB | Output is correct |
19 | Correct | 167 ms | 11992 KB | Output is correct |
20 | Correct | 106 ms | 14420 KB | Output is correct |
21 | Correct | 112 ms | 17700 KB | Output is correct |
22 | Correct | 399 ms | 17928 KB | Output is correct |
23 | Correct | 249 ms | 16212 KB | Output is correct |
24 | Correct | 187 ms | 15892 KB | Output is correct |
25 | Correct | 149 ms | 18012 KB | Output is correct |
26 | Correct | 165 ms | 16468 KB | Output is correct |
27 | Correct | 211 ms | 16536 KB | Output is correct |
28 | Correct | 124 ms | 20052 KB | Output is correct |
29 | Correct | 225 ms | 16228 KB | Output is correct |
30 | Correct | 111 ms | 18252 KB | Output is correct |
31 | Correct | 175 ms | 18216 KB | Output is correct |
32 | Correct | 163 ms | 18256 KB | Output is correct |
33 | Correct | 145 ms | 16980 KB | Output is correct |
34 | Correct | 213 ms | 17236 KB | Output is correct |
35 | Correct | 218 ms | 17600 KB | Output is correct |
36 | Correct | 109 ms | 16212 KB | Output is correct |
37 | Correct | 308 ms | 18256 KB | Output is correct |
38 | Correct | 333 ms | 16212 KB | Output is correct |
39 | Correct | 163 ms | 17236 KB | Output is correct |
40 | Correct | 139 ms | 17232 KB | Output is correct |
41 | Correct | 153 ms | 14928 KB | Output is correct |
42 | Correct | 575 ms | 15040 KB | Output is correct |
43 | Correct | 308 ms | 14796 KB | Output is correct |
44 | Correct | 201 ms | 14932 KB | Output is correct |
45 | Correct | 188 ms | 14932 KB | Output is correct |
46 | Correct | 191 ms | 14928 KB | Output is correct |
47 | Correct | 223 ms | 14796 KB | Output is correct |
48 | Correct | 180 ms | 15184 KB | Output is correct |
49 | Correct | 290 ms | 14788 KB | Output is correct |
50 | Correct | 185 ms | 14928 KB | Output is correct |
51 | Correct | 192 ms | 14864 KB | Output is correct |
52 | Correct | 195 ms | 14932 KB | Output is correct |
53 | Correct | 176 ms | 14928 KB | Output is correct |
54 | Correct | 215 ms | 14776 KB | Output is correct |
55 | Correct | 219 ms | 14928 KB | Output is correct |
56 | Correct | 174 ms | 14672 KB | Output is correct |
57 | Correct | 292 ms | 14928 KB | Output is correct |
58 | Correct | 602 ms | 14680 KB | Output is correct |
59 | Correct | 193 ms | 14932 KB | Output is correct |
60 | Correct | 202 ms | 14784 KB | Output is correct |
61 | Correct | 1158 ms | 14716 KB | Output is correct |
62 | Correct | 224 ms | 14932 KB | Output is correct |
63 | Correct | 223 ms | 14932 KB | Output is correct |
64 | Execution timed out | 2093 ms | 13912 KB | Time limit exceeded |
65 | Halted | 0 ms | 0 KB | - |