답안 #376194

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376194 2021-03-11T03:59:28 Z daniel920712 Lamps (JOI19_lamps) C++14
100 / 100
326 ms 129900 KB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <time.h>

using namespace std;
char a[1000005];
char b[1000005];
bool have[15][1000005]={0};
int DP[15][1000005],N;
bool vis[1000005]={0};
int Father[1000005];
queue < pair < int , int > > BFS;
int F(int what,int here)
{
    if(here==N) return 0;
    if(have[what][here]) return DP[what][here];
    DP[what][here]=2e9;
    have[what][here]=1;
    if(a[here]==b[here]) DP[what][here]=min(DP[what][here],F(0,here+1));
    if(a[here]!=b[here]) DP[what][here]=min(DP[what][here],F(1,here+1)+1);
    if(b[here]=='1')
    {
        DP[what][here]=min(DP[what][here],F(3,here+1)+2);
        DP[what][here]=min(DP[what][here],F(5,here+1)+1);
        DP[what][here]=min(DP[what][here],F(6,here+1)+2);
        DP[what][here]=min(DP[what][here],F(9,here+1)+2);
    }
    else
    {
        DP[what][here]=min(DP[what][here],F(2,here+1)+2);
        DP[what][here]=min(DP[what][here],F(4,here+1)+1);
        DP[what][here]=min(DP[what][here],F(7,here+1)+2);
        DP[what][here]=min(DP[what][here],F(8,here+1)+2);
    }
    if(what==1)
    {
        if(a[here]!=b[here]) DP[what][here]=min(DP[what][here],F(1,here+1));
        else
        {
            if(b[here]=='1')
            {
                DP[what][here]=min(DP[what][here],F(6,here+1)+1);
                DP[what][here]=min(DP[what][here],F(3,here+1)+1);
            }
            else
            {
                DP[what][here]=min(DP[what][here],F(7,here+1)+1);
                DP[what][here]=min(DP[what][here],F(2,here+1)+1);
            }
        }
    }
    if(what==2)
    {
        if(b[here]=='1')
        {
            DP[what][here]=min(DP[what][here],F(5,here+1));
            DP[what][here]=min(DP[what][here],F(6,here+1)+1);
            DP[what][here]=min(DP[what][here],F(3,here+1)+1);
        }
        else DP[what][here]=min(DP[what][here],F(2,here+1));

        if(a[here]!=b[here]) DP[what][here]=min(DP[what][here],F(1,here+1));
    }
    if(what==3)
    {
        if(b[here]=='0')
        {
            DP[what][here]=min(DP[what][here],F(4,here+1));
            DP[what][here]=min(DP[what][here],F(7,here+1)+1);
            DP[what][here]=min(DP[what][here],F(2,here+1)+1);
        }
        else DP[what][here]=min(DP[what][here],F(3,here+1));

        if(a[here]!=b[here]) DP[what][here]=min(DP[what][here],F(1,here+1));
    }
    if(what==4)
    {
        if(b[here]=='1')
        {
            DP[what][here]=min(DP[what][here],F(3,here+1)+1);
            DP[what][here]=min(DP[what][here],F(9,here+1)+1);
        }
        else
        {
            DP[what][here]=min(DP[what][here],F(4,here+1));
            DP[what][here]=min(DP[what][here],F(7,here+1)+1);
        }
    }
    if(what==5)
    {
        if(b[here]=='0')
        {
            DP[what][here]=min(DP[what][here],F(2,here+1)+1);
            DP[what][here]=min(DP[what][here],F(8,here+1)+1);
        }
        else
        {
            DP[what][here]=min(DP[what][here],F(5,here+1));
            DP[what][here]=min(DP[what][here],F(6,here+1)+1);
        }
    }
    if(what==6)
    {
        if(b[here]=='0')
        {
            DP[what][here]=min(DP[what][here],F(8,here+1)+1);
            DP[what][here]=min(DP[what][here],F(2,here+1)+1);

        }
        else DP[what][here]=min(DP[what][here],F(6,here+1));

        if(a[here]!=b[here]) DP[what][here]=min(DP[what][here],F(1,here+1));
    }
    if(what==7)
    {
        if(b[here]=='1')
        {
            DP[what][here]=min(DP[what][here],F(9,here+1)+1);
            DP[what][here]=min(DP[what][here],F(3,here+1)+1);
        }
        else DP[what][here]=min(DP[what][here],F(7,here+1));

        if(a[here]!=b[here]) DP[what][here]=min(DP[what][here],F(1,here+1));
    }
    if(what==8)
    {
        if(b[here]=='1')
        {
            DP[what][here]=min(DP[what][here],F(9,here+1)+1);
            DP[what][here]=min(DP[what][here],F(3,here+1)+1);
            DP[what][here]=min(DP[what][here],F(6,here+1)+1);
            DP[what][here]=min(DP[what][here],F(5,here+1));
        }
        else
        {
            DP[what][here]=min(DP[what][here],F(8,here+1));
            DP[what][here]=min(DP[what][here],F(7,here+1)+1);
        }
    }
    if(what==9)
    {
        if(b[here]=='0')
        {
            DP[what][here]=min(DP[what][here],F(8,here+1)+1);
            DP[what][here]=min(DP[what][here],F(2,here+1)+1);
            DP[what][here]=min(DP[what][here],F(7,here+1)+1);
            DP[what][here]=min(DP[what][here],F(4,here+1));
        }
        else
        {
            DP[what][here]=min(DP[what][here],F(9,here+1));
            DP[what][here]=min(DP[what][here],F(6,here+1)+1);
        }
    }
    return DP[what][here];
}
int main()
{
    int M,ans=0,i,j,x=0,y=0,aa,tt,st;
    scanf("%d",&N);
    scanf("%s %s",a,b);
    /*srand(time(NULL));
    //N=15;
    x=y=0;
    for(i=0;i<N;i++)
    {
        //a[i]=rand()%2+'0';
        //b[i]=rand()%2+'0';
        x*=2;
        x+=a[i]-'0';
        y*=2;
        y+=b[i]-'0';
    }
    st=x;
    printf("%s\n%s\n",a,b);

    BFS.push(make_pair(x,0));
    while(!BFS.empty())
    {
        x=BFS.front().first;
        aa=BFS.front().second;
        BFS.pop();
        //if(vis[x]) continue;
        vis[x]=1;
        //printf("%d %d %d\n",x,y,aa);
        if(x==y)
        {
            printf("%d %d\n",aa,F(0,0));
            while(y!=st)
            {
                for(i=N-1;i>=0;i--)
                {
                    if(y&(1<<i)) printf("1");
                    else printf("0");
                }
                printf("\n");
                y=Father[y];
            }
            for(i=N-1;i>=0;i--)
            {
                if(y&(1<<i)) printf("1");
                else printf("0");
            }
            printf("\n");

            if(F(0,0)>aa) while(1);

            return 0;
        }
        tt=0;
        for(i=0;i<N;i++)
        {
            tt=x;
            for(j=i;j<N;j++)
            {
                if(tt&(1<<j)) tt-=1<<j;
                //printf("vv %d %d\n",tt,vis[tt]);
                if(!vis[tt])
                {
                    vis[tt]=1;
                    Father[tt]=x;
                    BFS.push(make_pair(tt,aa+1));
                }
            }
        }

        for(i=0;i<N;i++)
        {
            tt=x;
            for(j=i;j<N;j++)
            {
                if(!(tt&(1<<j))) tt+=1<<j;
                if(!vis[tt])
                {
                    vis[tt]=1;
                    Father[tt]=x;
                    BFS.push(make_pair(tt,aa+1));
                }
            }
        }

        for(i=0;i<N;i++)
        {
            tt=x;
            for(j=i;j<N;j++)
            {
                if(!(tt&(1<<j))) tt+=1<<j;
                else tt-=1<<j;
                if(!vis[tt])
                {
                    vis[tt]=1;
                    Father[tt]=x;
                    BFS.push(make_pair(tt,aa+1));
                }
            }
        }


    }
    //printf("%d\n",F(0));*/
    printf("%d\n",F(0,0));
    return 0;
}
/*
15
011011010101111
101111100001011
*/

Compilation message

lamp.cpp: In function 'int main()':
lamp.cpp:161:9: warning: unused variable 'M' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |         ^
lamp.cpp:161:11: warning: unused variable 'ans' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |           ^~~
lamp.cpp:161:17: warning: unused variable 'i' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |                 ^
lamp.cpp:161:19: warning: unused variable 'j' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |                   ^
lamp.cpp:161:21: warning: unused variable 'x' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |                     ^
lamp.cpp:161:25: warning: unused variable 'y' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |                         ^
lamp.cpp:161:29: warning: unused variable 'aa' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |                             ^~
lamp.cpp:161:32: warning: unused variable 'tt' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |                                ^~
lamp.cpp:161:35: warning: unused variable 'st' [-Wunused-variable]
  161 |     int M,ans=0,i,j,x=0,y=0,aa,tt,st;
      |                                   ^~
lamp.cpp:162:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  162 |     scanf("%d",&N);
      |     ~~~~~^~~~~~~~~
lamp.cpp:163:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  163 |     scanf("%s %s",a,b);
      |     ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 384 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 492 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 384 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 492 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
35 Correct 1 ms 748 KB Output is correct
36 Correct 1 ms 748 KB Output is correct
37 Correct 1 ms 768 KB Output is correct
38 Correct 1 ms 748 KB Output is correct
39 Correct 1 ms 748 KB Output is correct
40 Correct 1 ms 748 KB Output is correct
41 Correct 1 ms 764 KB Output is correct
42 Correct 1 ms 748 KB Output is correct
43 Correct 1 ms 748 KB Output is correct
44 Correct 2 ms 748 KB Output is correct
45 Correct 1 ms 748 KB Output is correct
46 Correct 1 ms 748 KB Output is correct
47 Correct 1 ms 748 KB Output is correct
48 Correct 1 ms 748 KB Output is correct
49 Correct 1 ms 748 KB Output is correct
50 Correct 1 ms 748 KB Output is correct
51 Correct 1 ms 748 KB Output is correct
52 Correct 1 ms 748 KB Output is correct
53 Correct 1 ms 748 KB Output is correct
54 Correct 1 ms 748 KB Output is correct
55 Correct 1 ms 748 KB Output is correct
56 Correct 1 ms 748 KB Output is correct
57 Correct 2 ms 748 KB Output is correct
58 Correct 1 ms 748 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 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 225 ms 105068 KB Output is correct
8 Correct 267 ms 129512 KB Output is correct
9 Correct 265 ms 129456 KB Output is correct
10 Correct 262 ms 129516 KB Output is correct
11 Correct 262 ms 129516 KB Output is correct
12 Correct 237 ms 105068 KB Output is correct
13 Correct 244 ms 111340 KB Output is correct
14 Correct 246 ms 129516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 384 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 492 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
35 Correct 1 ms 748 KB Output is correct
36 Correct 1 ms 748 KB Output is correct
37 Correct 1 ms 768 KB Output is correct
38 Correct 1 ms 748 KB Output is correct
39 Correct 1 ms 748 KB Output is correct
40 Correct 1 ms 748 KB Output is correct
41 Correct 1 ms 764 KB Output is correct
42 Correct 1 ms 748 KB Output is correct
43 Correct 1 ms 748 KB Output is correct
44 Correct 2 ms 748 KB Output is correct
45 Correct 1 ms 748 KB Output is correct
46 Correct 1 ms 748 KB Output is correct
47 Correct 1 ms 748 KB Output is correct
48 Correct 1 ms 748 KB Output is correct
49 Correct 1 ms 748 KB Output is correct
50 Correct 1 ms 748 KB Output is correct
51 Correct 1 ms 748 KB Output is correct
52 Correct 1 ms 748 KB Output is correct
53 Correct 1 ms 748 KB Output is correct
54 Correct 1 ms 748 KB Output is correct
55 Correct 1 ms 748 KB Output is correct
56 Correct 1 ms 748 KB Output is correct
57 Correct 2 ms 748 KB Output is correct
58 Correct 1 ms 748 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1 ms 364 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 225 ms 105068 KB Output is correct
66 Correct 267 ms 129512 KB Output is correct
67 Correct 265 ms 129456 KB Output is correct
68 Correct 262 ms 129516 KB Output is correct
69 Correct 262 ms 129516 KB Output is correct
70 Correct 237 ms 105068 KB Output is correct
71 Correct 244 ms 111340 KB Output is correct
72 Correct 246 ms 129516 KB Output is correct
73 Correct 234 ms 109932 KB Output is correct
74 Correct 260 ms 124780 KB Output is correct
75 Correct 264 ms 124908 KB Output is correct
76 Correct 271 ms 124780 KB Output is correct
77 Correct 262 ms 129644 KB Output is correct
78 Correct 281 ms 129704 KB Output is correct
79 Correct 258 ms 124780 KB Output is correct
80 Correct 266 ms 124780 KB Output is correct
81 Correct 266 ms 129644 KB Output is correct
82 Correct 262 ms 129900 KB Output is correct
83 Correct 283 ms 129644 KB Output is correct
84 Correct 262 ms 129644 KB Output is correct
85 Correct 263 ms 129644 KB Output is correct
86 Correct 268 ms 129644 KB Output is correct
87 Correct 264 ms 129644 KB Output is correct
88 Correct 267 ms 129644 KB Output is correct
89 Correct 263 ms 129644 KB Output is correct
90 Correct 271 ms 129644 KB Output is correct
91 Correct 271 ms 129644 KB Output is correct
92 Correct 274 ms 129788 KB Output is correct
93 Correct 285 ms 129644 KB Output is correct
94 Correct 278 ms 129644 KB Output is correct
95 Correct 280 ms 129644 KB Output is correct
96 Correct 284 ms 129772 KB Output is correct
97 Correct 277 ms 129644 KB Output is correct
98 Correct 285 ms 129676 KB Output is correct
99 Correct 276 ms 129644 KB Output is correct
100 Correct 285 ms 129644 KB Output is correct
101 Correct 288 ms 129644 KB Output is correct
102 Correct 326 ms 129696 KB Output is correct