# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
159657 | GioChkhaidze | Snake Escaping (JOI18_snake_escaping) | C++14 | 2069 ms | 5540 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
int n,q,C[1148576],M[20],dp[1794323],vs;
pair < int , int > v[1794323];
main () {
ios::sync_with_stdio(false);
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[++vs].F=rs;
v[vs].S=i;
}
sort(v+1,v+vs+1);
for (int i=1; i<=vs; i++) {
int x=v[i].S;
for (int j=n-1; j>=0; j--) {
if (x-2*M[j]>=0) {
x-=2*M[j];
if (dp[v[i].S]<(dp[v[i].S-2*M[j]]+dp[v[i].S-M[j]]))
dp[v[i].S]=(dp[v[i].S-2*M[j]]+dp[v[i].S-M[j]]);
}
else
if (x-M[j]>=0) x-=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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |