結果

問題 No.3114 0→1
ユーザー toku4388
提出日時 2025-04-20 01:31:11
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,680 bytes
コンパイル時間 3,139 ms
コンパイル使用メモリ 279,956 KB
実行使用メモリ 14,348 KB
最終ジャッジ日時 2025-04-20 01:31:17
合計ジャッジ時間 4,655 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
template <typename T>
bool chmin(T &a, const T &b) {
	if (b < a) {
		a = b;
		return true;
	}
	return false;
}
template <typename T>
bool chmax(T &a, const T &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
#ifdef _DEBUG
#define show(x)          \
	cerr << #x << " : "; \
	showVal(x)
template <typename T>
void showVal(const T &a) {
	cerr << a << endl;
}
template <typename T, typename U>
void showVal(const pair<T, U> &a) {
	cerr << a.first << " " << a.second << endl;
}
template <typename T>
void showVal(const vector<T> &a) {
	for (const T &v : a) cerr << v << " ";
	cerr << endl;
}
template <typename T, typename U>
void showVal(const vector<pair<T, U>> &a) {
	cerr << endl;
	for (const pair<T, U> &v : a) cerr << v.first << " " << v.second << endl;
}
template <typename T, typename U>
void showVal(const map<T, U> &a) {
	cerr << endl;
	for (const auto &v : a) cerr << "[" << v.first << "] " << v.second << endl;
}
template <typename T>
void showVal(const vector<vector<T>> &a) {
	cerr << endl;
	for (const vector<T> &v : a) showVal(v);
}
#else
#define show(x)
#endif
int main() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	vector<vector<int>> dp(n + 1, vector<int>(1 << 2, INF));
	dp[0][0b11] = 0;
	for (int i = 0; i < n; i++) {
		for (int b = 0; b < (1 << 2); b++) {
			for (int v = 0; v < 2; v++) {
				if (v == 0 && ((b >> 1) == 0 || (b & 1) == 0)) continue;
				chmin(dp[i + 1][((b << 1) | v) & 0b11], dp[i][b] + ((v == (s[i] - '0')) ? 0 : 1));
			}
		}
	}
	show(dp);
	int ans = INF;
	for (int b = 0; b < (1 << 2); b++) {
		chmin(ans, dp[n][b]);
	}
	cout << ans << endl;
	return 0;
}
0