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$的子序列 (这个子序列的相邻元素可以重复) ...
随机推荐
- gcc编译器常用选项的含义
-w: 关闭编译时的警告, 也就是编译后不显示任何warning,因此有时编译中会出现一些诸如数据转换之类的可忽略警告, -Wall: 显示编译后所有警告 -W: 显示警告,但是只是显示编译器认为的会 ...
- ASP制作建议留言板
<html> <head> <meta http-equiv="Content-Type" content="text/html;cha ...
- C++扬帆远航——11(斐波那契数列)
/* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:Feibo.cpp * 作者:常轩 * 微信公众号:Worldh ...
- [工具] Docker安装及portainer GUI
一.Docker Engine安装 1.安装流程 1)移除旧版本(如果有旧版本) yum remove docker \ docker-client \ docker-client-latest \ ...
- Spring Boot 2.x基础教程:使用MyBatis访问MySQL
之前我们已经介绍了两种在Spring Boot中访问关系型数据库的方式: 使用spring-boot-starter-jdbc 使用spring-boot-starter-data-jpa 虽然Spr ...
- PxCook+photoshop实现傻瓜式切图(推荐小白使用)
确定需求 刚入门前端的小伙伴经过一个阶段的学习,已经准备小试牛刀了.但看到设计师给出的psd图,又头疼了,天啊撸,怎么办,我不会切图啊.今天我就带领小白学习傻瓜式切图.包学包会.( ̄▽ ̄)" ...
- Ubuntu几秒钟没有任何操作自动黑屏
在鼠标或键盘30秒内没有做任何操作以后,显示器自动黑屏. 重新点击鼠标或键盘,屏幕唤醒. 设置中心各种设置方式都已经尝试过.无效. $xset -q // 执行该命令 Keyboard Control ...
- JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)
5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...
- vmware企业虚拟化平台vSphere管理与配置
├─1-CCIE-DC课程介绍.avi ├─2-vSphere-简介.avi ├─3-vSphere-新功能介绍.avi ├─4-vSphere-授权介绍.avi ├─5-vSphere-课程拓扑介绍 ...
- Serializable详解(1):代码验证Java序列化与反序列化
说明:本文为Serializable详解(1),最后两段内容在翻译上出现歧义(暂时未翻译),将在后续的Serializable(2)文中补充. 介绍:本文根据JDK英文文档翻译而成,本译文并非完全按照 ...