Submission #986810

# Submission time Handle Problem Language Result Execution time Memory
986810 2024-05-21T09:14:49 Z cig32 Lamps (JOI19_lamps) C++17
0 / 100
1000 ms 19908 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;
mt19937_64 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  return uniform_int_distribution<int>(x, y)(rng);
}
const int MAXN = 1e6 + 10;
string a, b;
int n;
int ps[MAXN];
int zero(int l, int r) {
  int nums = ps[r] - ps[l];
  int cnt0, cnt1;
  if(nums % 2 == 1) {
    cnt0 = cnt1 = (nums + 1) / 2;
  }
  else {
    cnt0 = nums / 2 + 1, cnt1 = nums / 2;
    if(b[l] == '1') swap(cnt0, cnt1);
  }
  return cnt0;
}
int one(int l, int r) {
  int nums = ps[r] - ps[l];
  int cnt0, cnt1;
  if(nums % 2 == 1) {
    cnt0 = cnt1 = (nums + 1) / 2;
  }
  else {
    cnt0 = nums / 2 + 1, cnt1 = nums / 2;
    if(b[l] == '1') swap(cnt0, cnt1);
  }
  return cnt1;
}
int mincnt(int l, int r) {
  return min(zero(l, r), one(l, r));
}
void solve(int tc) {
  cin >> n;
  cin >> a >> b;
  a = " " + a;
  b = " " + b;
  for(int i=2; i<=n; i++) ps[i] = ps[i-1] + (b[i] != b[i-1]);
  int dp[n+1];
  dp[0] = 0;
  for(int i=1; i<=n; i++) {
    int cnteq = 0, cntneq = 0;
    dp[i] = 1e9;
    for(int j=i-1; j>=0; j--) {
      cnteq += (a[j+1] == b[j+1]);
      cntneq += (a[j+1] != b[j+1]);
      if(cntneq == 0) {
        dp[i] = min(dp[i], dp[j]);
      }
      if(cnteq == 0) {
        dp[i] = min(dp[i], dp[j] + 1);
      }
      dp[i] = min(dp[i], dp[j] + 1 + mincnt(j+1, i));
    }
  }
  cout << dp[n] << '\n';
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
/*
g++ T2421_brute.cpp -std=c++17 -O2 -o T2421_brute
./T2421_brute < input.txt

18
101001010010101110
001100001111000000
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Execution timed out 1089 ms 19908 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Halted 0 ms 0 KB -