问题 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. LIS 51Nod 1134 最长递增子序列

    给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10.   Input 第1行:1个 ...

  2. HTML/JavaScript实现地图以鼠标为圆心缩放和移动

    代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  3. 记一个js toUpperCase函数 大小写特性

    toUpperCase()是javascript中小写变大写的函数 "ı".toUpperCase() == 'I',"ſ".toUpperCase() == ...

  4. C#索引器学习笔记

    本笔记摘抄自:https://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html,记录一下学习过程以备后续查用. 索引器允许类或者结构的实 ...

  5. AcWing 1049. 大盗阿福

    //f[i,j]表示所有走了i步,且当前位于状态j的所有走法 j=1表示选第i个 j=0表示不选 //如果j=0 那么表示不选第i个 那么就可以从f[i-1,0]和f[i-1,1]转移过来 //如果j ...

  6. 题解【洛谷P1896】[SCOI2005]互不侵犯

    题面 棋盘类状压 DP 经典题. 我们考虑设 \(dp_{i,j,s}\) 表示前 \(i\) 行已经摆了 \(j\) 个国王,且第 \(i\) 行国王摆放的状态为 \(s\) 的合法方案数. 转移的 ...

  7. 【C语言】【欢哥TV】冒泡法排序视频演示

    fromB站 点击进入:Go

  8. hadoop 配置信息记录

    ssh-keygen  -t   rsa   -P  '' 192.168.157.148 hadoop01192.168.157.149 hadoop02 mkdir  /root/hadoopmk ...

  9. 小总结:fibonacci数的产生

    我写的一个固定的函数来嘞: ]={,}; void f() { ;i<;i++) { fib[i]=fib[i-]+fib[i-]; } } 1,1,2,3,5,8,13,21,34,55,.. ...

  10. ECMAScript基本语法——④变量

    简介 变量:一小块存储数据的内存空间先申请了一块内存空间,规定空间的存储类型,给空间赋值3, 想找到这个3可以通过内存空间的地址值,但是通过地址值太麻烦了,给这个空间起了一个名字a 通过这个a可以找到 ...