Codeforces Round #373 (Div. 2) B
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.
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.
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.
- 5
rbbrr
- 1
- 5
bbbbb
- 2
- 3
rbr
- 0
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.
题意:给你颜色排列,和两种操作,一种是交换颜色,一种是改变颜色,问你最少的操作,可以使得颜色交替排列
解法:首先交替排列应该是rbrbrbrbrbrbr.. 或者brbrbrbrbr 那么讨论两种情况,一种是偶数位是r 一种是偶数位是b
再求出每种情况中,哪些位置是不符合的,比如rrbbbr 中r不在符合位置的有两个,b不在符合位置的也有两个,交换就是取两个的最小值,涂色是求它们的差值
两种情况再求一次最小值
- #include<bits/stdc++.h>
- using namespace std;
- int n, a[10000];
- int dp[100000][3];
- set<char>q;
- map<char,int>q1,q2;
- char s[100010];
- int main()
- {
- // string s;
- scanf("%d",&n);
- scanf("%s",s);
- int sum1,sum2;
- for(int i=0; i<n; i++)
- {
- if(i%2==0&&s[i]!='r')
- {
- q1[s[i]]++;
- }
- else if(i%2&&s[i]!='b')
- {
- q1[s[i]]++;
- }
- }
- sum1=fabs(q1['r']-q1['b'])+min(q1['r'],q1['b']);;
- q1.clear();
- //cout<<sum1<<endl;
- for(int i=0; i<n; i++)
- {
- if(i%2==0&&s[i]!='b')
- {
- q2[s[i]]++;
- }
- else if(i%2&&s[i]!='r')
- {
- q2[s[i]]++;
- }
- }
- //int Min2=min(q2['r'],q2['b']);
- sum2=fabs(q2['r']-q2['b'])+min(q2['r'],q2['b']);
- q2.clear();
- printf("%d\n",min(sum1,sum2));
- return 0;
- }
Codeforces Round #373 (Div. 2) B的更多相关文章
- Codeforces Round #373 (Div. 1)
Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...
- Codeforces Round #373 (Div. 2)A B
Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题
C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade —— 贪心 + 字符串处理
题目链接:http://codeforces.com/problemset/problem/719/C C. Efim and Strange Grade time limit per test 1 ...
- Codeforces Round #373 (Div. 2)
A,B,C傻逼题,就不说了. E题: #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...
- Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题
A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...
- 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E
http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...
随机推荐
- 解决 linux [Fedora] 升级 导致VMware启动出现"before you can run vmware workstation, serveral modules must be complied and loaded into the runing kernel" 而无法卸载
解决: 开机启动 进入 升级之前的内核系统 然后 执行卸载 VMware 命令 # vmware-uninstall You have gotten this message because you ...
- sdutoj 2154 Shopping
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...
- [转]jQueryEasyUI Messager基本使用
一.jQueryEasyUI下载地址 http://www.jeasyui.com/ 二.jQueryEasyUI Messager基本使用 1.$.messager.alert(title, msg ...
- Python学习总结10:获取shell输出结果
Python中获取shell命令的输出结果的常见方法如下几种: 1. import subprocess output = subprocess.Popen(['ls','-l'],stdout=su ...
- String.Format数字格式化参考
String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转) 数字 {0:N2} 12.36 数字 {0:N0} 13 货币 {0:c2} $12.36 货币 {0:c4 ...
- OpenCV cv::Mat类
using namespace cv; 1.Mat的声明: Mat m=Mat(rows, cols, type); Mat m=Mat(Size(width,height), type); type ...
- Fragement
package com.exmple.frage; import java.util.ArrayList; import java.util.Calendar; import java.util.Ha ...
- linux文件所属用户和组
使用chown命令可以修改文件或目录所属的用户: 命令:chown 用户 目录或文件名 例如:chown -R qq /home/qq (把home目录下的qq目录的拥有者改为qq用户) 使用chg ...
- oracle 序列 ,check约束
====================序列 //查询当前用户序列 select * from user_sequences //查询所有序列 select * from all_sequences; ...
- ubuntu屏幕分辨率问题
今天在ubuntu下工作时突然屏幕上下各出现了一个大概2厘米的黑条,感觉屏幕被横向拉长了,莫名其妙,开始以为简单的调整下分辨率就好了,在系统设置显示里面发现分辨率只有两个可选参数,并且对象为未知,由于 ...