• 题意:给你一个只含有\(0\)和\(1\)的字符串,每次操作可以将\(0\)改成\(1\)或\(1\)改成\(0\),问最少操作多少次,使得子序列中不含有\(010\)和\(101\).

  • 题解:仔细想一想不难发现,构造后的字符串要么全是\(1\)和\(0\),要么就是\(000....111\)和\(111...000\),我们对\(0\)求一个前缀和,判断一下这些情况,更新最小值即可.

  • 代码:

    1. #include <iostream>
    2. #include <cstdio>
    3. #include <cstring>
    4. #include <cmath>
    5. #include <algorithm>
    6. #include <stack>
    7. #include <queue>
    8. #include <vector>
    9. #include <map>
    10. #include <set>
    11. #include <unordered_set>
    12. #include <unordered_map>
    13. #define ll long long
    14. #define fi first
    15. #define se second
    16. #define pb push_back
    17. #define me memset
    18. const int N = 1e6 + 10;
    19. const int mod = 1e9 + 7;
    20. const int INF = 0x3f3f3f3f;
    21. using namespace std;
    22. typedef pair<int,int> PII;
    23. typedef pair<ll,ll> PLL;
    24. int t;
    25. int pre[N];
    26. string s;
    27. int main() {
    28. ios::sync_with_stdio(false);cin.tie(0);
    29. cin>>t;
    30. while(t--){
    31. cin>>s;
    32. s=" "+s;
    33. me(pre,0,sizeof(pre));
    34. int len=s.size();
    35. int cnt0=0,cnt1=0;
    36. for(int i=1;i<len;++i){
    37. if(s[i]=='0'){
    38. cnt0++;
    39. pre[i]=pre[i-1]+1;
    40. }
    41. else{
    42. cnt1++;
    43. pre[i]=pre[i-1];
    44. }
    45. }
    46. int ans=min(cnt0,cnt1);
    47. for(int i=1;i<len;++i){
    48. ans=min(ans,pre[i]+(len-1-i-(pre[len-1]-pre[i])));
    49. }
    50. for(int i=1;i<len;++i){
    51. ans=min(ans,i-pre[i]+pre[len-1]-pre[i]);
    52. }
    53. printf("%d\n",ans);
    54. }
    55. return 0;
    56. }

Codeforces Round #646 (Div. 2) B. Subsequence Hate (思维,前缀和)的更多相关文章

  1. Codeforces Round #646 (Div. 2) B. Subsequence Hate(前缀和)

    题目链接:https://codeforces.com/contest/1363/problem/B 题意 可以将 $01$ 串中的 $0$ 变为 $1$.$1$ 变为 $0$,问至少需要变换多少字符 ...

  2. 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 ...

  3. Codeforces Round #646 (Div. 2)【B. Subsequence Hate题解】

    具体思路已经在代码注释中给出,这里不再赘述. #include<iostream> #include<algorithm> using namespace std; int t ...

  4. Codeforces Round #646 (Div. 2) 题解 (ABCDE)

    目录 A. Odd Selection B. Subsequence Hate C. Game On Leaves D. Guess The Maximums E. Tree Shuffling ht ...

  5. Codeforces Round #646 (Div. 2) E. Tree Shuffling(树上dp)

    题目链接:https://codeforces.com/contest/1363/problem/E 题意 有一棵 $n$ 个结点,根为结点 $1$ 的树,每个结点有一个选取代价 $a_i$,当前 $ ...

  6. Codeforces Round #646 (Div. 2) C. Game On Leaves(树上博弈)

    题目链接:https://codeforces.com/contest/1363/problem/C 题意 有一棵 $n$ 个结点的树,每次只能取叶子结点,判断谁能最先取到结点 $x$ . 题解 除非 ...

  7. Codeforces Round #646 (Div. 2) A. Odd Selection(数学)

    题目链接:https://codeforces.com/contest/1363/problem/A 题意 判断是否能从 $n$ 个数中选 $x$ 个数加起来和为奇数. 题解 首先 $n$ 个数中至少 ...

  8. Codeforces Round #646 (Div. 2)【C. Game On Leaves 题解】

    题意分析 关于这道题,意思就是两个人摘叶子,谁最后摘到编号为x的谁就赢了.既然是叶子,说明其最多只有一个分支,由于题目上说了是无向图,那就是度数小于等于的节点.也就是一步步移除度数小于等于的节点,直到 ...

  9. Codeforces Round #646 (Div. 2) C、Game On Leaves

    题目链接:C.Game On Leaves 题意: 给你一个n个节点的无根树,你每次可以删除一个叶节点.如果谁先删除x号节点谁就赢了.两个人轮流操作 题解: 如果x号节点本身就是一个叶节点,那么谁先走 ...

随机推荐

  1. Memcached repcached 高可用

    Memcached + repcached 高可用环境 repcached 就是一个让memcached的机器能够互为主从,前端可以加一台HAProxy,后端两台memcached互为主从后,写入任何 ...

  2. C语言指针-从底层原理到花式技巧,用图文和代码帮你讲解透彻

    这是道哥的第014篇原创 目录 一.前言 二.变量与指针的本质 1. 内存地址 2. 32位与64位系统 3. 变量 4. 指针变量 5. 操作指针变量 5.1 指针变量自身的值 5.2 获取指针变量 ...

  3. 【Nginx】yum安装nginx

    这里是nginx的yum安装源: centos7: rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...

  4. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  5. Android事件分发机制四:学了事件分发有什么用?

    " 学了事件分发,影响我CV大法吗?" " 影响我陪女朋友的时间" " ..... " 前言 Android事件分发机制已经来到第四篇了,在 ...

  6. 网络编程-I/O复用

    I/O模型 Unix下可用的I/O模型有五种: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll.epoll) 信号驱动式I/O(SIGIO) 异步I/O(POSIX的aio_系列函数 ...

  7. 什么是 MVC 模式

    概述 MVC,即 Model 模型.View 视图,及 Controller 控制器. View:视图,为用高糊提供使用界面,与用户直接进行交互. Model:模型,承载数据,并对用户提交请求进行计算 ...

  8. Building a high performance JSON parser

    Building a high performance JSON parser https://dave.cheney.net/high-performance-json.html

  9. Update Node Using a Package Manager nodesource

    How to Update Node.js to Latest Version (Linux, Ubuntu, OSX, Others) - HostingAdvice.com https://www ...

  10. 回滚原理 Since database connections are thread-local, this is thread-safe.

    mysql django 实践: django @transaction.atomic 机制分析  1.数据库清空表Tab 2.请求django-view        @transaction.at ...