Submission #253140

# Submission time Handle Problem Language Result Execution time Memory
253140 2020-07-27T01:47:36 Z Dilshod_Imomov Lamps (JOI19_lamps) C++17
100 / 100
320 ms 67152 KB
# include <bits/stdc++.h>
# define speed ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
// # pragma GCC optimize "-O3"
// # define int long long
  
using namespace std;
  
const int N = 1e6 + 7;
const int mod = 1e9 + 7;

vector < vector < int > > vc = { {}, {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} }; 
// map < int, map < vector < int >, int > > dp;
int n, cnt[20][20], dp[N][16], change[2][16];
string a, b;

int get( int i, int x ) {
	if ( (int)vc[x].size() == 0 ) {
		int mn = N;
		for ( int j = 0; j < 16; j++ ) {
			mn = min( mn, dp[i - 1][j] );
		}
		return mn + (a[i] != b[i]);
	}
	int mn = N;
	for ( int y = 0; y < 16; y++ ) {
		int vsz = vc[x].size(), usz = vc[y].size();
		if ( vsz <= usz ) {
			mn = min( mn, dp[i - 1][y] + vsz - cnt[x][y] );
		}
		else {
			mn = min( mn, dp[i - 1][y] + vsz - cnt[y][x] );
		}
	}
	return mn;
}

int32_t main() {
    speed;
    cin >> n;
    cin >> a >> b;
    for ( int i = 0; i < 16; i++ ) {
    	for ( int j = 0; j < 16; j++ ) {
    		int vsz = vc[i].size(), mx = 0; 
			for ( int mask = 0; mask < (1 << vsz); mask++ ) {
				vector < int > x;
				int cnt = 0;
				for ( int bit = 0; bit < 3; bit++ ) {
					if ( (mask >> bit) & 1 ) {
						x.push_back( vc[i][bit] );
						cnt++;
					}
				}
				int ok = 0;
				if ( cnt ) {
					int ind = 0;
					for ( auto z: vc[j] ) {
						if ( ind < cnt && z == x[ind] ) {
							ind++;
						}
					}
					if ( ind == cnt ) {
						ok = 1;
					}
				}
				if ( ok ) {
					mx = max( mx, cnt );
				}
			}
			cnt[i][j] = mx;
    	}
    }
    for ( int i = 0; i < 16; i++ ) {
		char x = '0', y = '1';
		for ( auto j: vc[i] ) {
			if ( j == 1 ) {
				x = '0';
				y = '0';
			}
			else if ( j == 2 ) {
				x = '1';
				y = '1';
			}
			else {
				if ( y == '0' ) {
					y = '1';
				}
				else {
					y = '0';
				}
				if ( x == '0' ) {
					x = '1';
				}
				else {
					x = '0';
				}
			}
		}
    	change[0][i] = x - '0';
    	change[1][i] = y - '0';
    }
    for ( int j = 0; j < 16; j++ ) {
		if ( ('0' + change[a[0] - '0'][j]) != b[0] ) {
			dp[0][j] = N;
			continue;
		}
    	dp[0][j] = vc[j].size();
    }
    if ( a[0] != b[0] ) {
    	dp[0][0] = 1;
    }
    for ( int i = 1; i < n; i++ ) {
    	for ( int ind = 0; ind < 16; ind++ ) {
    		if ( ('0' + change[a[i] - '0'][ind]) != b[i] ) {
    			dp[i][ind] = N;
    			continue;
    		}
    		dp[i][ind] = get( i, ind );
    		/*for ( auto j: v ) {
    			cout << j << ' ';
    		}
    		cout << '\n';
    		cout << i << ' ' << dp[i][v] << '\n';
    		cout << "------------\n";*/
    	}
    }
    int mn = N;
    for ( int i = 0; i < 16; i++ ) {
    	mn = min( mn, dp[n - 1][i] );
    }
    cout << mn << '\n';
}
/*
8
11011100
01101001

13
1010010010100
0000111001011

18
001100010010000110
110110001000100101
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 0 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 0 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 1 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 2 ms 512 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 1 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 1 ms 512 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 1 ms 512 KB Output is correct
46 Correct 1 ms 512 KB Output is correct
47 Correct 1 ms 512 KB Output is correct
48 Correct 1 ms 512 KB Output is correct
49 Correct 1 ms 512 KB Output is correct
50 Correct 1 ms 512 KB Output is correct
51 Correct 1 ms 512 KB Output is correct
52 Correct 1 ms 512 KB Output is correct
53 Correct 1 ms 512 KB Output is correct
54 Correct 2 ms 512 KB Output is correct
55 Correct 2 ms 512 KB Output is correct
56 Correct 1 ms 512 KB Output is correct
57 Correct 1 ms 512 KB Output is correct
58 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 258 ms 65224 KB Output is correct
8 Correct 302 ms 67020 KB Output is correct
9 Correct 318 ms 67032 KB Output is correct
10 Correct 285 ms 67024 KB Output is correct
11 Correct 320 ms 67016 KB Output is correct
12 Correct 274 ms 67144 KB Output is correct
13 Correct 273 ms 67020 KB Output is correct
14 Correct 267 ms 67144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 0 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 1 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 2 ms 512 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 1 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 1 ms 512 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 1 ms 512 KB Output is correct
46 Correct 1 ms 512 KB Output is correct
47 Correct 1 ms 512 KB Output is correct
48 Correct 1 ms 512 KB Output is correct
49 Correct 1 ms 512 KB Output is correct
50 Correct 1 ms 512 KB Output is correct
51 Correct 1 ms 512 KB Output is correct
52 Correct 1 ms 512 KB Output is correct
53 Correct 1 ms 512 KB Output is correct
54 Correct 2 ms 512 KB Output is correct
55 Correct 2 ms 512 KB Output is correct
56 Correct 1 ms 512 KB Output is correct
57 Correct 1 ms 512 KB Output is correct
58 Correct 1 ms 512 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 0 ms 384 KB Output is correct
61 Correct 0 ms 384 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 0 ms 384 KB Output is correct
64 Correct 0 ms 384 KB Output is correct
65 Correct 258 ms 65224 KB Output is correct
66 Correct 302 ms 67020 KB Output is correct
67 Correct 318 ms 67032 KB Output is correct
68 Correct 285 ms 67024 KB Output is correct
69 Correct 320 ms 67016 KB Output is correct
70 Correct 274 ms 67144 KB Output is correct
71 Correct 273 ms 67020 KB Output is correct
72 Correct 267 ms 67144 KB Output is correct
73 Correct 274 ms 67024 KB Output is correct
74 Correct 268 ms 67152 KB Output is correct
75 Correct 311 ms 67020 KB Output is correct
76 Correct 309 ms 67020 KB Output is correct
77 Correct 284 ms 67020 KB Output is correct
78 Correct 291 ms 67020 KB Output is correct
79 Correct 278 ms 67012 KB Output is correct
80 Correct 293 ms 67084 KB Output is correct
81 Correct 291 ms 67016 KB Output is correct
82 Correct 288 ms 67016 KB Output is correct
83 Correct 277 ms 67020 KB Output is correct
84 Correct 277 ms 67084 KB Output is correct
85 Correct 261 ms 67040 KB Output is correct
86 Correct 279 ms 67072 KB Output is correct
87 Correct 260 ms 67016 KB Output is correct
88 Correct 260 ms 67020 KB Output is correct
89 Correct 271 ms 67016 KB Output is correct
90 Correct 273 ms 67016 KB Output is correct
91 Correct 294 ms 67016 KB Output is correct
92 Correct 298 ms 67148 KB Output is correct
93 Correct 303 ms 67024 KB Output is correct
94 Correct 305 ms 67016 KB Output is correct
95 Correct 304 ms 67016 KB Output is correct
96 Correct 296 ms 67140 KB Output is correct
97 Correct 303 ms 67016 KB Output is correct
98 Correct 298 ms 67024 KB Output is correct
99 Correct 293 ms 67144 KB Output is correct
100 Correct 289 ms 67016 KB Output is correct
101 Correct 283 ms 67016 KB Output is correct
102 Correct 282 ms 67012 KB Output is correct