codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同。

解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到。

解题代码:

  1 // File Name: 219c.cpp
  2 // Author: darkdream
  3 // Created Time: 2014年07月26日 星期六 15时45分56秒
  4
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24
 25 using namespace std;
 26 char str[1000000];
 27 int main(){
 28     int n , k ;
 29     scanf("%d %d",&n,&k);
 30     scanf("%s",str);
 31     int len = strlen(str);
 32     int sum  = 0 ;
 33     if(k == 2)
 34     {
 35        int tempa,tempb;;
 36        tempa = tempb = 0 ;
 37        for(int i = 0 ;i < len ;i ++)
 38        {
 39           if(i %2 == 0)
 40           {
 41               if(str[i] == ‘A‘)
 42                   tempa ++;
 43               else tempb ++;
 44           }else {
 45              if(str[i] == ‘B‘)
 46                  tempa ++ ;
 47              else tempb ++ ;
 48           }
 49        }
 50        if(tempa < tempb)
 51        {
 52           printf("%d\n",tempa);
 53           for(int i = 0 ;i < len ;i ++)
 54           {
 55             if(i % 2 == 0 )
 56                 printf("B");
 57             else printf("A");
 58           }
 59        }else{
 60           printf("%d\n",tempb);
 61           for(int i = 0 ;i < len ;i ++)
 62           {
 63             if(i % 2 == 0 )
 64                 printf("A");
 65             else printf("B");
 66           }
 67
 68        }
 69        return 0 ;
 70     }
 71     for(int i = 1 ;i < len ;i ++)
 72     {
 73        if(str[i] == str[i-1])
 74        {
 75            int ok = 0 ;
 76            for(int j = 0 ;j < k ;j ++)
 77            {
 78               char t = j + ‘A‘;
 79               if(t != str[i-1] && t != str[i+1] )
 80               {
 81                  str[i] = t ;
 82                  ok = 1;
 83                  break;
 84               }
 85            }
 86            if(ok == 0 )
 87            for(int j = 0 ;j < k ;j ++)
 88            {
 89              char t = j +‘A‘;
 90              if(t != str[i-1])
 91              {
 92                str[i] = t;
 93                break;
 94              }
 95            }
 96            sum ++ ;
 97        }
 98     }
 99     printf("%d\n",sum);
100     puts(str);
101 return 0;
102 }

codeforces219C - Color Stripe DP+贪心,布布扣,bubuko.com

时间: 2024-12-17 18:03:53

codeforces219C - Color Stripe DP+贪心的相关文章

PAT 甲级 1045 Favorite Color Stripe(DP)

题目链接 Favorite Color Stripe 题意:给定A序列和B序列,你需要在B序列中找出任意一个最长的子序列,使得这个子序列也是A的子序列 (这个子序列的相邻元素可以重复) 只要求出这个子序列长度的最大值即可 很基础的DP题,设f[i]为当前以a[i]结尾的子序列的长度的最大值,扫描B序列的每个元素时实时更新即可. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i

codeforces349B - Color the Fence 贪心+DP

题意:1-9每个字母需要消耗ai升油漆,问你用油漆最多刻意画多大的数字 解题思路: 首先我们要贪心,可以知道我最优是先使我们位数尽可能长,然后才是就是位数长的情况下使得从最高位开始尽可能大.所以要取满足最长位最小的那个数,方便我们DP 解题代码: 1 // File Name: 349b.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月24日 星期四 21时40分13秒 4 5 #include<vector> 6 #include&

贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

题目传送门 1 /* 2 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 3 同时不和下一个相等就是最优的解法 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 5e5 + 10; 11 const int IN

【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个正整数L(<=10000),代表条带的长度,接着输入L个正整数表示条带上的颜色的编号.输出以喜爱颜色顺序排列的最长子串长度(不必每种颜色都有,只保证相对位置相同,同种颜色可连续). 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>

PAT 1045. Favorite Color Stripe

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe. It is

PAT1045. Favorite Color Stripe

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe. It is

1045 Favorite Color Stripe (最长不下降子序列 LIS)

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe. It is

PAT 1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30) Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J