답안 #104979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104979 2019-04-10T03:45:09 Z antimirage Lamps (JOI19_lamps) C++14
51 / 100
1000 ms 20032 KB
# include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1e6 + 5;
 
int n, dp[N][2], pref[N][2];

unordered_set <char> st;
 
string a, b;
 
inline int calc (int l, int r, int type){
 
	  if (type == 0)
			return pref[r][0] - pref[l - 1][0] + (b[l - 1] == b[l] && b[l] == '0');
 
	  return pref[r][1] - pref[l - 1][1] + (b[l - 1] == b[l] && b[l] == '1');
}
main(){
 
	  memset( dp, 0x3f3f3f3f, sizeof(dp) );
 
	  cin >> n >> a >> b;
 
	  a = ' ' + a;
	  b = ' ' + b;
 
	  for (int i = 1; i <= n; i++){
		  
			st.insert(a[i]);
 
			if (b[i] != b[i - 1])
				  pref[i][ b[i] - 48 ] = 1;
 
			pref[i][0] += pref[i - 1][0];
			pref[i][1] += pref[i - 1][1];
	  }
	  if (st.size() == 1 && a[1] == '0'){
		cout << calc(1, n, 1) << endl;
		return 0;
	  }
	  dp[0][0] = 0;
 
	  for (int i = 1; i <= n; i++){
 
			if (a[i] == b[i])
				  dp[i][0] = min( dp[i - 1][1], dp[i - 1][0] );
 
			if (a[i] != b[i])
				  dp[i][1] = min( dp[i - 1][1], dp[i - 1][0] + 1);
 
			for (int j = i; j >= 1; j--){
				  dp[i][0] = min( dp[i][0], dp[j - 1][0] + calc( j, i, 1 ) + 1 );
				  dp[i][0] = min( dp[i][0], dp[j - 1][0] + calc( j, i, 0 ) + 1 );
				  
				  dp[i][0] = min( dp[i][0], dp[j - 1][1] + calc( j, i, 1 ) - (b[j] == '1') + 1 );
				  dp[i][0] = min( dp[i][0], dp[j - 1][1] + calc( j, i, 0 ) - (b[j] == '0') + 1 );
 
				  dp[i][1] = min( dp[i][1], dp[j - 1][1] + calc( j, i, 0 ) + 1 );
 
				  dp[i][1] = min( dp[i][1], dp[j - 1][0] + calc( j, i, 0 ) - (b[i] == '0') + 2 );
				  dp[i][1] = min( dp[i][1], dp[j - 1][1] + max(0, calc( j, i, 0 ) - (b[j] == '0') - (b[i] == '0') ) + 2 );
 
				  dp[i][1] = min( dp[i][1], dp[j - 1][1] + calc( j, i, 1 ) + 1 );
 
				  dp[i][1] = min( dp[i][1], dp[j - 1][0] + calc( j, i, 1 ) -  (b[i] == '1') + 2 );
				  dp[i][1] = min( dp[i][1], dp[j - 1][1] + max(0, calc( j, i, 1 ) - (b[j] == '1') - (b[i] == '1') ) + 2 );
			}
	  }
	  cout << min(dp[n][0], dp[n][1]) << endl;
}
/**
13
0101100101001
1011111000000

13
0001101001101
0101100011011
**/

Compilation message

