Codeforces 719B

题意:给出一串包含‘r‘和‘b‘的字符串,每次可以把r和b的位置互换,或者改变b为r,或变r为b,求最后能成为r,b间隔序列的操作的最少次数

30分钟内没什么正确方向,理解题解后,感觉,好巧妙的思维,可能是我想不到吧。

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4
 5 int n;
 6 char s[100005];
 7 int main()
 8 {
 9     while(~scanf("%d",&n))
10     {
11         int xr,yr,xb,yb;
12         xr=yr=xb=yb=0;
13         scanf("%s",&s);
14         for(int i=0;i<n;i++)
15         {
16             if(i%2)
17             {
18                 if(s[i]==‘r‘)xr++;  //奇数位r的数量
19                 else xb++;      //奇数位b的数量
20             }
21             else if(s[i]==‘r‘)yr++; //偶数位r的数量
22             else yb++;          //偶数位b的数量
23         }
24         int ans=min(max(xr,yb),max(yr,xb));  25         cout<<ans<<endl;                     26     //max(xr,yb):为了转换成"brbr..."需要的最少次数27     //只有xr和yb需要交换,而max取最大的,多出来的就只能染色了,所以能保证执行步数最少
28     }
29     return 0;
30 }
时间: 2024-10-12 18:26:31

Codeforces 719B的相关文章

CodeForces 719B. Anatoly and Cockroaches

链接: [http://codeforces.com/group/1EzrFFyOc0/contest/719/problem/B] 题意: cockroaches:蟑螂.有两种颜色red和black.输入n,另一行输入n个字符,r代表red,b代表black,那个人喜欢蟑螂颜色交替排列. 你有两种操作:1.swap任意两个字符.2.你可以用颜料把某种颜色变成另一种颜色.为了满足那个人最少需要几个操作. 思路: 交替排列有两种情况:1.rbrbrb.....2.brbrbr....分别计算这两种

CodeForces 719B Anatoly and Cockroaches (水题贪心)

题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <c

codeforces 719B:Anatoly and Cockroaches

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. An

CodeForces 719B Anatoly and Cockroaches 思维锻炼题

题目大意:有一排蟑螂,只有r和b两种颜色,你可以交换任意两只蟑螂的位置,或涂改一个蟑螂的颜色,使其变成r和b交互排列的形式.问做少的操作次数. 题目思路:更改后的队列只有两种形式:长度为n以r开头:长度为n以b开头.与初始串进行比较并统计改变次数记作ans,算出必须进行的涂色操作的次数step,我们可以把交换两只不同颜色的蟑螂的位置看做进行了两次涂改操作,其次数为(ans-step). 那么得出改变为模板串的最小操作次数为:step+(ans-step)/2; #include<iostream

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store