问题 C: Coloring Colorfully

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

N块瓦片从左到右排成一行。每个块的初始颜色由长度为N的字符串S表示。

如果S的第i个字符为0,则左边的第i个平铺将被漆成黑色,如果该字符为1,则漆成白色。

你想重新油漆一些瓷砖黑色或白色,使任何两个相邻的瓷砖有不同的颜色。

至少需要重新粉刷多少瓷砖才能满足条件?

Constraints
1≤|S|≤105
Si is 0 or 1.

输入

Input is given from Standard Input in the following format:
S

输出

Print the minimum number of tiles that need to be repainted to satisfy the condition.

样例输入
Copy

000

样例输出 Copy

1

提示

The condition can be satisfied by repainting the middle tile white\
解析:1.从前向后枚举时:如果两个都是相同的,就变后面的,因为保证前面是可以的那么后面也是行的
2.从后往前枚举一次,如果相同就变前面的
3.两种情况取最小值
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int INF=0x3f3f3f3f;
const int maxn=5e5;
char a[maxn];
char b[maxn];
int sum[maxn];
int len;
void inint(){
scanf("%s",a+);
len=strlen(a+);
for(int i=;i<=len;i++){
b[i]=a[i];
}
}
int main(){
inint();
int ans1=;
for(int i=;i<=len;i++){
if(a[i]==''&&a[i-]==''){
a[i]='';
ans1++;
}
else if(a[i]==''&&a[i-]==''){
a[i]='';
ans1++;
}
}
int ans2=;
for(int i=len-;i>=;i--){
if(b[i]==''&&b[i+]==''){
b[i]='';
ans2++;
}
else if(b[i]==''&&b[i+]==''){
b[i]='';
ans2++;
}
}
printf("%d",min(ans1,ans2));
}
 

Coloring Colorfully的更多相关文章

  1. Atcoder Beginner Contest 124 解题报告

    心态爆炸.本来能全做出来的.但是由于双开了Comet oj一个比赛,写了ABC就去搞那个的B题 还被搞死了. 回来写了一会D就过了.可惜比赛已经结束了.真的是作死. A - Buttons #incl ...

  2. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  3. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  4. Codeforces Round #369 (Div. 2) C. Coloring Trees DP

    C. Coloring Trees   ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...

  5. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  6. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  7. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

  8. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

随机推荐

  1. git Auto packing the repository in background for optimum performance

    遇到问题: git本地仓库,如果长时间不进行清理,几个月以后的某一天,可能拉取代码的时候突然提示你 git Auto packing the repository in background for ...

  2. Maven修改test/rsource的output folder报错Test source folder 'src/test/java'... is not also used for main s

    eclipse新建maven项目时候,只出来三个文件夹,然后大都督手动添加了缺失的src/test/resource 的文件夹,最后想修改一下 Output folder的路径为 (原来是     d ...

  3. dva-loading 实践用法

    dva 中页面过渡效果封装的很好,下面介绍常用的两个 js 库. 之前对 dva-loading 理解存在误区,认为只要在 index.js 中配置一下就没事了,事实上 dva-loading 只是提 ...

  4. kanbanflow的使用

    也许在工作中大家都听说过番茄工作法,就是每次在一个番茄钟25分钟内保持高度专注,并且在时间结束的时候会提醒你,然后稍作休息5分钟:此外,在产品迭代开发过程中常常会接受到不同的task:那么,我们是否可 ...

  5. Java改变引用数据类型的值

    Java改变引用数据类型的值 在Java中,引用数据类型的数据传递的是值(地址)的拷贝 对于以下代码 class BirthDate { private int day; private int mo ...

  6. Real-time Compressive Tracking

    这是RTC算法的文献blog Real-time Compressive Tracking Kaihua Zhang1, Lei Zhang1, Ming-Hsuan Yang2 1Dept. of ...

  7. 事件&vue修饰符

    JavaScript 事件 HTML事件是发生在HTML元素上的事情.当在HTML页面帐使用JavaScript时,javascript可以触发这些事件 HTML 事件 HTML事件可以是浏览器的行为 ...

  8. 牛客网——剑指offer(跳台阶以及变态跳台阶_java实现)

    首先说一个剪枝的概念: 剪枝出现在递归和类递归程序里,因为递归操作用图来表示就是一棵树,树有很多分叉,如果不作处理,就有很多重复分叉,会降低效率,如果能把这些分叉先行记录下来,就可以大大提升效率——这 ...

  9. [CQOI2007] 余数求和 - 整除分块

    \(\sum_{i=1}^n\;k\;mod\;i\) Solution \(\sum_{i=1}^n\;k\;mod\;i\\=\sum_{i=1}^n(k-i\lfloor{\frac{k}{i} ...

  10. SDOI2010 粟粟的书架 lg2468(可持久化,前缀和)

    题面见https://www.luogu.org/problemnew/show/P2468 然后这道题属于合二为一题,看一眼数据范围就能发现 首先我们先考虑50分,二维前缀和维护一下(反正我不记得公 ...