zoukankan      html  css  js  c++  java
  • 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.

    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 to alternate. 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.

    Examples
    Input
    5
    rbbrr
    Output
    1
    Input
    5
    bbbbb
    Output
    2
    Input
    3
    rbr
    Output
    0
    Note

    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.

    正解:贪心

    解题报告;

      Xlight他们都看成了只能交换相邻的,调了好久,论不看题的危害。

      考虑最终序列只有可能有2种情况,那么分别枚举,两个答案取一个min即可。

      考虑直接贪心,首先我们可以统计出有多少个错位的元素,最后直接把能交换的全部交换,否则就暴力染色就可以了。

     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 using namespace std;
    14 typedef long long LL;
    15 const int inf = (1<<30);
    16 const int MAXN = 100011;
    17 int n,a[MAXN];
    18 int cnt[3];
    19 int ans,ans2;
    20 
    21 inline int getint()
    22 {
    23     int w=0,q=0; char c=getchar();
    24     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
    25     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
    26 }
    27 
    28 inline void work(){
    29     n=getint(); char c;
    30     for(int i=1;i<=n;i++) {
    31     c=getchar(); while(c!='r' && c!='b') c=getchar();
    32     if(c=='b') a[i]=1; else a[i]=0;
    33     }
    34     int tag=1;  int minl;
    35     for(int i=1;i<=n;i++) {
    36     if(tag!=a[i]){
    37         cnt[tag]++;    
    38         if(cnt[tag^1]>0) {
    39         minl=min(cnt[tag^1],cnt[tag]);
    40         cnt[tag]-=minl; cnt[tag^1]-=minl;
    41         ans+=minl;
    42         } 
    43     }
    44     tag^=1;
    45     }
    46     minl=min(cnt[1],cnt[0]); ans+=minl; cnt[1]-=minl; cnt[0]-=minl;
    47     ans+=cnt[1]; ans+=cnt[0];
    48 
    49     tag=0;  cnt[1]=cnt[0]=0;
    50     for(int i=1;i<=n;i++) {
    51     if(tag!=a[i]){
    52         cnt[tag]++;    
    53         if(cnt[tag^1]>0) {
    54         minl=min(cnt[tag^1],cnt[tag]);
    55         cnt[tag]-=minl; cnt[tag^1]-=minl;
    56         ans2+=minl;
    57         } 
    58     }
    59     tag^=1;
    60     }
    61     minl=min(cnt[1],cnt[0]); ans2+=minl; cnt[1]-=minl; cnt[0]-=minl;
    62     ans2+=cnt[1]; ans2+=cnt[0];
    63 
    64     printf("%d",min(ans,ans2));
    65 }
    66 
    67 int main()
    68 {
    69     work();
    70     return 0;
    71 }
  • 相关阅读:
    ASP.NET 4.0 与 Entity Framework 4第四篇Entity Framework在三层架构中的使用
    ASP.NET 4.0 与 Entity Framework 4第一篇采用ModelFirst 开发方式创建数据库
    ASP.NET 4.0 与 Entity Framework 4第二篇使用Entity Framework 进行CRUD操作
    ASP.NET 4.0 与 Entity Framework 4第三篇使用Entity Framework调用存储过程
    Unity3D4.* NGUI制作动态字库
    Unity3D内存释放
    1112 Stucked Keyboard (20 分)(map)【回顾】
    1116 Come on! Let's C (20 分)(hash散列)
    1108 Finding Average (20 分)(字符串处理)
    1113 Integer Set Partition (25 分)(排序)
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5904324.html
Copyright © 2011-2022 走看看