Submission #331812

# Submission time Handle Problem Language Result Execution time Memory
331812 2020-11-30T08:21:38 Z daniel920712 Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 31336 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 Con1[1100005];
int Con2[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)];
        }
    }
    for(j=0;j<(1<<N);j++)
    {
        Con1[j]=con1[N%2][j];
        Con2[j]=con2[N%2][j];
    }

    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[y];
                    else ans-=Con1[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[y];
                    else ans-=Con2[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: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:24:36: warning: unused variable 'xx' [-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:46:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   46 |         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 4592 KB Output is correct
12 Correct 373 ms 4076 KB Output is correct
13 Correct 457 ms 3564 KB Output is correct
14 Correct 576 ms 3436 KB Output is correct
15 Correct 432 ms 4460 KB Output is correct
16 Correct 558 ms 3564 KB Output is correct
17 Correct 523 ms 3820 KB Output is correct
18 Correct 279 ms 5440 KB Output is correct
19 Correct 322 ms 2412 KB Output is correct
20 Correct 370 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 4592 KB Output is correct
12 Correct 373 ms 4076 KB Output is correct
13 Correct 457 ms 3564 KB Output is correct
14 Correct 576 ms 3436 KB Output is correct
15 Correct 432 ms 4460 KB Output is correct
16 Correct 558 ms 3564 KB Output is correct
17 Correct 523 ms 3820 KB Output is correct
18 Correct 279 ms 5440 KB Output is correct
19 Correct 322 ms 2412 KB Output is correct
20 Correct 370 ms 4076 KB Output is correct
21 Correct 419 ms 4716 KB Output is correct
22 Correct 426 ms 4844 KB Output is correct
23 Correct 611 ms 3820 KB Output is correct
24 Correct 922 ms 3692 KB Output is correct
25 Correct 510 ms 5608 KB Output is correct
26 Correct 800 ms 4204 KB Output is correct
27 Correct 698 ms 4076 KB Output is correct
28 Correct 297 ms 6636 KB Output is correct
29 Correct 394 ms 2584 KB Output is correct
30 Correct 447 ms 4844 KB Output is correct
31 Correct 634 ms 4588 KB Output is correct
32 Correct 926 ms 4588 KB Output is correct
33 Correct 507 ms 3692 KB Output is correct
34 Correct 798 ms 3772 KB Output is correct
35 Correct 706 ms 4076 KB Output is correct
36 Correct 322 ms 2848 KB Output is correct
37 Correct 386 ms 4716 KB Output is correct
38 Correct 423 ms 2668 KB Output is correct
39 Correct 623 ms 3948 KB Output is correct
40 Correct 899 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 90 ms 26220 KB Output is correct
12 Correct 95 ms 26220 KB Output is correct
13 Correct 123 ms 26348 KB Output is correct
14 Correct 203 ms 26220 KB Output is correct
15 Correct 104 ms 26348 KB Output is correct
16 Correct 192 ms 26348 KB Output is correct
17 Correct 157 ms 26236 KB Output is correct
18 Correct 77 ms 26348 KB Output is correct
19 Correct 90 ms 26092 KB Output is correct
20 Correct 95 ms 26220 KB Output is correct
21 Correct 126 ms 26220 KB Output is correct
22 Correct 212 ms 26348 KB Output is correct
23 Correct 96 ms 26092 KB Output is correct
24 Correct 186 ms 26220 KB Output is correct
25 Correct 154 ms 26220 KB Output is correct
26 Correct 78 ms 26092 KB Output is correct
27 Correct 89 ms 26220 KB Output is correct
28 Correct 90 ms 26092 KB Output is correct
29 Correct 120 ms 26220 KB Output is correct
30 Correct 186 ms 26348 KB Output is correct
31 Correct 95 ms 26092 KB Output is correct
32 Correct 186 ms 26220 KB Output is correct
33 Correct 153 ms 26220 KB Output is correct
34 Correct 78 ms 26092 KB Output is correct
35 Correct 132 ms 26220 KB Output is correct
36 Correct 134 ms 26220 KB Output is correct
37 Correct 134 ms 26348 KB Output is correct
38 Correct 137 ms 26348 KB Output is correct
39 Correct 138 ms 26348 KB Output is correct
40 Correct 134 ms 26356 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 4592 KB Output is correct
12 Correct 373 ms 4076 KB Output is correct
13 Correct 457 ms 3564 KB Output is correct
14 Correct 576 ms 3436 KB Output is correct
15 Correct 432 ms 4460 KB Output is correct
16 Correct 558 ms 3564 KB Output is correct
17 Correct 523 ms 3820 KB Output is correct
18 Correct 279 ms 5440 KB Output is correct
19 Correct 322 ms 2412 KB Output is correct
20 Correct 370 ms 4076 KB Output is correct
21 Correct 419 ms 4716 KB Output is correct
22 Correct 426 ms 4844 KB Output is correct
23 Correct 611 ms 3820 KB Output is correct
24 Correct 922 ms 3692 KB Output is correct
25 Correct 510 ms 5608 KB Output is correct
26 Correct 800 ms 4204 KB Output is correct
27 Correct 698 ms 4076 KB Output is correct
28 Correct 297 ms 6636 KB Output is correct
29 Correct 394 ms 2584 KB Output is correct
30 Correct 447 ms 4844 KB Output is correct
31 Correct 634 ms 4588 KB Output is correct
32 Correct 926 ms 4588 KB Output is correct
33 Correct 507 ms 3692 KB Output is correct
34 Correct 798 ms 3772 KB Output is correct
35 Correct 706 ms 4076 KB Output is correct
36 Correct 322 ms 2848 KB Output is correct
37 Correct 386 ms 4716 KB Output is correct
38 Correct 423 ms 2668 KB Output is correct
39 Correct 623 ms 3948 KB Output is correct
40 Correct 899 ms 4076 KB Output is correct
41 Correct 90 ms 26220 KB Output is correct
42 Correct 95 ms 26220 KB Output is correct
43 Correct 123 ms 26348 KB Output is correct
44 Correct 203 ms 26220 KB Output is correct
45 Correct 104 ms 26348 KB Output is correct
46 Correct 192 ms 26348 KB Output is correct
47 Correct 157 ms 26236 KB Output is correct
48 Correct 77 ms 26348 KB Output is correct
49 Correct 90 ms 26092 KB Output is correct
50 Correct 95 ms 26220 KB Output is correct
51 Correct 126 ms 26220 KB Output is correct
52 Correct 212 ms 26348 KB Output is correct
53 Correct 96 ms 26092 KB Output is correct
54 Correct 186 ms 26220 KB Output is correct
55 Correct 154 ms 26220 KB Output is correct
56 Correct 78 ms 26092 KB Output is correct
57 Correct 89 ms 26220 KB Output is correct
58 Correct 90 ms 26092 KB Output is correct
59 Correct 120 ms 26220 KB Output is correct
60 Correct 186 ms 26348 KB Output is correct
61 Correct 95 ms 26092 KB Output is correct
62 Correct 186 ms 26220 KB Output is correct
63 Correct 153 ms 26220 KB Output is correct
64 Correct 78 ms 26092 KB Output is correct
65 Correct 132 ms 26220 KB Output is correct
66 Correct 134 ms 26220 KB Output is correct
67 Correct 134 ms 26348 KB Output is correct
68 Correct 137 ms 26348 KB Output is correct
69 Correct 138 ms 26348 KB Output is correct
70 Correct 134 ms 26356 KB Output is correct
71 Correct 714 ms 30956 KB Output is correct
72 Correct 703 ms 31336 KB Output is correct
73 Correct 1284 ms 29676 KB Output is correct
74 Execution timed out 2053 ms 29164 KB Time limit exceeded
75 Halted 0 ms 0 KB -