lamp.cpp:20:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 11 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 9 ms 8192 KB Output is correct
7 Correct 8 ms 8092 KB Output is correct
8 Correct 10 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 10 ms 8192 KB Output is correct
11 Correct 10 ms 8192 KB Output is correct
12 Correct 10 ms 8192 KB Output is correct
13 Correct 10 ms 8192 KB Output is correct
14 Correct 9 ms 8192 KB Output is correct
15 Correct 9 ms 8192 KB Output is correct
16 Correct 10 ms 8192 KB Output is correct
17 Correct 11 ms 8192 KB Output is correct
18 Correct 9 ms 8192 KB Output is correct
19 Correct 9 ms 8064 KB Output is correct
20 Correct 10 ms 8192 KB Output is correct
21 Correct 10 ms 8192 KB Output is correct
22 Correct 9 ms 8192 KB Output is correct
23 Correct 10 ms 8192 KB Output is correct
24 Correct 9 ms 8192 KB Output is correct
25 Correct 9 ms 8192 KB Output is correct
26 Correct 9 ms 8192 KB Output is correct
27 Correct 10 ms 8192 KB Output is correct
28 Correct 8 ms 8192 KB Output is correct
29 Correct 9 ms 8192 KB Output is correct
30 Correct 9 ms 8192 KB Output is correct
31 Correct 10 ms 8192 KB Output is correct
32 Correct 9 ms 8192 KB Output is correct
33 Correct 10 ms 8320 KB Output is correct
34 Correct 6 ms 8192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 11 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 9 ms 8192 KB Output is correct
7 Correct 8 ms 8092 KB Output is correct
8 Correct 10 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 10 ms 8192 KB Output is correct
11 Correct 10 ms 8192 KB Output is correct
12 Correct 10 ms 8192 KB Output is correct
13 Correct 10 ms 8192 KB Output is correct
14 Correct 9 ms 8192 KB Output is correct
15 Correct 9 ms 8192 KB Output is correct
16 Correct 10 ms 8192 KB Output is correct
17 Correct 11 ms 8192 KB Output is correct
18 Correct 9 ms 8192 KB Output is correct
19 Correct 9 ms 8064 KB Output is correct
20 Correct 10 ms 8192 KB Output is correct
21 Correct 10 ms 8192 KB Output is correct
22 Correct 9 ms 8192 KB Output is correct
23 Correct 10 ms 8192 KB Output is correct
24 Correct 9 ms 8192 KB Output is correct
25 Correct 9 ms 8192 KB Output is correct
26 Correct 9 ms 8192 KB Output is correct
27 Correct 10 ms 8192 KB Output is correct
28 Correct 8 ms 8192 KB Output is correct
29 Correct 9 ms 8192 KB Output is correct
30 Correct 9 ms 8192 KB Output is correct
31 Correct 10 ms 8192 KB Output is correct
32 Correct 9 ms 8192 KB Output is correct
33 Correct 10 ms 8320 KB Output is correct
34 Correct 6 ms 8192 KB Output is correct
35 Correct 50 ms 8224 KB Output is correct
36 Correct 9 ms 8192 KB Output is correct
37 Correct 9 ms 8192 KB Output is correct
38 Correct 55 ms 8192 KB Output is correct
39 Correct 58 ms 8192 KB Output is correct
40 Correct 53 ms 8192 KB Output is correct
41 Correct 65 ms 8312 KB Output is correct
42 Correct 53 ms 8192 KB Output is correct
43 Correct 59 ms 8312 KB Output is correct
44 Correct 77 ms 8192 KB Output is correct
45 Correct 51 ms 8192 KB Output is correct
46 Correct 55 ms 8192 KB Output is correct
47 Correct 58 ms 8192 KB Output is correct
48 Correct 54 ms 8192 KB Output is correct
49 Correct 52 ms 8192 KB Output is correct
50 Correct 54 ms 8192 KB Output is correct
51 Correct 60 ms 8312 KB Output is correct
52 Correct 54 ms 8192 KB Output is correct
53 Correct 52 ms 8192 KB Output is correct
54 Correct 52 ms 8192 KB Output is correct
55 Correct 72 ms 8192 KB Output is correct
56 Correct 56 ms 8256 KB Output is correct
57 Correct 58 ms 8224 KB Output is correct
58 Correct 54 ms 8192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 10 ms 8192 KB Output is correct
3 Correct 9 ms 8192 KB Output is correct
4 Correct 10 ms 8192 KB Output is correct
5 Correct 9 ms 8320 KB Output is correct
6 Correct 8 ms 8192 KB Output is correct
7 Correct 99 ms 17984 KB Output is correct
8 Correct 94 ms 19904 KB Output is correct
9 Correct 127 ms 20032 KB Output is correct
10 Correct 131 ms 19888 KB Output is correct
11 Correct 126 ms 19888 KB Output is correct
12 Correct 115 ms 19948 KB Output is correct
13 Correct 119 ms 19864 KB Output is correct
14 Correct 103 ms 19900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 11 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 9 ms 8192 KB Output is correct
7 Correct 8 ms 8092 KB Output is correct
8 Correct 10 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 10 ms 8192 KB Output is correct
11 Correct 10 ms 8192 KB Output is correct
12 Correct 10 ms 8192 KB Output is correct
13 Correct 10 ms 8192 KB Output is correct
14 Correct 9 ms 8192 KB Output is correct
15 Correct 9 ms 8192 KB Output is correct
16 Correct 10 ms 8192 KB Output is correct
17 Correct 11 ms 8192 KB Output is correct
18 Correct 9 ms 8192 KB Output is correct
19 Correct 9 ms 8064 KB Output is correct
20 Correct 10 ms 8192 KB Output is correct
21 Correct 10 ms 8192 KB Output is correct
22 Correct 9 ms 8192 KB Output is correct
23 Correct 10 ms 8192 KB Output is correct
24 Correct 9 ms 8192 KB Output is correct
25 Correct 9 ms 8192 KB Output is correct
26 Correct 9 ms 8192 KB Output is correct
27 Correct 10 ms 8192 KB Output is correct
28 Correct 8 ms 8192 KB Output is correct
29 Correct 9 ms 8192 KB Output is correct
30 Correct 9 ms 8192 KB Output is correct
31 Correct 10 ms 8192 KB Output is correct
32 Correct 9 ms 8192 KB Output is correct
33 Correct 10 ms 8320 KB Output is correct
34 Correct 6 ms 8192 KB Output is correct
35 Correct 50 ms 8224 KB Output is correct
36 Correct 9 ms 8192 KB Output is correct
37 Correct 9 ms 8192 KB Output is correct
38 Correct 55 ms 8192 KB Output is correct
39 Correct 58 ms 8192 KB Output is correct
40 Correct 53 ms 8192 KB Output is correct
41 Correct 65 ms 8312 KB Output is correct
42 Correct 53 ms 8192 KB Output is correct
43 Correct 59 ms 8312 KB Output is correct
44 Correct 77 ms 8192 KB Output is correct
45 Correct 51 ms 8192 KB Output is correct
46 Correct 55 ms 8192 KB Output is correct
47 Correct 58 ms 8192 KB Output is correct
48 Correct 54 ms 8192 KB Output is correct
49 Correct 52 ms 8192 KB Output is correct
50 Correct 54 ms 8192 KB Output is correct
51 Correct 60 ms 8312 KB Output is correct
52 Correct 54 ms 8192 KB Output is correct
53 Correct 52 ms 8192 KB Output is correct
54 Correct 52 ms 8192 KB Output is correct
55 Correct 72 ms 8192 KB Output is correct
56 Correct 56 ms 8256 KB Output is correct
57 Correct 58 ms 8224 KB Output is correct
58 Correct 54 ms 8192 KB Output is correct
59 Correct 9 ms 8192 KB Output is correct
60 Correct 10 ms 8192 KB Output is correct
61 Correct 9 ms 8192 KB Output is correct
62 Correct 10 ms 8192 KB Output is correct
63 Correct 9 ms 8320 KB Output is correct
64 Correct 8 ms 8192 KB Output is correct
65 Correct 99 ms 17984 KB Output is correct
66 Correct 94 ms 19904 KB Output is correct
67 Correct 127 ms 20032 KB Output is correct
68 Correct 131 ms 19888 KB Output is correct
69 Correct 126 ms 19888 KB Output is correct
70 Correct 115 ms 19948 KB Output is correct
71 Correct 119 ms 19864 KB Output is correct
72 Correct 103 ms 19900 KB Output is correct
73 Execution timed out 1081 ms 19940 KB Time limit exceeded
74 Halted 0 ms 0 KB -