#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
int n,q,C[1148576];
long long M[20],dp[1794323];
vector < pair < int , int > > v;
main () {
cin>>n>>q;
M[0]=1;
for (int i=1; i<=n; i++)
M[i]=M[i-1]*3;
for (int i=0; i<(1<<n); i++) {
char c;
cin>>c;
C[i]=c-'0';
int x=0;
for (int j=0; j<n; j++)
if (i&(1<<j)) x+=M[j];
dp[x]+=C[i];
}
for (int i=0; i<M[n]; i++) {
int x=i,rs=0;
for (int j=n-1; j>=0; j--)
if (x-2*M[j]>=0) x-=2*M[j],rs++;
else
if (x-M[j]>=0) x-=M[j];
v.push_back({rs,i});
}
sort(v.begin(),v.end());
for (int i=0; i<v.size(); i++) {
int x=v[i].S,f[20];
for (int j=0; j<n; j++) f[j]=0;
for (int j=n-1; j>=0; j--) {
if (x-2*M[j]>=0) x-=2*M[j],f[j]=2;
else
if (x-M[j]>=0) x-=M[j],f[j]=1;
else f[j]=0;
}
for (int j=0; j<n; j++) {
if (f[j]==2) {
dp[v[i].S]=max(dp[v[i].S],(dp[v[i].S-2*M[j]]+dp[v[i].S-M[j]]));
}
}
}
for (int i=1; i<=q; i++) {
string s;
int x=0;
cin>>s;
for (int j=0; j<s.size(); j++) {
if (s[s.size()-j-1]=='?') x+=2*M[j];
else
if (s[s.size()-j-1]=='1') x+=M[j];
}
cout<<dp[x]<<endl;
}
}
Compilation message
snake_escaping.cpp:11:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:41:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v.size(); i++) {
~^~~~~~~~~
snake_escaping.cpp:64:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0; j<s.size(); j++) {
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1388 KB |
Output is correct |
2 |
Correct |
18 ms |
1392 KB |
Output is correct |
3 |
Correct |
18 ms |
1392 KB |
Output is correct |
4 |
Correct |
18 ms |
1392 KB |
Output is correct |
5 |
Correct |
18 ms |
1392 KB |
Output is correct |
6 |
Correct |
18 ms |
1392 KB |
Output is correct |
7 |
Correct |
18 ms |
1392 KB |
Output is correct |
8 |
Correct |
18 ms |
1392 KB |
Output is correct |
9 |
Correct |
18 ms |
1392 KB |
Output is correct |
10 |
Correct |
18 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1388 KB |
Output is correct |
2 |
Correct |
18 ms |
1392 KB |
Output is correct |
3 |
Correct |
18 ms |
1392 KB |
Output is correct |
4 |
Correct |
18 ms |
1392 KB |
Output is correct |
5 |
Correct |
18 ms |
1392 KB |
Output is correct |
6 |
Correct |
18 ms |
1392 KB |
Output is correct |
7 |
Correct |
18 ms |
1392 KB |
Output is correct |
8 |
Correct |
18 ms |
1392 KB |
Output is correct |
9 |
Correct |
18 ms |
1392 KB |
Output is correct |
10 |
Correct |
18 ms |
1388 KB |
Output is correct |
11 |
Execution timed out |
2054 ms |
10300 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1388 KB |
Output is correct |
2 |
Correct |
18 ms |
1392 KB |
Output is correct |
3 |
Correct |
18 ms |
1392 KB |
Output is correct |
4 |
Correct |
18 ms |
1392 KB |
Output is correct |
5 |
Correct |
18 ms |
1392 KB |
Output is correct |
6 |
Correct |
18 ms |
1392 KB |
Output is correct |
7 |
Correct |
18 ms |
1392 KB |
Output is correct |
8 |
Correct |
18 ms |
1392 KB |
Output is correct |
9 |
Correct |
18 ms |
1392 KB |
Output is correct |
10 |
Correct |
18 ms |
1388 KB |
Output is correct |
11 |
Execution timed out |
2054 ms |
10300 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1388 KB |
Output is correct |
2 |
Correct |
18 ms |
1392 KB |
Output is correct |
3 |
Correct |
18 ms |
1392 KB |
Output is correct |
4 |
Correct |
18 ms |
1392 KB |
Output is correct |
5 |
Correct |
18 ms |
1392 KB |
Output is correct |
6 |
Correct |
18 ms |
1392 KB |
Output is correct |
7 |
Correct |
18 ms |
1392 KB |
Output is correct |
8 |
Correct |
18 ms |
1392 KB |
Output is correct |
9 |
Correct |
18 ms |
1392 KB |
Output is correct |
10 |
Correct |
18 ms |
1388 KB |
Output is correct |
11 |
Runtime error |
10 ms |
3704 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1388 KB |
Output is correct |
2 |
Correct |
18 ms |
1392 KB |
Output is correct |
3 |
Correct |
18 ms |
1392 KB |
Output is correct |
4 |
Correct |
18 ms |
1392 KB |
Output is correct |
5 |
Correct |
18 ms |
1392 KB |
Output is correct |
6 |
Correct |
18 ms |
1392 KB |
Output is correct |
7 |
Correct |
18 ms |
1392 KB |
Output is correct |
8 |
Correct |
18 ms |
1392 KB |
Output is correct |
9 |
Correct |
18 ms |
1392 KB |
Output is correct |
10 |
Correct |
18 ms |
1388 KB |
Output is correct |
11 |
Execution timed out |
2054 ms |
10300 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |