답안 #253136

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253136 2020-07-27T01:17:32 Z Dilshod_Imomov Lamps (JOI19_lamps) C++17
47 / 100
1000 ms 29764 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;

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;
string a, b;

int get( int i, vector < int > v ) {
	if ( (int)v.size() == 0 ) {
		int mn = 1e9;
		for ( auto u: vc ) {
			mn = min( mn, dp[i - 1][u] );
		}
		return mn + (a[i] != b[i]);
	}
	int mn = 1e9;
	for ( auto u: vc ) {
		int vsz = v.size(), usz = u.size();
		if ( vsz <= usz ) {
			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( v[bit] );
						cnt++;
					}
				}
				int ok = 0;
				if ( cnt ) {
					int ind = 0;
					for ( auto z: u ) {
						if ( ind < cnt && z == x[ind] ) {
							ind++;
						}
					}
					if ( ind == cnt ) {
						ok = 1;
					}
				}
				if ( ok ) {
					mn = min( mn, dp[i - 1][u] + vsz - cnt );
				}
				else {
					mn = min( mn, dp[i - 1][u] + vsz );
				}
			}
		}
		else {
			for ( int mask = 0; mask < (1 << usz); mask++ ) {
				vector < int > x;
				int cnt = 0;
				for ( int bit = 0; bit < 3; bit++ ) {
					if ( (mask >> bit) & 1 ) {
						x.push_back( u[bit] );
						cnt++;
					}
				}
				int ok = 0;
				if ( cnt ) {
					int ind = 0;
					for ( auto z: v ) {
						if ( ind < cnt && z == x[ind] ) {
							ind++;
						}
					}
					if ( ind == cnt ) {
						ok = 1;
					}
				}
				if ( ok ) {
					mn = min( mn, dp[i - 1][u] + vsz - cnt );
				}
				else {
					mn = min( mn, dp[i - 1][u] + vsz );
				}
			}
		}
	}
	return mn;
}

int32_t main() {
    speed;
    cin >> n;
    cin >> a >> b;
    for ( auto v: vc ) {
		char x = a[0];
		for ( auto j: v ) {
			if ( j == 1 ) {
				x = '0';
			}
			else if ( j == 2 ) {
				x = '1';
			}
			else {
				if ( x == '0' ) {
					x = '1';
				}
				else {
					x = '0';
				}
			}
		}
		if ( x != b[0] ) {
			dp[0][v] = 1e9;
			continue;
		}
    	dp[0][v] = v.size();
    }
    if ( a[0] != b[0] ) {
    	dp[0][{}] = 1;
    }
    for ( int i = 1; i < n; i++ ) {
    	for ( auto v: vc ) {
    		char x = a[i];
    		for ( auto j: v ) {
    			if ( j == 1 ) {
    				x = '0';
    			}
    			else if ( j == 2 ) {
    				x = '1';
    			}
    			else {
    				if ( x == '0' ) {
    					x = '1';
    				}
    				else {
    					x = '0';
    				}
    			}
    		}
    		if ( x != b[i] ) {
    			dp[i][v] = 1e9;
    			continue;
    		}
    		dp[i][v] = get( i, v );
    		/*for ( auto j: v ) {
    			cout << j << ' ';
    		}
    		cout << '\n';
    		cout << i << ' ' << dp[i][v] << '\n';
    		cout << "------------\n";*/
    	}
    }
    int mn = 1e9;
    for ( auto v: vc ) {
    	mn = min( mn, dp[n - 1][v] );
    }
    cout << mn << '\n';
}
/*
8
11011100
01101001

13
1010010010100
0000111001011

18
001100010010000110
110110001000100101
*/
# 결과 실행 시간 메모리 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 0 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 0 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 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 0 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 0 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 142 ms 3960 KB Output is correct
36 Correct 142 ms 3960 KB Output is correct
37 Correct 145 ms 3960 KB Output is correct
38 Correct 144 ms 3960 KB Output is correct
39 Correct 144 ms 4036 KB Output is correct
40 Correct 147 ms 3960 KB Output is correct
41 Correct 144 ms 3960 KB Output is correct
42 Correct 139 ms 3960 KB Output is correct
43 Correct 146 ms 4088 KB Output is correct
44 Correct 141 ms 3960 KB Output is correct
45 Correct 140 ms 4068 KB Output is correct
46 Correct 142 ms 3960 KB Output is correct
47 Correct 139 ms 4088 KB Output is correct
48 Correct 139 ms 3996 KB Output is correct
49 Correct 139 ms 3960 KB Output is correct
50 Correct 143 ms 4088 KB Output is correct
51 Correct 144 ms 3960 KB Output is correct
52 Correct 140 ms 3960 KB Output is correct
53 Correct 142 ms 3928 KB Output is correct
54 Correct 139 ms 3960 KB Output is correct
55 Correct 139 ms 4088 KB Output is correct
56 Correct 141 ms 3960 KB Output is correct
57 Correct 139 ms 3960 KB Output is correct
58 Correct 142 ms 4088 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 384 KB Output is correct
7 Execution timed out 1092 ms 29764 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 0 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 0 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 142 ms 3960 KB Output is correct
36 Correct 142 ms 3960 KB Output is correct
37 Correct 145 ms 3960 KB Output is correct
38 Correct 144 ms 3960 KB Output is correct
39 Correct 144 ms 4036 KB Output is correct
40 Correct 147 ms 3960 KB Output is correct
41 Correct 144 ms 3960 KB Output is correct
42 Correct 139 ms 3960 KB Output is correct
43 Correct 146 ms 4088 KB Output is correct
44 Correct 141 ms 3960 KB Output is correct
45 Correct 140 ms 4068 KB Output is correct
46 Correct 142 ms 3960 KB Output is correct
47 Correct 139 ms 4088 KB Output is correct
48 Correct 139 ms 3996 KB Output is correct
49 Correct 139 ms 3960 KB Output is correct
50 Correct 143 ms 4088 KB Output is correct
51 Correct 144 ms 3960 KB Output is correct
52 Correct 140 ms 3960 KB Output is correct
53 Correct 142 ms 3928 KB Output is correct
54 Correct 139 ms 3960 KB Output is correct
55 Correct 139 ms 4088 KB Output is correct
56 Correct 141 ms 3960 KB Output is correct
57 Correct 139 ms 3960 KB Output is correct
58 Correct 142 ms 4088 KB Output is correct
59 Correct 1 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 0 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 Execution timed out 1092 ms 29764 KB Time limit exceeded
66 Halted 0 ms 0 KB -