Submission #823108

# Submission time Handle Problem Language Result Execution time Memory
823108 2023-08-12T08:11:18 Z TheOpChicken Lamps (JOI19_lamps) C++17
100 / 100
235 ms 67060 KB
#include <bits/stdc++.h>
using namespace std;

/*
0 = nothing. 
1 = toggle
2 = set 0s
3 = set 1s
0: 0 0 0
1: 0 0 1
2: 0 0 2
3: 0 0 3
4: 0 1 2
5: 0 1 3
6: 0 2 1
7: 0 2 3
8: 0 3 1
9: 0 3 2
10: 1 2 3
11: 1 3 2
12: 2 1 3
13: 2 3 1
14: 3 1 2
15: 3 2 1
*/


const int maxN = 1e6 + 5, inf = 1e9;
int dp[maxN][16];

bool res[16][2] = {{0, 1}, {1, 0}, {1, 1}, {0, 0}, {0, 0}, {1, 1}, {1, 1}, {1, 1}, {0, 0}, {0, 0}, {1, 1}, {0, 0}, {1, 1}, {0, 0}, {0, 0}, {1, 1}};

vector<vector<int> > ops = {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 1}, {2, 3}, {3, 1}, {3, 2}, {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}};

int ops_needed[16][16];

int lcs(vector<int> X, vector<int> Y)
{
    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 L[m][n];
}

int main(){
	for (int i = 0; i < 16; i++){
		for (int j = 0; j < 16; j++){
			ops_needed[i][j] = ops[j].size() - lcs(ops[i], ops[j]);
		}
	}

	int n;
	cin >> n;

	string a, b;
	cin >> a >> b;

	dp[0][0] = 0;
	for (int i = 1; i < 16; i++) dp[0][i] = inf;

	for (int i = 1; i <= n; i++){
		bool a_bit = a[i-1] - '0', b_bit = b[i-1] - '0';
		for (int j = 0; j < 16; j++){
			dp[i][j] = inf;
			if (res[j][a_bit] != b_bit) continue;
			for (int k = 0; k < 16; k++) dp[i][j] = min(dp[i][j], dp[i-1][k] + ops_needed[k][j]);
		}
	}

	int ans = inf;
	for (int i = 0; i < 16; i++) ans = min(ans, dp[n][i]);

	cout << ans << endl;
}

# 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 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 1 ms 212 KB Output is correct
15 Correct 0 ms 316 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 1 ms 212 KB Output is correct
21 Correct 0 ms 316 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 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 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 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 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 1 ms 212 KB Output is correct
15 Correct 0 ms 316 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 1 ms 212 KB Output is correct
21 Correct 0 ms 316 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 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 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 0 ms 212 KB Output is correct
35 Correct 1 ms 444 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 444 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 372 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 452 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 448 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 448 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 452 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 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 0 ms 212 KB Output is correct
7 Correct 181 ms 65012 KB Output is correct
8 Correct 161 ms 64976 KB Output is correct
9 Correct 161 ms 64956 KB Output is correct
10 Correct 170 ms 65076 KB Output is correct
11 Correct 167 ms 65004 KB Output is correct
12 Correct 168 ms 64968 KB Output is correct
13 Correct 162 ms 64912 KB Output is correct
14 Correct 161 ms 64964 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 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 1 ms 212 KB Output is correct
15 Correct 0 ms 316 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 1 ms 212 KB Output is correct
21 Correct 0 ms 316 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 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 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 0 ms 212 KB Output is correct
35 Correct 1 ms 444 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 444 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 372 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 452 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 448 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 448 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 452 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 0 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 0 ms 212 KB Output is correct
65 Correct 181 ms 65012 KB Output is correct
66 Correct 161 ms 64976 KB Output is correct
67 Correct 161 ms 64956 KB Output is correct
68 Correct 170 ms 65076 KB Output is correct
69 Correct 167 ms 65004 KB Output is correct
70 Correct 168 ms 64968 KB Output is correct
71 Correct 162 ms 64912 KB Output is correct
72 Correct 161 ms 64964 KB Output is correct
73 Correct 170 ms 66876 KB Output is correct
74 Correct 165 ms 66908 KB Output is correct
75 Correct 169 ms 67040 KB Output is correct
76 Correct 166 ms 66888 KB Output is correct
77 Correct 173 ms 66976 KB Output is correct
78 Correct 167 ms 66940 KB Output is correct
79 Correct 177 ms 66944 KB Output is correct
80 Correct 159 ms 66940 KB Output is correct
81 Correct 161 ms 66900 KB Output is correct
82 Correct 183 ms 66868 KB Output is correct
83 Correct 173 ms 66912 KB Output is correct
84 Correct 171 ms 66900 KB Output is correct
85 Correct 225 ms 66968 KB Output is correct
86 Correct 168 ms 66968 KB Output is correct
87 Correct 177 ms 66912 KB Output is correct
88 Correct 189 ms 66904 KB Output is correct
89 Correct 183 ms 66892 KB Output is correct
90 Correct 172 ms 66916 KB Output is correct
91 Correct 169 ms 66972 KB Output is correct
92 Correct 200 ms 67060 KB Output is correct
93 Correct 188 ms 66916 KB Output is correct
94 Correct 226 ms 66920 KB Output is correct
95 Correct 177 ms 66864 KB Output is correct
96 Correct 184 ms 66956 KB Output is correct
97 Correct 175 ms 66912 KB Output is correct
98 Correct 209 ms 66988 KB Output is correct
99 Correct 228 ms 66948 KB Output is correct
100 Correct 201 ms 66896 KB Output is correct
101 Correct 235 ms 66912 KB Output is correct
102 Correct 217 ms 66936 KB Output is correct