问题 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. Cloud开发动态列的简单账表

    业务场景:客户需要根据过滤条件的不同显示不同的列.如下方式可以实现动态的列名. using System;using System.Collections.Generic;using System.L ...

  2. 假期学习【九】首都之窗百姓信件爬取代码优化以及处理 2020.2.7 Python

    今天对爬取程序进行了优化,修正了错误. 遇到了两个问题与错误: 1.忘记了对文件的读写设置格式,导致导入Hive数据库无法正常显示以及写入. 2.文件的读写操作有误导致数据量仅有应该有的1/2 不完整 ...

  3. Python 类方法、实例方法、静态方法的使用与及实例

    类方法 使用装饰器@classmethod 第一个参数必须是当前类对象,该参数名一般约定为“cls” (可修改但不建议)通过他来传递类的属性和方法(不能传实例的属性和方法) 调用:实例对象和类对象多可 ...

  4. [ZJOI2008] 骑士 - 基环树dp

    一类基环树dp都是这个套路吧 随便拆掉环上的一条边 然后跑树形dp,设\(f[i][0/1]\)表示以第\(i\)个人为根的子树,第\(i\)个人选或不选,能收获的最大值 以断点\(u,v\)为根分别 ...

  5. 一个vue的日历组件

    说明: 1.基于element-ui开发的vue日历组件. 地址 更新: 1.增加value-format指定返回值的格式2.增加头部插槽自定义头部 <ele-calendar > < ...

  6. Python-selenium,使用SenKey模块时所碰到的坑

    一.SenKey模块(模拟鼠标键盘操作) :python3中没有该模块,使用PyUserInput模块代替 二.PyUserInput模块安装前需要安装:pywin32和pyHook模块,pywin3 ...

  7. C语言运算符的优先级与结合性

    结合性:左结合是从左到右依次执行,右结合是从右到左依次执行. 优先级 运算符 名称或作用 运算类型 结合方向 特点 1 () [] -> . 小括号运算符 下标运算符 指向结构成员运算符 结构成 ...

  8. python面试的100题(21)

    正则表达式 94.请写出一段代码用正则匹配出ip? ip地址的生成规则. IP地址,是由32位数字二进制转为四个十进制的字符串组成. 怎么转化?下面讲解: 二进制:111111111111111111 ...

  9. js获取自定义data属性

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. [HEOI2015] 小Z的房间 - 矩阵树定理

    #include <bits/stdc++.h> using namespace std; #define int long long const int N = 105; const i ...