Submission #104607

# Submission time Handle Problem Language Result Execution time Memory
104607 2019-04-08T10:43:31 Z antimirage Lamps (JOI19_lamps) C++14
0 / 100
1000 ms 21848 KB
# include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

int n, dp[N][2], pref[N][2], fl[N];

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++){

            if (a[i] != a[i - 1])
                  fl[i]++;

            fl[i] += fl[i - 1];

            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];
      }
      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 - ( fl[i] == fl[j] && a[i] == '0') );
                  dp[i][0] = min( dp[i][0], dp[j - 1][0] + calc( j, i, 0 ) + 1 - ( fl[i] == fl[j] && a[i] == '1') );

                  dp[i][0] = min( dp[i][0], dp[j - 1][1] + calc( j, i, 1 ) + 1 - ( fl[i] == fl[j] && a[i] == '1') );
                  dp[i][0] = min( dp[i][0], dp[j - 1][1] + calc( j, i, 0 ) + 1 - ( fl[i] == fl[j] && a[i] == '0') );

                  dp[i][1] = min( dp[i][1], dp[j - 1][1] + calc( j, i, 0 ) + 1 - ( fl[i] == fl[j] && a[i] == '0') );
                  dp[i][1] = min( dp[i][1], dp[j - 1][1] + calc( j, i, 1 ) + 1 - ( fl[i] == fl[j] && a[i] == '1') );

                  dp[i][1] = min( dp[i][1], dp[j - 1][0] + calc( j, i, 0 ) + 2 - ( fl[i] == fl[j] && a[i] == '1') );
                  dp[i][1] = min( dp[i][1], dp[j - 1][0] + calc( j, i, 1 ) + 2 - ( fl[i] == fl[j] && a[i] == '0') );
            }
      }
      cout << min(dp[n][1], dp[n][0]) << endl;
}

Compilation message

lamp.cpp:18:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8192 KB Output is correct
2 Correct 10 ms 8192 KB Output is correct
3 Correct 7 ms 8192 KB Output is correct
4 Correct 9 ms 8228 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 10 ms 8320 KB Output is correct
7 Correct 9 ms 8140 KB Output is correct
8 Correct 8 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 10 ms 8136 KB Output is correct
11 Correct 9 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 10 ms 8192 KB Output is correct
15 Correct 11 ms 8320 KB Output is correct
16 Correct 9 ms 8320 KB Output is correct
17 Correct 9 ms 8192 KB Output is correct
18 Correct 8 ms 8192 KB Output is correct
19 Correct 9 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 9 ms 8192 KB Output is correct
22 Correct 10 ms 8192 KB Output is correct
23 Correct 10 ms 8212 KB Output is correct
24 Correct 10 ms 8192 KB Output is correct
25 Correct 11 ms 8192 KB Output is correct
26 Incorrect 9 ms 8192 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8192 KB Output is correct
2 Correct 10 ms 8192 KB Output is correct
3 Correct 7 ms 8192 KB Output is correct
4 Correct 9 ms 8228 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 10 ms 8320 KB Output is correct
7 Correct 9 ms 8140 KB Output is correct
8 Correct 8 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 10 ms 8136 KB Output is correct
11 Correct 9 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 10 ms 8192 KB Output is correct
15 Correct 11 ms 8320 KB Output is correct
16 Correct 9 ms 8320 KB Output is correct
17 Correct 9 ms 8192 KB Output is correct
18 Correct 8 ms 8192 KB Output is correct
19 Correct 9 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 9 ms 8192 KB Output is correct
22 Correct 10 ms 8192 KB Output is correct
23 Correct 10 ms 8212 KB Output is correct
24 Correct 10 ms 8192 KB Output is correct
25 Correct 11 ms 8192 KB Output is correct
26 Incorrect 9 ms 8192 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8192 KB Output is correct
2 Correct 10 ms 8192 KB Output is correct
3 Correct 10 ms 8164 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8064 KB Output is correct
6 Correct 10 ms 8192 KB Output is correct
7 Execution timed out 1062 ms 21848 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8192 KB Output is correct
2 Correct 10 ms 8192 KB Output is correct
3 Correct 7 ms 8192 KB Output is correct
4 Correct 9 ms 8228 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 10 ms 8320 KB Output is correct
7 Correct 9 ms 8140 KB Output is correct
8 Correct 8 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 10 ms 8136 KB Output is correct
11 Correct 9 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 10 ms 8192 KB Output is correct
15 Correct 11 ms 8320 KB Output is correct
16 Correct 9 ms 8320 KB Output is correct
17 Correct 9 ms 8192 KB Output is correct
18 Correct 8 ms 8192 KB Output is correct
19 Correct 9 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 9 ms 8192 KB Output is correct
22 Correct 10 ms 8192 KB Output is correct
23 Correct 10 ms 8212 KB Output is correct
24 Correct 10 ms 8192 KB Output is correct
25 Correct 11 ms 8192 KB Output is correct
26 Incorrect 9 ms 8192 KB Output isn't correct
27 Halted 0 ms 0 KB -