# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1023352 | 2024-07-14T16:41:56 Z | snpmrnhlol | Snake Escaping (JOI18_snake_escaping) | C++17 | 1 ms | 348 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] == '?'){ 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
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Incorrect | 1 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Incorrect | 1 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Incorrect | 1 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Incorrect | 1 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Incorrect | 1 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |