Codeforce219C-Color Stripe
2 seconds
256 megabytes
standard input
standard output
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
The first input line contains two integers n and k (1 ≤ n ≤ 5·105; 2 ≤ k ≤ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Print a single integer — the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
6 3
ABBACC
2
ABCACA
3 2
BBB
1
BAB 题意:给你一行n个元素,k种颜色,要求相邻的元素颜色不能相同,输出最少改变颜色的数量和n个元素最后的颜色(若有多种可能可任意输出一种)
注意:k==2时必定是奇偶位不相同,要选一个最少改变方案,所以要知道奇位放A要改变的数多还是偶位放A要改变的数多,在训练时卡在这种情况了,
其实一开始想到了ABBA这种情况,但当时的思路是只改变相同颜色的元素,没想到不同颜色元素也能改变,所以当时就假设如果有偶数个相同颜色的元素,其两边元素颜色必不同,结果就一直wrong answer on the test 15...
#include<bits/stdc++.h>
using namespace std;
const int amn=5e5+;
char mp[amn];
int a[amn],c[];
int main(){
int n,k;
ios::sync_with_stdio();
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>mp[i];
a[i]=mp[i]-'A'+;
}
int ans=,c1=,c2=;
if(k==){ ///k==2时必定是奇偶位不相同,要选一个最少改变方案,所以要知道奇位放A要改变的数多还是偶位放A要改变的数多
for(int i=;i<=n;i++){ /// 我们先假设奇位为A,偶位为B,因为有可能合法情况是偶位为A,奇数位为B,所以最后要比较哪个不合法的数量少,这样更改少的那个才能得到最少改变方案,故下面统计不合法数,
if(i%==){ ///若偶位为A,则A的不合法数加1,否则B的不合法数加1
if(mp[i]=='A')c1++;
else c2++;
}
else{ ///若偶位为A,则B的不合法数加1,否则A的不合法数加1
if(mp[i]=='A')c2++;
else c1++;
}
}
ans=min(c1,c2); ///选一个最小不合法数,得到最少改变方案
for(int i=;i<=n;i++){
if(ans==c1){ ///若偶数位为A是不合法的,要将奇位变为A,偶位变为B
if(i%)
mp[i]='A';
else
mp[i]='B';
}
else{
if(i%) ///若奇数为位A是不合法的,要将偶数位变为A,奇数位变为B
mp[i]='B';
else
mp[i]='A';
}
}
}
else{
a[n+]=;
memset(c,,sizeof c);
bool f=,d=;
int fr=,ta=,frc,mic,tac,it,cc;
for(int i=;i<=n;i++){
if(a[i]==a[i-]){
d=;
if(fr>ta){
fr=i-;
if(i->=){
frc=a[fr-];//cout<<frc<<'!'<<endl;
c[a[i]]=c[frc]=;
}
else{
c[a[i]]=;
f=;
}
mic=a[i];
ta=i+;
tac=a[ta];
c[tac]=;
}
else{ ta=i+;
tac=a[ta];
c[tac]=;
}
//printf("i:%d fr:%d ta:%d\n",i,fr,ta);
if(!f&&i==n){
ans+=(ta-fr)/;
it=fr+;
cc=frc;
while(it<ta){
a[it]=cc;
mp[it]=a[it]-+'A';
it+=;
}
break;
} }
else if(d){
//printf("i:%d fr:%d ta:%d tac:%d\n",i,fr,ta,tac);
d=;
ans+=(ta-fr)/;
if(f){
cc=tac;
if(ta>n)
for(int i=;i<=k;i++){
if(!c[i]){
cc=i;
break;
}
}
it=ta-;
while(it>){
a[it]=cc;
mp[it]=a[it]-+'A';
it-=;
}
f=;
}
else{
cc=frc;
//printf("---\ni:%d frc: %d tac: %d\n",i,frc,tac);
//for(int i=1;i<=26;i++)cout<<c[i]<<' ';
//cout<<"\n---\n";
if(frc==tac){
for(int i=;i<=k;i++){
if(!c[i]){
cc=i;
break;
}
}
}
//cout<<i<<'?'<<cc<<endl;
memset(c,,sizeof c);
it=fr+;
while(it<ta){
a[it]=cc;
mp[it]=a[it]-+'A';
it+=;
}
}
fr=ta;
ta--;
}
}
if(f){
ans+=(ta-fr)/;
for(int i=;i<=k;i++){
if(!c[i]){
cc=i;
break;
}
}
memset(c,,sizeof c);
it=fr+;
while(it<ta){
a[it]=cc;
mp[it]=a[it]-+'A';
it+=;
}
fr=ta;
ta--;
}
}
cout<<ans<<endl;
cout<<mp+<<endl;
}
Codeforce219C-Color Stripe的更多相关文章
- CodeForces 219C Color Stripe
Color Stripe Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- 1045. Favorite Color Stripe (30) -LCS允许元素重复
题目如下: Eva is trying to make her own color stripe out of a given one. She would like to keep only her ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- PAT 1045 Favorite Color Stripe[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
- PAT甲级1045. Favorite Color Stripe
PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...
- pat1045. Favorite Color Stripe (30)
1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 1045. Favorite Color Stripe (30) -LCS同意元素反复
题目例如以下: Eva is trying to make her own color stripe out of a given one. She would like to keep only h ...
- 1045 Favorite Color Stripe (30)(30 分)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
随机推荐
- 两篇很好的EPG相关文章
两篇很好的EPG相关文章 原文地址:http://blog.sina.com.cn/s/blog_53220cef0100pi8j.html 1 基于DVB-SI的数字有线电视机顶盒节目指南的设计实现 ...
- 7-42 jmu-python-找字符 (15 分)
输入一个字符串及字符,输出第一次出现该字符的位置. 输入格式: 行1:输入字符串 行2:输入一个字符 输出格式: 找到,输出对应位置,格式index=X的, X表示查找到位置 找不到,输出can't ...
- .NET异步程序设计之async&await
目录 0.背景引入 1.async和await基本语法 2.异步方法的执行顺序 3.取消一个异步操作 4.同步和异步等待任务 5.异步操作中的异常处理 6.多线程和异步的区分 7. 在 .NET MV ...
- Git将文件上传至Github过程
1.安装Git工具(在这里就不多说了) 2.我们需要先创建一个本地的版本库(其实也就是一个文件夹). 你可以直接在桌面右击新建文件夹,也可以右击打开Git bash命令行窗口通过命令来创建. 现在我通 ...
- 桌面运维之Windows快捷键,每一个工程师都是“快捷键”的工程师!
1.win快捷键 首先教大家win7新增的3D效果: Win + Tab 快速切换已打开的程序(和Alt+tab一样的效果) Win + Home 将所有使用中窗口以外的窗口最小化 Win + Spa ...
- Tomcat8优化--Apache JMeter测试
一.部署测试java web项目(压力测试环境搭建) 1.mysql环境 #切换到mysql目录 cd /usr/local/mysql #查看mysql环境 rpm -qa | grep -i my ...
- Flutter 裁剪类组件 最全总结
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 ClipRect ClipRect组件使用矩形裁剪子组件, ...
- IRM3800 红外遥控器解码 linux驱动
这一次还是接在 Cemera 上.用 中断引脚 EINT20 也就是 GPG12. 之前焊的 51 板子上有一个红外接收器. 请注意了,是 标准的 NEC 码规范:首次发送的是9ms的高电平脉冲,其后 ...
- 【音视频连载-001】基础学习篇- SDL 介绍以及工程配置
技术开发故事会连载 这是音视频基础学习系列的第一篇文章,主要讲解 SDL 是什么以及为什么要用到它,看似和音视频没啥卵关系,其实必不可少. SDL 简介 SDL 是 "Simple Dire ...
- JVM01——JVM内存区域的构成
从本文开始将为各位带来JVM方面的知识点,关注我的公众号「Java面典」了解更多Java相关知识点. JVM内存主要分为三部分线程私有(Thread Local).线程共享(Thread Shared ...