Submission #823168

# Submission time Handle Problem Language Result Execution time Memory
823168 2023-08-12T08:46:01 Z BidoTeima Lamps (JOI19_lamps) C++17
100 / 100
758 ms 66976 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int>vec[20];  
int cost[16][16];
int calc(int current, int target){
    if(cost[current][target]!=-1)
        return cost[current][target];
    vector<int>X=vec[current];
    vector<int>Y=vec[target];
        int m = X.size();
    int n = Y.size();
    int L[m + 1][n + 1];
 
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0)
                L[i][j] = 0;
 
            else if (X[i - 1] == Y[j - 1])
                L[i][j] = L[i - 1][j - 1] + 1;
 
            else
                L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }
    return cost[current][target]=n-L[m][n];
}
int main()
{        
    memset(cost,-1,sizeof(cost));
    int n;
    string a,b;
    cin>>n>>a>>b;
    for(char&ch:a)ch-='0';
    for(char&ch:b)ch-='0';
    map<vector<int>,int>ind{};
    vector<int>vmsk[20];
    int mask[20];
    int to[2][20];
    int cur=0;
    for(int i = 0; i < 8; i++){
        vector<int>v;
        int msk=0;
        for(int j = 0; j < 3; j++){
            if(i&(1<<j))v.push_back(j),msk+=(1<<j);
        }
        vmsk[msk]=v;
        do{
            vec[cur]=v;
            ind[v]=cur;
            mask[cur]=msk;
            ++cur;
        }while(next_permutation(v.begin(),v.end()));
    }
    int dp[16][n+1];
    for(int i = 0; i < 16; i++){
        for(int j = 0; j < n + 1; j++){
            dp[i][j]=1e6+1;
        }
    }
    for(int i = 0; i < 16; i++){ 
        int val = a[0];
        for(int x : vec[i]){
            if(x==0)val=0;
            else if(x==1)val=1;
            else val ^= 1;
        }
        if(val==b[0]){
            dp[i][0]=vec[i].size();
            //assert(vec[i].size());
        }
        to[a[0]][i]=val;
        val=a[0]^1;
        for(int x : vec[i]){
            if(x==0)val=0;
            else if(x==1)val=1;
            else val ^= 1;
        }
        to[a[0]^1][i]=val;
    }
    /*for(int i = 0; i < n; i++){
        for(int j = 0; j < cur; j++){
            assert(dp[j][i]);
        }
    }*/
    for(int i = 0; i + 1 < n; i++){
        for(int j = 0; j < 16; j++){
            if(to[a[i+1]][j]!=b[i+1])continue;
            for(int k = 0; k < 16; k++){
                dp[j][i+1]=min(dp[j][i+1],dp[k][i]+calc(k,j));
            }
        }
    }
    int ans = 1e9;
    for(int j = 0; j < 16; j++){
        ans=min(ans,dp[j][n-1]);
    }
    assert(cur==16);
    cout<<ans;
    return 0;
}

Compilation message

lamp.cpp: In function 'int main()':
lamp.cpp:73:16: warning: array subscript has type 'char' [-Wchar-subscripts]
   73 |         to[a[0]][i]=val;
      |                ^
lamp.cpp:89:25: warning: array subscript has type 'char' [-Wchar-subscripts]
   89 |             if(to[a[i+1]][j]!=b[i+1])continue;
      |                         ^
lamp.cpp:39:9: warning: variable 'mask' set but not used [-Wunused-but-set-variable]
   39 |     int mask[20];
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 2 ms 340 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 2 ms 340 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 658 ms 65008 KB Output is correct
8 Correct 616 ms 64908 KB Output is correct
9 Correct 676 ms 65004 KB Output is correct
10 Correct 649 ms 65004 KB Output is correct
11 Correct 659 ms 65004 KB Output is correct
12 Correct 586 ms 65004 KB Output is correct
13 Correct 661 ms 65008 KB Output is correct
14 Correct 665 ms 65264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 2 ms 340 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 2 ms 340 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 658 ms 65008 KB Output is correct
66 Correct 616 ms 64908 KB Output is correct
67 Correct 676 ms 65004 KB Output is correct
68 Correct 649 ms 65004 KB Output is correct
69 Correct 659 ms 65004 KB Output is correct
70 Correct 586 ms 65004 KB Output is correct
71 Correct 661 ms 65008 KB Output is correct
72 Correct 665 ms 65264 KB Output is correct
73 Correct 704 ms 66960 KB Output is correct
74 Correct 681 ms 66960 KB Output is correct
75 Correct 729 ms 66964 KB Output is correct
76 Correct 677 ms 66960 KB Output is correct
77 Correct 650 ms 66960 KB Output is correct
78 Correct 683 ms 66856 KB Output is correct
79 Correct 722 ms 66920 KB Output is correct
80 Correct 642 ms 66960 KB Output is correct
81 Correct 716 ms 66964 KB Output is correct
82 Correct 647 ms 66964 KB Output is correct
83 Correct 648 ms 66968 KB Output is correct
84 Correct 609 ms 66964 KB Output is correct
85 Correct 634 ms 66964 KB Output is correct
86 Correct 619 ms 66964 KB Output is correct
87 Correct 647 ms 66972 KB Output is correct
88 Correct 703 ms 66964 KB Output is correct
89 Correct 744 ms 66964 KB Output is correct
90 Correct 758 ms 66960 KB Output is correct
91 Correct 656 ms 66960 KB Output is correct
92 Correct 719 ms 66960 KB Output is correct
93 Correct 714 ms 66968 KB Output is correct
94 Correct 654 ms 66960 KB Output is correct
95 Correct 725 ms 66960 KB Output is correct
96 Correct 636 ms 66960 KB Output is correct
97 Correct 719 ms 66956 KB Output is correct
98 Correct 639 ms 66960 KB Output is correct
99 Correct 590 ms 66964 KB Output is correct
100 Correct 608 ms 66976 KB Output is correct
101 Correct 604 ms 66976 KB Output is correct
102 Correct 601 ms 66968 KB Output is correct