結果
問題 |
No.3114 0→1
|
ユーザー |
|
提出日時 | 2025-04-19 09:26:01 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 12 ms / 2,000 ms |
コード長 | 1,252 bytes |
コンパイル時間 | 2,003 ms |
コンパイル使用メモリ | 197,852 KB |
実行使用メモリ | 7,848 KB |
最終ジャッジ日時 | 2025-04-19 09:26:05 |
合計ジャッジ時間 | 3,581 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string S; cin >> N >> S; long long ps = 0; // current prefix‐sum (1→+1, 0→–1, with flips applied on the fly) long long max_ps = 0; // max prefix‐sum over positions ≤ i–2 int flips = 0; // total flips performed priority_queue<int> zeros; // max‐heap of indices of kept zeros for (int i = 0; i < N; i++) { long long prev = ps; // ps at position i–1 if (S[i] == '1') { ps = prev + 1; } else { // tentatively keep this zero zeros.push(i); ps = prev - 1; // for i>=1 we must have ps(i) >= max_ps, else flip zeros if (i >= 1) { while (ps < max_ps) { zeros.pop(); // flip the most‐recent kept zero flips++; ps += 2; // flipping gives +2 to all suffix sums } } } // update max_ps = max over ps[0..i-2] if (i >= 1) { max_ps = max(max_ps, prev); } } cout << flips << "\n"; return 0; }