結果

問題 No.884 Eat and Add
ユーザー okok
提出日時 2019-09-13 23:16:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 1,000 ms
コード長 1,668 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 88,788 KB
実行使用メモリ 16,100 KB
最終ジャッジ日時 2023-09-17 15:19:32
合計ジャッジ時間 1,871 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 25 ms
15,844 KB
testcase_04 AC 26 ms
16,100 KB
testcase_05 AC 26 ms
15,912 KB
testcase_06 AC 25 ms
15,912 KB
testcase_07 AC 24 ms
15,896 KB
testcase_08 AC 23 ms
15,896 KB
testcase_09 AC 24 ms
15,936 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}



signed main(){
	// cin.tie(0);
	// ios::sync_with_stdio(false);
	// cout<<fixed<<setprecision(10);
	
	string N;
	vector<int> con;
	vector<vector<int>> dp;
	
	cin>>N;
	
	N = "0" + N;
	
	con.resize(N.size()+2, 0);
	for(int i = N.size()-1; i >= 0; i--){
		if(i == N.size()-1){
		} else {
			if(N[i] != N[i+1]){
				con[i] = 0;
			} else {
				con[i] = con[i+1] + 1;
			}
		}
	}
	// for(int i = 0; i < N.size(); i++){
		// cout<<"i = "<<i<<" con = "<<con[i]<<endl;
	// }
	// cout<<"D"<<endl;	
	// cout<<"A"<<endl;
	dp.assign(N.size()+10, vector<int>(2, INF));
	// cout<<"B"<<endl;
	dp[0][0] = 0;
	// cout<<"C"<<endl;
	for(int i = N.size()-1, j = 0; i >= 0; i--, j++){
		// cout<<i<<" N = "<<N[i]<<endl;
		if(N[i] == '0'){
			dp[j+1][0] = min(dp[j+1][0],min(dp[j][0], dp[j][1]+1));
			dp[j+1][1] = min(dp[j+1][1],min(dp[j][0]+1, dp[j][1]+1));
		} else if(N[i] == '1'){
			// dp[j][0]+1;
			// dp[j][1];
			// dp[j+con[i]+1][1];
			// cout<<"con = "<<con[i]<<" j = "<<j<<endl;
			// = min(dp[j][0]+1, dp[j][1]);
			// dp[j+con[i]+1][1] = min(dp[j+con[i]+1][1],min(dp[j][0]+1, dp[j][1]));
			dp[j+1][0] = min(dp[j+1][0],dp[j][0] + 1);
			dp[j+1][1] = min(dp[j+1][1],min(dp[j][1],dp[j][0]+1));
		}
		// cout<<"dp "<<dp[j][0]<<" "<<dp[j][1]<<endl;
		// cout<<i<<" <> "<<endl;
	}
	cout<<dp[N.size()][0]<<endl;
	
	// cout<<"E "<<endl;
	return 0;
}
0