Submission #293429

#TimeUsernameProblemLanguageResultExecution timeMemory
293429rama_pangMonochrome Points (JOI20_monochrome)C++14
100 / 100
9 ms3596 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N;
  string S;
  cin >> N >> S;

  vector<int> mismatch_white;
  vector<int> mismatch_black;
  for (int i = 0; i < N; i++) {
    if (S[i] == S[i + N]) {
      if (S[i] == 'W') {
        mismatch_white.emplace_back(i);
      } else {
        mismatch_black.emplace_back(i);
      }
    }
  }
  
  auto Solve = [&](int shift) {
    long long res = 0;
    for (int i = 0; i < (int) mismatch_black.size(); i++) {
      res += abs(mismatch_black[i] - mismatch_white[i  + shift]);
    }
    return res;
  };

  int mismatch = mismatch_white.size();
  for (int add = 1; add <= 2; add++) {
    for (int i = 0; i < mismatch; i++) {
      mismatch_white.emplace_back(mismatch_white[i]);
    }
  }
  for (int i = 0; i < mismatch; i++) {
    mismatch_white[i] -= N;
    mismatch_white[i + 2 * mismatch] += N;
  }

  int lo = 0, hi = 2 * mismatch;
  while (lo < hi) {
    int md = (lo + hi) / 2;
    if (Solve(md) < Solve(md + 1)) {
      hi = md;
    } else {
      lo = md + 1;
    }
  }

  cout << (1ll * N * (N - 1) / 2 - Solve(lo)) << "\n";
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...