結果
| 問題 |
No.3114 0→1
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-18 21:23:38 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,201 bytes |
| コンパイル時間 | 808 ms |
| コンパイル使用メモリ | 71,652 KB |
| 実行使用メモリ | 22,600 KB |
| 最終ジャッジ日時 | 2025-04-18 21:23:46 |
| 合計ジャッジ時間 | 7,190 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | TLE * 1 -- * 29 |
ソースコード
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9;
int main() {
int N;
string S;
cin >> N >> S;
// 差の範囲は [-N, N] → インデックスに +N のオフセットをかけて 0~2N
int offset = N;
vector<int> dp(2 * N + 1, INF);
dp[offset] = 0; // 初期状態:差 0, 操作数 0
for (char c : S) {
vector<int> next_dp(2 * N + 1, INF);
for (int b = -N; b <= N; ++b) {
int idx = b + offset;
if (dp[idx] == INF) continue;
// そのまま使う場合
if (c == '1') {
next_dp[idx + 1] = min(next_dp[idx + 1], dp[idx]);
} else {
next_dp[idx - 1] = min(next_dp[idx - 1], dp[idx]);
// 0 → 1 に変える場合(操作を使う)
next_dp[idx + 1] = min(next_dp[idx + 1], dp[idx] + 1);
}
}
dp = next_dp;
}
// 最終的に、差 b >= 0 の最小値を探す(b + N >= N)
int ans = INF;
for (int b = 0; b <= N; ++b) {
ans = min(ans, dp[b + offset]);
}
cout << ans << endl;
return 0;
}