답안 #331797

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331797 2020-11-30T07:07:27 Z daniel920712 Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 26348 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;
void F(int x,int y,int z)
{
    if(x==N)
    {
        if(z%2==0) ans+=con1[N%2][y];
        else ans-=con1[N%2][y];
        return;
    }
    if(tt[x]=='1')
    {
        F(x+1,y+(1<<(N-x-1)),z);
        F(x+1,y,z+1);
    }
    else if(tt[x]=='0') F(x+1,y,z);
    else F(x+1,y+(1<<(N-x-1)),z);

}
void F2(int x,int y)
{
    if(x==N)
    {
        ans+=(all[y]-'0');
        return;
    }
    if(tt[x]=='?')
    {
        F2(x+1,y);
        F2(x+1,y+(1<<(N-x-1)));
    }
    else F2(x+1,y+(1<<(N-x-1))*(tt[x]-'0'));
}
void F3(int x,int y,int z)
{
    if(x==N)
    {
        if(z%2==0) ans+=con2[N%2][y];
        else ans-=con2[N%2][y];
        return;
    }
    if(tt[x]=='0')
    {
        F3(x+1,y+(1<<(N-x-1)),z+1);
        F3(x+1,y,z);
    }
    else if(tt[x]=='1') F3(x+1,y+(1<<(N-x-1)),z);
    else F3(x+1,y,z);

}
int main()
{
    //freopen("05-05.txt","rt",stdin);
    //freopen("tt.txt","w+t",stdout);
    int M,i,j,ok,a,b,c,x,y,z;
    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++;
        }
        if(b<=a&&b<=c)
        {
            BFS.push(make_pair(make_pair(0,0),0));
            while(!BFS.empty())
            {
                x=BFS.front().first.first;
                y=BFS.front().first.second;
                z=BFS.front().second;
                BFS.pop();
                if(x==N)
                {
                    if(z==0) ans+=con1[N%2][y];
                    else ans-=con1[N%2][y];
                    continue;
                }
                if(tt[x]=='1')
                {
                    BFS.push(make_pair(make_pair(x+1,y+(1<<(N-x-1))),z));
                    BFS.push(make_pair(make_pair(x+1,y),z^1));
                }
                else if(tt[x]=='0') BFS.push(make_pair(make_pair(x+1,y),z));
                else BFS.push(make_pair(make_pair(x+1,y+(1<<(N-x-1))),z));
            }
        }
        else if(a<=b&&a<=c) F2(0,0);
        else if(c<=b&&c<=a) F3(0,0,0);
        printf("%d\n",ans);

    }

	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:71:15: warning: unused variable 'ok' [-Wunused-variable]
   71 |     int M,i,j,ok,a,b,c,x,y,z;
      |               ^~
snake_escaping.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   72 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |     scanf("%s",all);
      |     ~~~~~^~~~~~~~~~
snake_escaping.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   90 |         scanf("%s",tt);
      |         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 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 2 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 2 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
