首页 > 其他分享 >Educational Codeforces Round 149 (Rated for Div. 2) C. Best Binary String

Educational Codeforces Round 149 (Rated for Div. 2) C. Best Binary String

时间:2023-10-19 19:57:18浏览次数:36  
标签:std Binary Educational Rated reverse int cost ans dp

给一个字符串 \(s\) 包含 \(0, 1, ?\) 。

定义一个 \(01\) 串 \(s\) 的 \(cost\) 为:选择 \(s\) 的任意一个子段 \([l, r]\) 并 \(reverse\) 。将 \(s\) 变为一个非降序序列时的 \(reverse\) 最小次数即 \(cost\) 。

你可以让 \(s\) 的 \(?\) 换成 \(0/1\) ,使新 \(s\) 的 \(cost\) 最小。输出 \(cost\) 最小的 \(s\) 。

首先分析一个 \(01\) 串 \(s\) 的 \(cost\) ,即需要 \(reverse\) 多少次。不难和证明发现使 \(s\) 非降序需要 \(reverse\) 的次数等于 \(s\) 中 \(10\) 子串的个数。

可以使用 \(navie\) 的 \(dp\) 做法,\(dp_{i, j}\) 为考虑到前 \(i\) 个位置,以 \(j\) 结尾,可以产生的 \(10\) 串数量。

view
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int dp[300005][2], pre[300005][2];
void solve(){
	std::string s; std::cin >> s; int n = s.length(); s = " " + s;
	for (int i = 1; i <= n; i++) {
		dp[i][0] = dp[i][1] = 1 << 30;
		if (i == 1) {
			if (s[1] == '?') {
				dp[1][0] = dp[1][1] = 0;
			}
			else if (s[i] == '1') {
				dp[1][1] = 0;
			}
			else if (s[i] == '0') {
				dp[1][0] = 0;
			}
		}
		else {
			if (s[i] == '?') {
				dp[i][0] = std::min(dp[i - 1][0], dp[i - 1][1] + 1);
				pre[i][0] = dp[i - 1][0] < dp[i-1][1] + 1 ? 0 : 1;
				dp[i][1] = std::min(dp[i - 1][0], dp[i - 1][1]);
				pre[i][1] = dp[i - 1][0] < dp[i-1][1] ? 0 : 1;
			}
			else if (s[i] == '1') {
				dp[i][1] = std::min(dp[i - 1][0], dp[i - 1][1]);
				pre[i][1] = dp[i - 1][0] < dp[i-1][1] ? 0 : 1;
			}
			else if (s[i] == '0') {
				dp[i][0] = std::min(dp[i - 1][0], dp[i - 1][1] + 1);
				pre[i][0] = dp[i - 1][0] < dp[i-1][1] + 1 ? 0 : 1;
			}
		}
	}
//	std::cout << dp[n][0] << ' ' << dp[n][1] << '\n';
	std::vector<int> ans;
	int p = dp[n][0] < dp[n][1] ? 0 : 1; ans.push_back(p);
	for (int i = n; i >= 2; --i) {
		p = pre[i][p];
		ans.push_back(p);
	}
	std::reverse(ans.begin(), ans.end());
	int m = ans.size();
	for (int i = 0; i < m; i++) std::cout << ans[i]; std::cout << "\n";
}
		                
int main() {
	int _ = 1; std::cin >> _;
	while (_--) solve();
	return 0;
}

标签:std,Binary,Educational,Rated,reverse,int,cost,ans,dp
From: https://www.cnblogs.com/zsxuan/p/17774744.html

