[codeforces] B - Anatoly and Cockroaches 贪心
来源:互联网 发布:淘宝团队代卖产品 编辑:程序博客网 时间:2024/06/10 09:50
Description
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.
Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line toalternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.
Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches.
The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.
Output
Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.
Sample Input
5rbbrr
1
5bbbbb
2
3rbr
0
Hint
In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.
In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.
In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.
先涂色,计算r和b分别被涂了多少次,然后取大数为答案,因为如果一r一b被涂,就可以直接交换。
#include <iostream> #include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)const int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;using namespace std;typedef long long ll;typedef unsigned long long ull; inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}int main(){char str[200010];memset(str,0,sizeof(str));int n,t,i,j,jiayi=0;;int dian =0,flag;//n = read();//t = read();scanf("%d %d",&n,&t);flag = n;//getchar();scanf("%s",str+1);for(i=1;i<=n;i++)if(str[i] == '.')dian = i;for(i=dian+1;i<=n;i++){if(str[i]>='5'){flag = i;break;}}if(i==n+1){puts(str+1);return 0;}while(t--){str[flag] = '0';for(i=flag-1;i>dian;i--){str[i]++;if(str[i]<':')break;if(str[i]==':')str[i]='0';}if(i==dian){jiayi = 1;break;}if(str[i]>='5')flag = i;else {flag = i;break;}}if(jiayi){for(i=dian-1;i>=0;i--){str[i]++;if(str[i]<':')break;if(str[i]==':'){str[i]='0';}}if(str[0])cout << '1';for(i=1;i<dian;i++)cout << str[i];return 0;}for(i=1;i<=flag;i++){cout << str[i];}return 0;}
- [codeforces] B - Anatoly and Cockroaches 贪心
- 【Codeforces Round #373 (Div. 2) 】【贪心】B. Anatoly and Cockroaches
- Codeforces 719B B. Anatoly and Cockroaches
- 【31.58%】【codeforces 719B】 Anatoly and Cockroaches
- Anatoly and Cockroaches Codeforces Round#373-B
- Codeforces-719B-Anatoly and Cockroaches
- Codeforces 719B. Anatoly and Cockroaches
- CodeForces 719B. Anatoly and Cockroaches
- Codeforces 719B-Anatoly and Cockroaches
- Codeforces 719B Anatoly and Cockroaches【思维】
- codeforces 719B:Anatoly and Cockroaches(思维)
- B. Anatoly and Cockroaches
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 贪心、字符串
- codeforces373B+Anatoly and Cockroaches+贪心
- cf--B - Anatoly and Cockroaches
- CodeForces 719B Anatoly and Cockroaches(小强排序)
- codeforces 716 B. Anatoly and Cockroaches(greedy)
- Codeforces Round #373 (Div. 2) B.Anatoly and Cockroaches
- 一心多用多线程-Thread的interrupt机制
- web服务器boa代码分析
- 何时可以买房.py
- eclipse启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
- UVa 11549 Calculator Conundrum(STL map)
- [codeforces] B - Anatoly and Cockroaches 贪心
- VS 自定义代码段 自定义注释
- Android ContentProvider 使用
- Rails uva514
- bootstrap 学习之(二)------ 排版
- Maven 远程仓库 之 中央存储库
- Docker桥接网络
- Fastlane实战(一):移动开发自动化之道
- Java 缺少X11