Submission #331808

# Submission time Handle Problem Language Result Execution time Memory
331808 2020-11-30T08:16:28 Z daniel920712 Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 23148 KB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string.h>
#include <map>
#include <utility>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
char all[1148576];
char tt[25];
int con1[2][1100005];
int con2[2][1100005];
int ans=0;
int N;
queue < pair < pair < int , int > , int > > BFS;
vector < int > xxx;
int main()
{
    //freopen("01-05.txt","rt",stdin);
    //freopen("tt.txt","w+t",stdout);
    int M,i,j,ok,a,b,c,x,y,z,con=0,xx;
    scanf("%d %d",&N,&M);
    scanf("%s",all);
    for(i=0;i<(1<<N);i++) con2[0][i]=con1[0][i]=all[i]-'0';
    for(i=0;i<N;i++)
    {
        for(j=0;j<(1<<N);j++)
        {
            con1[1-i%2][j]=con1[i%2][j];
            con2[1-i%2][j]=con2[i%2][j];
            if(j&(1<<i)) con1[1-i%2][j]+=con1[i%2][j-(1<<i)];
            else con2[1-i%2][j]+=con2[i%2][j+(1<<i)];
        }
    }



    while(M--)
    {
        scanf("%s",tt);
        ans=0;
        a=0;
        b=0;
        c=0;
        for(i=0;i<N;i++)
        {
            if(tt[i]=='?') a++;
            else if(tt[i]=='1') b++;
            else c++;
        }

        xxx.clear();
        con=0;
        if(b<=a&&b<=c)
        {
            for(i=0;i<N;i++)
            {
                if(tt[i]=='1') xxx.push_back(i);
                else if(tt[i]=='?') con+=(1<<(N-i-1));
            }


            BFS.push(make_pair(make_pair(0,con),0));
            while(!BFS.empty())
            {
                x=BFS.front().first.first;
                y=BFS.front().first.second;
                z=BFS.front().second;
                BFS.pop();
                if(x==b)
                {
                    if(z==0) ans+=con1[N%2][y];
                    else ans-=con1[N%2][y];
                    continue;
                }
                xx=xxx[x];

                BFS.push(make_pair(make_pair(x+1,y+(1<<(N-xx-1))),z));
                BFS.push(make_pair(make_pair(x+1,y),z^1));


            }
        }
        else if(a<=b&&a<=c)
        {
            for(i=0;i<N;i++)
            {
                if(tt[i]=='?') xxx.push_back(i);
                else if(tt[i]=='1') con+=(1<<(N-i-1));
            }
            BFS.push(make_pair(make_pair(0,con),0));
            while(!BFS.empty())
            {
                x=BFS.front().first.first;
                y=BFS.front().first.second;
                z=BFS.front().second;
                BFS.pop();
                if(x==a)
                {
                    ans+=(all[y]-'0');
                    continue;
                }

                xx=xxx[x];
                BFS.push(make_pair(make_pair(x+1,y+(1<<(N-xx-1))),z));
                BFS.push(make_pair(make_pair(x+1,y),z));

            }
        }
        else if(c<=b&&c<=a)
        {
            //printf("aa\n");
            for(i=0;i<N;i++)
            {
                if(tt[i]=='0') xxx.push_back(i);
                else if(tt[i]=='1') con+=(1<<(N-i-1));
            }


            BFS.push(make_pair(make_pair(0,con),0));
            while(!BFS.empty())
            {
                x=BFS.front().first.first;
                y=BFS.front().first.second;
                z=BFS.front().second;
                //printf("aa %d %d\n",x,y);
                BFS.pop();
                if(x==c)
                {
                    if(z==0) ans+=con2[N%2][y];
                    else ans-=con2[N%2][y];
                    continue;
                }
                xx=xxx[x];
                BFS.push(make_pair(make_pair(x+1,y+(1<<(N-xx-1))),z^1));
                BFS.push(make_pair(make_pair(x+1,y),z));


            }
        }
        printf("%d\n",ans);
    }

	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:24:15: warning: unused variable 'ok' [-Wunused-variable]
   24 |     int M,i,j,ok,a,b,c,x,y,z,con=0,xx;
      |               ^~
snake_escaping.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   25 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   26 |     scanf("%s",all);
      |     ~~~~~^~~~~~~~~~
snake_escaping.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   43 |         scanf("%s",tt);
      |         ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 339 ms 4588 KB Output is correct
12 Correct 354 ms 4176 KB Output is correct
13 Correct 458 ms 3308 KB Output is correct
14 Correct 627 ms 3692 KB Output is correct
15 Correct 437 ms 4720 KB Output is correct
16 Correct 560 ms 3692 KB Output is correct
17 Correct 573 ms 3564 KB Output is correct
18 Correct 272 ms 5520 KB Output is correct
19 Correct 319 ms 2412 KB Output is correct
20 Correct 377 ms 4076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 339 ms 4588 KB Output is correct
12 Correct 354 ms 4176 KB Output is correct
13 Correct 458 ms 3308 KB Output is correct
14 Correct 627 ms 3692 KB Output is correct
15 Correct 437 ms 4720 KB Output is correct
16 Correct 560 ms 3692 KB Output is correct
17 Correct 573 ms 3564 KB Output is correct
18 Correct 272 ms 5520 KB Output is correct
19 Correct 319 ms 2412 KB Output is correct
20 Correct 377 ms 4076 KB Output is correct
21 Correct 408 ms 4492 KB Output is correct
22 Correct 433 ms 4716 KB Output is correct
23 Correct 621 ms 3692 KB Output is correct
24 Correct 925 ms 3476 KB Output is correct
25 Correct 541 ms 5692 KB Output is correct
26 Correct 793 ms 4076 KB Output is correct
27 Correct 706 ms 4092 KB Output is correct
28 Correct 308 ms 6660 KB Output is correct
29 Correct 393 ms 2540 KB Output is correct
30 Correct 483 ms 4716 KB Output is correct
31 Correct 651 ms 4696 KB Output is correct
32 Correct 955 ms 4588 KB Output is correct
33 Correct 496 ms 3460 KB Output is correct
34 Correct 780 ms 3692 KB Output is correct
35 Correct 709 ms 4076 KB Output is correct
36 Correct 311 ms 2540 KB Output is correct
37 Correct 398 ms 4716 KB Output is correct
38 Correct 454 ms 2796 KB Output is correct
39 Correct 736 ms 3692 KB Output is correct
40 Correct 960 ms 3564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 86 ms 18028 KB Output is correct
12 Correct 86 ms 18028 KB Output is correct
13 Correct 122 ms 17964 KB Output is correct
14 Correct 221 ms 18028 KB Output is correct
15 Correct 107 ms 18156 KB Output is correct
16 Correct 184 ms 18028 KB Output is correct
17 Correct 159 ms 18156 KB Output is correct
18 Correct 75 ms 18156 KB Output is correct
19 Correct 84 ms 17900 KB Output is correct
20 Correct 91 ms 18028 KB Output is correct
21 Correct 128 ms 18028 KB Output is correct
22 Correct 226 ms 18092 KB Output is correct
23 Correct 94 ms 17900 KB Output is correct
24 Correct 195 ms 18028 KB Output is correct
25 Correct 161 ms 18048 KB Output is correct
26 Correct 73 ms 18028 KB Output is correct
27 Correct 87 ms 18172 KB Output is correct
28 Correct 89 ms 17900 KB Output is correct
29 Correct 116 ms 17900 KB Output is correct
30 Correct 185 ms 18028 KB Output is correct
31 Correct 95 ms 18032 KB Output is correct
32 Correct 194 ms 18036 KB Output is correct
33 Correct 158 ms 18028 KB Output is correct
34 Correct 83 ms 17900 KB Output is correct
35 Correct 139 ms 18028 KB Output is correct
36 Correct 137 ms 18028 KB Output is correct
37 Correct 134 ms 18156 KB Output is correct
38 Correct 138 ms 18028 KB Output is correct
39 Correct 139 ms 18028 KB Output is correct
40 Correct 138 ms 18028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 339 ms 4588 KB Output is correct
12 Correct 354 ms 4176 KB Output is correct
13 Correct 458 ms 3308 KB Output is correct
14 Correct 627 ms 3692 KB Output is correct
15 Correct 437 ms 4720 KB Output is correct
16 Correct 560 ms 3692 KB Output is correct
17 Correct 573 ms 3564 KB Output is correct
18 Correct 272 ms 5520 KB Output is correct
19 Correct 319 ms 2412 KB Output is correct
20 Correct 377 ms 4076 KB Output is correct
21 Correct 408 ms 4492 KB Output is correct
22 Correct 433 ms 4716 KB Output is correct
23 Correct 621 ms 3692 KB Output is correct
24 Correct 925 ms 3476 KB Output is correct
25 Correct 541 ms 5692 KB Output is correct
26 Correct 793 ms 4076 KB Output is correct
27 Correct 706 ms 4092 KB Output is correct
28 Correct 308 ms 6660 KB Output is correct
29 Correct 393 ms 2540 KB Output is correct
30 Correct 483 ms 4716 KB Output is correct
31 Correct 651 ms 4696 KB Output is correct
32 Correct 955 ms 4588 KB Output is correct
33 Correct 496 ms 3460 KB Output is correct
34 Correct 780 ms 3692 KB Output is correct
35 Correct 709 ms 4076 KB Output is correct
36 Correct 311 ms 2540 KB Output is correct
37 Correct 398 ms 4716 KB Output is correct
38 Correct 454 ms 2796 KB Output is correct
39 Correct 736 ms 3692 KB Output is correct
40 Correct 960 ms 3564 KB Output is correct
41 Correct 86 ms 18028 KB Output is correct
42 Correct 86 ms 18028 KB Output is correct
43 Correct 122 ms 17964 KB Output is correct
44 Correct 221 ms 18028 KB Output is correct
45 Correct 107 ms 18156 KB Output is correct
46 Correct 184 ms 18028 KB Output is correct
47 Correct 159 ms 18156 KB Output is correct
48 Correct 75 ms 18156 KB Output is correct
49 Correct 84 ms 17900 KB Output is correct
50 Correct 91 ms 18028 KB Output is correct
51 Correct 128 ms 18028 KB Output is correct
52 Correct 226 ms 18092 KB Output is correct
53 Correct 94 ms 17900 KB Output is correct
54 Correct 195 ms 18028 KB Output is correct
55 Correct 161 ms 18048 KB Output is correct
56 Correct 73 ms 18028 KB Output is correct
57 Correct 87 ms 18172 KB Output is correct
58 Correct 89 ms 17900 KB Output is correct
59 Correct 116 ms 17900 KB Output is correct
60 Correct 185 ms 18028 KB Output is correct
61 Correct 95 ms 18032 KB Output is correct
62 Correct 194 ms 18036 KB Output is correct
63 Correct 158 ms 18028 KB Output is correct
64 Correct 83 ms 17900 KB Output is correct
65 Correct 139 ms 18028 KB Output is correct
66 Correct 137 ms 18028 KB Output is correct
67 Correct 134 ms 18156 KB Output is correct
68 Correct 138 ms 18028 KB Output is correct
69 Correct 139 ms 18028 KB Output is correct
70 Correct 138 ms 18028 KB Output is correct
71 Correct 733 ms 22948 KB Output is correct
72 Correct 733 ms 23148 KB Output is correct
73 Correct 1408 ms 21740 KB Output is correct
74 Execution timed out 2091 ms 22508 KB Time limit exceeded
75 Halted 0 ms 0 KB -