Submission #331809

# Submission time Handle Problem Language Result Execution time Memory
331809 2020-11-30T08:18:19 Z daniel920712 Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 23096 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()
{
    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;
                }

                BFS.push(make_pair(make_pair(x+1,y+(1<<(N-xxx[x]-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;
                }

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

            }
        }
        else if(c<=b&&c<=a)
        {

            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;
                }
                
                BFS.push(make_pair(make_pair(x+1,y+(1<<(N-xxx[x]-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:22:15: warning: unused variable 'ok' [-Wunused-variable]
   22 |     int M,i,j,ok,a,b,c,x,y,z,con=0,xx;
      |               ^~
snake_escaping.cpp:22:36: warning: unused variable 'xx' [-Wunused-variable]
   22 |     int M,i,j,ok,a,b,c,x,y,z,con=0,xx;
      |                                    ^~
snake_escaping.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   23 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   24 |     scanf("%s",all);
      |     ~~~~~^~~~~~~~~~
snake_escaping.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |         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 347 ms 4460 KB Output is correct
12 Correct 354 ms 4204 KB Output is correct
13 Correct 461 ms 3352 KB Output is correct
14 Correct 576 ms 3436 KB Output is correct
15 Correct 455 ms 4548 KB Output is correct
16 Correct 565 ms 3536 KB Output is correct
17 Correct 542 ms 3564 KB Output is correct
18 Correct 310 ms 5428 KB Output is correct
19 Correct 317 ms 2412 KB Output is correct
20 Correct 398 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 347 ms 4460 KB Output is correct
12 Correct 354 ms 4204 KB Output is correct
13 Correct 461 ms 3352 KB Output is correct
14 Correct 576 ms 3436 KB Output is correct
15 Correct 455 ms 4548 KB Output is correct
16 Correct 565 ms 3536 KB Output is correct
17 Correct 542 ms 3564 KB Output is correct
18 Correct 310 ms 5428 KB Output is correct
19 Correct 317 ms 2412 KB Output is correct
20 Correct 398 ms 4076 KB Output is correct
21 Correct 405 ms 4844 KB Output is correct
22 Correct 424 ms 4716 KB Output is correct
23 Correct 628 ms 3820 KB Output is correct
24 Correct 935 ms 3692 KB Output is correct
25 Correct 527 ms 5484 KB Output is correct
26 Correct 800 ms 4368 KB Output is correct
27 Correct 722 ms 3948 KB Output is correct
28 Correct 303 ms 6684 KB Output is correct
29 Correct 393 ms 2540 KB Output is correct
30 Correct 451 ms 4716 KB Output is correct
31 Correct 700 ms 4716 KB Output is correct
32 Correct 921 ms 4588 KB Output is correct
33 Correct 501 ms 3436 KB Output is correct
34 Correct 782 ms 3564 KB Output is correct
35 Correct 701 ms 4076 KB Output is correct
36 Correct 311 ms 2540 KB Output is correct
37 Correct 388 ms 4460 KB Output is correct
38 Correct 429 ms 2668 KB Output is correct
39 Correct 622 ms 3820 KB Output is correct
40 Correct 923 ms 3692 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 88 ms 18028 KB Output is correct
13 Correct 118 ms 17900 KB Output is correct
14 Correct 218 ms 18028 KB Output is correct
15 Correct 99 ms 18156 KB Output is correct
16 Correct 189 ms 18028 KB Output is correct
17 Correct 175 ms 18028 KB Output is correct
18 Correct 75 ms 18156 KB Output is correct
19 Correct 83 ms 18028 KB Output is correct
20 Correct 91 ms 18108 KB Output is correct
21 Correct 126 ms 18028 KB Output is correct
22 Correct 226 ms 18028 KB Output is correct
23 Correct 93 ms 17900 KB Output is correct
24 Correct 184 ms 18028 KB Output is correct
25 Correct 155 ms 18028 KB Output is correct
26 Correct 74 ms 17900 KB Output is correct
27 Correct 85 ms 18092 KB Output is correct
28 Correct 103 ms 17900 KB Output is correct
29 Correct 116 ms 17900 KB Output is correct
30 Correct 183 ms 18028 KB Output is correct
31 Correct 93 ms 17900 KB Output is correct
32 Correct 195 ms 18028 KB Output is correct
33 Correct 159 ms 18028 KB Output is correct
34 Correct 72 ms 17900 KB Output is correct
35 Correct 132 ms 18028 KB Output is correct
36 Correct 137 ms 18028 KB Output is correct
37 Correct 157 ms 18028 KB Output is correct
38 Correct 146 ms 18104 KB Output is correct
39 Correct 146 ms 18028 KB Output is correct
40 Correct 132 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 347 ms 4460 KB Output is correct
12 Correct 354 ms 4204 KB Output is correct
13 Correct 461 ms 3352 KB Output is correct
14 Correct 576 ms 3436 KB Output is correct
15 Correct 455 ms 4548 KB Output is correct
16 Correct 565 ms 3536 KB Output is correct
17 Correct 542 ms 3564 KB Output is correct
18 Correct 310 ms 5428 KB Output is correct
19 Correct 317 ms 2412 KB Output is correct
20 Correct 398 ms 4076 KB Output is correct
21 Correct 405 ms 4844 KB Output is correct
22 Correct 424 ms 4716 KB Output is correct
23 Correct 628 ms 3820 KB Output is correct
24 Correct 935 ms 3692 KB Output is correct
25 Correct 527 ms 5484 KB Output is correct
26 Correct 800 ms 4368 KB Output is correct
27 Correct 722 ms 3948 KB Output is correct
28 Correct 303 ms 6684 KB Output is correct
29 Correct 393 ms 2540 KB Output is correct
30 Correct 451 ms 4716 KB Output is correct
31 Correct 700 ms 4716 KB Output is correct
32 Correct 921 ms 4588 KB Output is correct
33 Correct 501 ms 3436 KB Output is correct
34 Correct 782 ms 3564 KB Output is correct
35 Correct 701 ms 4076 KB Output is correct
36 Correct 311 ms 2540 KB Output is correct
37 Correct 388 ms 4460 KB Output is correct
38 Correct 429 ms 2668 KB Output is correct
39 Correct 622 ms 3820 KB Output is correct
40 Correct 923 ms 3692 KB Output is correct
41 Correct 86 ms 18028 KB Output is correct
42 Correct 88 ms 18028 KB Output is correct
43 Correct 118 ms 17900 KB Output is correct
44 Correct 218 ms 18028 KB Output is correct
45 Correct 99 ms 18156 KB Output is correct
46 Correct 189 ms 18028 KB Output is correct
47 Correct 175 ms 18028 KB Output is correct
48 Correct 75 ms 18156 KB Output is correct
49 Correct 83 ms 18028 KB Output is correct
50 Correct 91 ms 18108 KB Output is correct
51 Correct 126 ms 18028 KB Output is correct
52 Correct 226 ms 18028 KB Output is correct
53 Correct 93 ms 17900 KB Output is correct
54 Correct 184 ms 18028 KB Output is correct
55 Correct 155 ms 18028 KB Output is correct
56 Correct 74 ms 17900 KB Output is correct
57 Correct 85 ms 18092 KB Output is correct
58 Correct 103 ms 17900 KB Output is correct
59 Correct 116 ms 17900 KB Output is correct
60 Correct 183 ms 18028 KB Output is correct
61 Correct 93 ms 17900 KB Output is correct
62 Correct 195 ms 18028 KB Output is correct
63 Correct 159 ms 18028 KB Output is correct
64 Correct 72 ms 17900 KB Output is correct
65 Correct 132 ms 18028 KB Output is correct
66 Correct 137 ms 18028 KB Output is correct
67 Correct 157 ms 18028 KB Output is correct
68 Correct 146 ms 18104 KB Output is correct
69 Correct 146 ms 18028 KB Output is correct
70 Correct 132 ms 18028 KB Output is correct
71 Correct 730 ms 22828 KB Output is correct
72 Correct 734 ms 23096 KB Output is correct
73 Correct 1402 ms 21600 KB Output is correct
74 Execution timed out 2061 ms 20160 KB Time limit exceeded
75 Halted 0 ms 0 KB -