Codeforces Round #646 (Div. 2) B. Subsequence Hate (思维,前缀和)
题意:给你一个只含有\(0\)和\(1\)的字符串,每次操作可以将\(0\)改成\(1\)或\(1\)改成\(0\),问最少操作多少次,使得子序列中不含有\(010\)和\(101\).
题解:仔细想一想不难发现,构造后的字符串要么全是\(1\)和\(0\),要么就是\(000....111\)和\(111...000\),我们对\(0\)求一个前缀和,判断一下这些情况,更新最小值即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
int t;
int pre[N];
string s;
int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>t;
while(t--){
cin>>s;
s=" "+s;
me(pre,0,sizeof(pre));
int len=s.size();
int cnt0=0,cnt1=0;
for(int i=1;i<len;++i){
if(s[i]=='0'){
cnt0++;
pre[i]=pre[i-1]+1;
}
else{
cnt1++;
pre[i]=pre[i-1];
}
}
int ans=min(cnt0,cnt1);
for(int i=1;i<len;++i){
ans=min(ans,pre[i]+(len-1-i-(pre[len-1]-pre[i])));
}
for(int i=1;i<len;++i){
ans=min(ans,i-pre[i]+pre[len-1]-pre[i]);
}
printf("%d\n",ans);
}
return 0;
}
Codeforces Round #646 (Div. 2) B. Subsequence Hate (思维,前缀和)的更多相关文章
- Codeforces Round #646 (Div. 2) B. Subsequence Hate(前缀和)
题目链接:https://codeforces.com/contest/1363/problem/B 题意 可以将 $01$ 串中的 $0$ 变为 $1$.$1$ 变为 $0$,问至少需要变换多少字符 ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #646 (Div. 2)【B. Subsequence Hate题解】
具体思路已经在代码注释中给出,这里不再赘述. #include<iostream> #include<algorithm> using namespace std; int t ...
- Codeforces Round #646 (Div. 2) 题解 (ABCDE)
目录 A. Odd Selection B. Subsequence Hate C. Game On Leaves D. Guess The Maximums E. Tree Shuffling ht ...
- Codeforces Round #646 (Div. 2) E. Tree Shuffling(树上dp)
题目链接:https://codeforces.com/contest/1363/problem/E 题意 有一棵 $n$ 个结点,根为结点 $1$ 的树,每个结点有一个选取代价 $a_i$,当前 $ ...
- Codeforces Round #646 (Div. 2) C. Game On Leaves(树上博弈)
题目链接:https://codeforces.com/contest/1363/problem/C 题意 有一棵 $n$ 个结点的树,每次只能取叶子结点,判断谁能最先取到结点 $x$ . 题解 除非 ...
- Codeforces Round #646 (Div. 2) A. Odd Selection(数学)
题目链接:https://codeforces.com/contest/1363/problem/A 题意 判断是否能从 $n$ 个数中选 $x$ 个数加起来和为奇数. 题解 首先 $n$ 个数中至少 ...
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 题解】
题意分析 关于这道题,意思就是两个人摘叶子,谁最后摘到编号为x的谁就赢了.既然是叶子,说明其最多只有一个分支,由于题目上说了是无向图,那就是度数小于等于的节点.也就是一步步移除度数小于等于的节点,直到 ...
- Codeforces Round #646 (Div. 2) C、Game On Leaves
题目链接:C.Game On Leaves 题意: 给你一个n个节点的无根树,你每次可以删除一个叶节点.如果谁先删除x号节点谁就赢了.两个人轮流操作 题解: 如果x号节点本身就是一个叶节点,那么谁先走 ...
随机推荐
- Memcached repcached 高可用
Memcached + repcached 高可用环境 repcached 就是一个让memcached的机器能够互为主从,前端可以加一台HAProxy,后端两台memcached互为主从后,写入任何 ...
- C语言指针-从底层原理到花式技巧,用图文和代码帮你讲解透彻
这是道哥的第014篇原创 目录 一.前言 二.变量与指针的本质 1. 内存地址 2. 32位与64位系统 3. 变量 4. 指针变量 5. 操作指针变量 5.1 指针变量自身的值 5.2 获取指针变量 ...
- 【Nginx】yum安装nginx
这里是nginx的yum安装源: centos7: rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- Android事件分发机制四:学了事件分发有什么用?
" 学了事件分发,影响我CV大法吗?" " 影响我陪女朋友的时间" " ..... " 前言 Android事件分发机制已经来到第四篇了,在 ...
- 网络编程-I/O复用
I/O模型 Unix下可用的I/O模型有五种: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll.epoll) 信号驱动式I/O(SIGIO) 异步I/O(POSIX的aio_系列函数 ...
- 什么是 MVC 模式
概述 MVC,即 Model 模型.View 视图,及 Controller 控制器. View:视图,为用高糊提供使用界面,与用户直接进行交互. Model:模型,承载数据,并对用户提交请求进行计算 ...
- Building a high performance JSON parser
Building a high performance JSON parser https://dave.cheney.net/high-performance-json.html
- Update Node Using a Package Manager nodesource
How to Update Node.js to Latest Version (Linux, Ubuntu, OSX, Others) - HostingAdvice.com https://www ...
- 回滚原理 Since database connections are thread-local, this is thread-safe.
mysql django 实践: django @transaction.atomic 机制分析 1.数据库清空表Tab 2.请求django-view @transaction.at ...