# 결과 실행 시간 메모리 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 2 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 2 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 336 ms 6124 KB Output is correct
12 Correct 477 ms 5228 KB Output is correct
13 Correct 500 ms 4332 KB Output is correct
14 Correct 858 ms 4480 KB Output is correct
15 Correct 560 ms 5356 KB Output is correct
16 Correct 673 ms 4588 KB Output is correct
17 Correct 578 ms 4536 KB Output is correct
18 Correct 407 ms 6396 KB Output is correct
19 Correct 307 ms 3436 KB Output is correct
20 Correct 385 ms 5112 KB Output is correct
# 결과 실행 시간 메모리 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 2 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 2 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 336 ms 6124 KB Output is correct
12 Correct 477 ms 5228 KB Output is correct
13 Correct 500 ms 4332 KB Output is correct
14 Correct 858 ms 4480 KB Output is correct
15 Correct 560 ms 5356 KB Output is correct
16 Correct 673 ms 4588 KB Output is correct
17 Correct 578 ms 4536 KB Output is correct
18 Correct 407 ms 6396 KB Output is correct
19 Correct 307 ms 3436 KB Output is correct
20 Correct 385 ms 5112 KB Output is correct
21 Correct 386 ms 5664 KB Output is correct
22 Correct 574 ms 5612 KB Output is correct
23 Correct 722 ms 4844 KB Output is correct
24 Correct 1506 ms 4812 KB Output is correct
25 Correct 622 ms 6508 KB Output is correct
26 Correct 965 ms 5096 KB Output is correct
27 Correct 843 ms 5272 KB Output is correct
28 Correct 466 ms 7612 KB Output is correct
29 Correct 319 ms 3564 KB Output is correct
30 Correct 433 ms 5696 KB Output is correct
31 Correct 823 ms 5740 KB Output is correct
32 Correct 1582 ms 5824 KB Output is correct
33 Correct 441 ms 4676 KB Output is correct
34 Correct 997 ms 4716 KB Output is correct
35 Correct 819 ms 4992 KB Output is correct
36 Correct 297 ms 3480 KB Output is correct
37 Correct 528 ms 5612 KB Output is correct
38 Correct 336 ms 3564 KB Output is correct
39 Correct 558 ms 4744 KB Output is correct
40 Correct 670 ms 4588 KB Output is correct
# 결과 실행 시간 메모리 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 2 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 2 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 84 ms 19180 KB Output is correct
12 Correct 101 ms 19308 KB Output is correct
13 Correct 157 ms 19180 KB Output is correct
14 Correct 369 ms 19260 KB Output is correct
15 Correct 125 ms 19308 KB Output is correct
16 Correct 232 ms 19188 KB Output is correct
17 Correct 199 ms 19180 KB Output is correct
18 Correct 102 ms 19436 KB Output is correct
19 Correct 80 ms 19052 KB Output is correct
20 Correct 87 ms 19180 KB Output is correct
21 Correct 173 ms 19180 KB Output is correct
22 Correct 189 ms 19180 KB Output is correct
23 Correct 92 ms 19180 KB Output is correct
24 Correct 220 ms 19180 KB Output is correct
25 Correct 191 ms 19180 KB Output is correct
26 Correct 75 ms 19048 KB Output is correct
27 Correct 97 ms 19292 KB Output is correct
28 Correct 78 ms 19052 KB Output is correct
29 Correct 109 ms 19180 KB Output is correct
30 Correct 140 ms 19180 KB Output is correct
31 Correct 105 ms 19180 KB Output is correct
32 Correct 291 ms 19168 KB Output is correct
33 Correct 206 ms 19308 KB Output is correct
34 Correct 86 ms 19052 KB Output is correct
35 Correct 155 ms 19180 KB Output is correct
36 Correct 159 ms 19308 KB Output is correct
37 Correct 185 ms 19180 KB Output is correct
38 Correct 163 ms 19180 KB Output is correct
39 Correct 159 ms 19308 KB Output is correct
40 Correct 154 ms 19308 KB Output is correct
# 결과 실행 시간 메모리 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 2 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 2 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 336 ms 6124 KB Output is correct
12 Correct 477 ms 5228 KB Output is correct
13 Correct 500 ms 4332 KB Output is correct
14 Correct 858 ms 4480 KB Output is correct
15 Correct 560 ms 5356 KB Output is correct
16 Correct 673 ms 4588 KB Output is correct
17 Correct 578 ms 4536 KB Output is correct
18 Correct 407 ms 6396 KB Output is correct
19 Correct 307 ms 3436 KB Output is correct
20 Correct 385 ms 5112 KB Output is correct
21 Correct 386 ms 5664 KB Output is correct
22 Correct 574 ms 5612 KB Output is correct
23 Correct 722 ms 4844 KB Output is correct
24 Correct 1506 ms 4812 KB Output is correct
25 Correct 622 ms 6508 KB Output is correct
26 Correct 965 ms 5096 KB Output is correct
27 Correct 843 ms 5272 KB Output is correct
28 Correct 466 ms 7612 KB Output is correct
29 Correct 319 ms 3564 KB Output is correct
30 Correct 433 ms 5696 KB Output is correct
31 Correct 823 ms 5740 KB Output is correct
32 Correct 1582 ms 5824 KB Output is correct
33 Correct 441 ms 4676 KB Output is correct
34 Correct 997 ms 4716 KB Output is correct
35 Correct 819 ms 4992 KB Output is correct
36 Correct 297 ms 3480 KB Output is correct
37 Correct 528 ms 5612 KB Output is correct
38 Correct 336 ms 3564 KB Output is correct
39 Correct 558 ms 4744 KB Output is correct
40 Correct 670 ms 4588 KB Output is correct
41 Correct 84 ms 19180 KB Output is correct
42 Correct 101 ms 19308 KB Output is correct
43 Correct 157 ms 19180 KB Output is correct
44 Correct 369 ms 19260 KB Output is correct
45 Correct 125 ms 19308 KB Output is correct
46 Correct 232 ms 19188 KB Output is correct
47 Correct 199 ms 19180 KB Output is correct
48 Correct 102 ms 19436 KB Output is correct
49 Correct 80 ms 19052 KB Output is correct
50 Correct 87 ms 19180 KB Output is correct
51 Correct 173 ms 19180 KB Output is correct
52 Correct 189 ms 19180 KB Output is correct
53 Correct 92 ms 19180 KB Output is correct
54 Correct 220 ms 19180 KB Output is correct
55 Correct 191 ms 19180 KB Output is correct
56 Correct 75 ms 19048 KB Output is correct
57 Correct 97 ms 19292 KB Output is correct
58 Correct 78 ms 19052 KB Output is correct
59 Correct 109 ms 19180 KB Output is correct
60 Correct 140 ms 19180 KB Output is correct
61 Correct 105 ms 19180 KB Output is correct
62 Correct 291 ms 19168 KB Output is correct
63 Correct 206 ms 19308 KB Output is correct
64 Correct 86 ms 19052 KB Output is correct
65 Correct 155 ms 19180 KB Output is correct
66 Correct 159 ms 19308 KB Output is correct
67 Correct 185 ms 19180 KB Output is correct
68 Correct 163 ms 19180 KB Output is correct
69 Correct 159 ms 19308 KB Output is correct
70 Correct 154 ms 19308 KB Output is correct
71 Correct 685 ms 26348 KB Output is correct
72 Correct 954 ms 25324 KB Output is correct
73 Execution timed out 2090 ms 23960 KB Time limit exceeded
74 Halted 0 ms 0 KB -