相关文章

  • 神经网络基础篇:详解二分类(Binary Classification)
    二分类注:当实现一个神经网络的时候,通常不直接使用for循环来遍历整个训练集(编程tips)举例逻辑回归逻辑回归是一个用于二分类(binaryclassification)的算法。首先从一个问题开始说起,这里有一个二分类问题的例子,假如有一张图片作为输入,比如这只猫,如果识别这张图片为猫,则输出标签......
  • Educational Codeforces Round 150 (Rated for Div. 2) B. Keep it Beautiful
    数组\(a=[a_1,a_2,\cdots,a_n]\)被称为是美丽的,如果可以将\([1,x]\)段移到\([x+1,n]\)段后面,\(x\geq0\),数组可以构成非降序。现在有一个数组\(a\)(一开始为空)和\(q\)个询问,第\(i\)个询问给一个正整数\(x_i\)。需要逐步执行以下操作。若\(x_i\)拼接......
  • Educational Codeforces Round 154 (Rated for Div. 2) B. Two Binary Strings
    给定两个长度相等的\(01\)字符串\(a\)和\(b\)。每个字符串都是以\(0\)开始以\(1\)结束。在一步操作中,你可以选择任意一个字符串:选择任意两个位置\(l,r\)满足\(s_l=s_r\),然后让\(\foralli\in[l,r],s_i=s_l\)。询问经过若干次操作后能否使得\(a=b......
  • Testing Round 16 (Unrated) B. Square?
    给定一个矩形,然后切成两个矩形。尺寸分别为\(a\timesb\),\(c\timesd\)。你需要确定开始的矩形是否可能是个正方形。假设初始矩形为正方形,则两个小矩形的长边是正方形的边长。不妨让\(a\geqb,c\geqd\)。只需判断\(a=c,a=b+d\)是否成立即可。view#includ......
  • Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices
    给一个\(n\)个整数的排列\(p_1,p_2,\cdots,p_n\),需要找到三个数\(i,j,k\)满足:\(1\leqi<j<k\leqn\)\(p_i<p_j\),\(p_j<p_k\)否则回答不可能。\(key\):若存在上述\(i,j,k\),则存在\(x\)满足\(p_{x-1}<p_{x},p_{x}>p_{x+1......
  • Landsat 8 Collection 2 Tier 1 calibrated top-of-atmosphere (TOA) reflectance
    Landsat8Collection2Tier1calibratedtop-of-atmosphere(TOA)reflectance.Calibrationcoefficientsareextractedfromtheimagemetadata.See Chanderetal.(2009) fordetailsontheTOAcomputation.Landsatsceneswiththehighestavailabledataqual......
  • 了解 MySQL 数据库的三大日志(redo log、undo log、binary log)
    前言MySQL中有以下几种日志,包括:redolog(重做日志)undolog(回滚日志)binarylog(二进制日志)errorlog(错误日志)slowquerylog(慢查询日志)generallog(一般查询日志)relaylog(中继日志)事务的特性:原子性(Atomicity):事务是最小的执行单位,不允许分割。事务的原子......
  • CF1204D2 Kirk and a Binary String (hard version) 题解
    CF1204D2KirkandaBinaryString(hardversion)题解分析先来分析\(01\)串的最长不下降子序列。全是\(0\)显然是不下降的,如果中间出现一个\(1\),为了维护不下降的性质,后面就只能全是\(1\)。一句话概括一下,\(0\)后面能跟\(0,1\),\(1\)后面只能跟\(1\)。现在来分析这......
  • Binary Tree Postorder Traversal
    SourceGivenabinarytree,returnthepostordertraversalofitsnodes'values.ExampleGivenbinarytree{1,#,2,3},1\2/3return[3,2,1].ChallengeCanyoudoitwithoutrecursion?题解1-递归首先使用递归便于理解。C++-Tra......
  • Educational Codeforces Round 116 (Rated for Div. 2) A. AB Balance
    给一个长为\(n\)的字符串\(s\),只包含\(0\)\(1\)两种字符。定义\(AB(s)\)是\(s\)中出现的\(01\)子串个数,\(BA(s)\)是\(s\)中出现的\(10\)子串个数。在一步操作中,可以选择一个字符进行异或。询问最小的操作次数使得\(AB(s)=BA(s)\)。显然连续的\(11\)或连......