Submission #679045

#TimeUsernameProblemLanguageResultExecution timeMemory
679045bashkortMonochrome Points (JOI20_monochrome)C++17
100 / 100
577 ms5076 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Fenwick { vector<int> a; int n{}; Fenwick() = default; Fenwick(int n) : n(n), a(n + 1) {} int sum(int i) { int ans = 0; for (int x = i + 1; x > 0; x -= x & -x) { ans += a[x]; } return ans; } void modify(int i, int val) { for (int x = i + 1; x <= n; x += x & -x) { a[x] += val; } } }; ll intersecting(vector<int> &p) { int n = p.size(); Fenwick fn(n); ll ans = 0; for (int i = 0; i < n; ++i) { if (p[i] > i) { ans -= fn.sum(i); continue; } ans += fn.sum(p[i]); fn.modify(p[i], 1); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; cin >> s; vector<int> p(2 * n); auto check_w = [&](int w) -> bool { int b = n - w; int left[2]{w, b}, pos[2]{}; bool yay = true; for (int i = 0; i < 2 * n; ++i) { int tp = s[i] == 'B'; if (left[tp]) { pos[tp] += 1; --left[tp]; } else { if (pos[tp ^ 1] == 0 && tp == 1) { return false; } pos[tp ^ 1] -= 1; } } return true; }; auto check_b = [&](int b) -> bool { int w = n - b; int left[2]{w, b}, pos[2]{}; bool yay = true; for (int i = 0; i < 2 * n; ++i) { int tp = s[i] == 'B'; if (left[tp]) { pos[tp] += 1; --left[tp]; } else { if (pos[tp ^ 1] == 0 && tp == 0) { return false; } pos[tp ^ 1] -= 1; } } return true; }; ll ans = 0; auto calc = [&](int w) -> ll { int b = n - w; queue<int> pos[2]; int left[2]{w, b}; bool yay = true; for (int i = 0; i < 2 * n; ++i) { int tp = s[i] == 'B'; if (left[tp]) { pos[tp].push(i); --left[tp]; } else { if (pos[tp ^ 1].empty()) { yay = false; break; } p[i] = pos[tp ^ 1].front(); p[p[i]] = i; pos[tp ^ 1].pop(); } } if (yay) { ll now = intersecting(p); ans = max(ans, now); return now; } else { return -1; } }; int lo = 0, hi = n + 1; while (lo + 1 < hi) { int mid = (lo + hi) >> 1; if (check_w(mid)) lo = mid; else hi = mid; } int R = lo; lo = 0, hi = n + 1; while (lo + 1 < hi) { int mid = (lo + hi) >> 1; if (check_b(mid)) lo = mid; else hi = mid; } int L = n - lo; assert(L <= R); lo = L, hi = R; calc(L), calc(R); while (lo < hi) { int mid = (lo + hi) >> 1; if (calc(mid + 1) >= calc(mid)) lo = mid + 1; else hi = mid; } cout << ans << '\n'; return 0; }

Compilation message (stderr)

monochrome.cpp: In constructor 'Fenwick::Fenwick(int)':
monochrome.cpp:8:9: warning: 'Fenwick::n' will be initialized after [-Wreorder]
    8 |     int n{};
      |         ^
monochrome.cpp:7:17: warning:   'std::vector<int> Fenwick::a' [-Wreorder]
    7 |     vector<int> a;
      |                 ^
monochrome.cpp:12:5: warning:   when initialized here [-Wreorder]
   12 |     Fenwick(int n) : n(n), a(n + 1) {}
      |     ^~~~~~~
monochrome.cpp: In lambda function:
monochrome.cpp:59:14: warning: unused variable 'yay' [-Wunused-variable]
   59 |         bool yay = true;
      |              ^~~
monochrome.cpp: In lambda function:
monochrome.cpp:79:14: warning: unused variable 'yay' [-Wunused-variable]
   79 |         bool yay = true;
      |              ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...