[Codeforces #608 div2]1272B Blocks
Description
There are nnn blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don’t have to minimize the number of operations, but it should not exceed 3⋅n3⋅n3⋅n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n(2≤n≤200)n(2≤n≤200)n(2≤n≤200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either “W” or “B”. If the i-th character is “W”, then the i-th block is white. If the i-th character is “B”, then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print −1−1−1.
Otherwise, print an integer k(0≤k≤3⋅n)k(0≤k≤3⋅n)k(0≤k≤3⋅n) — the number of operations. Then print kkk integers p1,p2,…,pk(1≤pj≤n−1)p_1,p_2,…,p_k (1≤p_j≤n−1)p1,p2,…,pk(1≤pj≤n−1), where pjp_jpj is the position of the left block in the pair of blocks that should be affected by the jjj-th operation.
If there are multiple answers, print any of them.
题意
给定一串黑白文本,每次可以将其中相邻2个颜色翻转,求一个可行的操作序列使得操作后颜色相同。
如果不能找到输出-1
思路
一开始看到n<=200n<=200n<=200直接打了一发爆搜+记忆化,然后MLE炸到飞起……
正解是假定操作后全白,从头扫到尾一次,假定全黑,从头扫到尾一次,看看能否成功。
比如:我们要全白,而此时颜色是 黑白黑黑黑
可以将黑视作高台阶,白视作低台阶,然后一路推过去,最后能推平就可以了。
推平位置1后,往后找到位置2,推平位置2后,2 3都平了,再往后遍历找到位置4,推平位置4后,全部推平,合法。
因此操作序列就为:1 2 4
显然这样操作只会有2种结果:全平或者最后一个不平。
全黑全白两个都扫一遍就好了,复杂度O(n)O(n)O(n)
Code
#include <cstdio>
#include <cstring>
using namespace std;
int n,len;
char all[201];
char temp[201];
int path[201];
int tot;
bool checkblack()
{
memcpy(temp,all,sizeof(all));
tot = 0;
for(int i = 1;i<len;++i)
{
if(temp[i] == 'W')
{
temp[i] = 'B';
temp[i+1] = (temp[i+1] == 'W' ? 'B' : 'W');
path[++tot] = i;
}
}
return temp[len] == 'B';
}
bool checkwhite()
{
memcpy(temp,all,sizeof(all));
tot = 0;
for(int i = 1;i<len;++i)
{
if(temp[i] == 'B')
{
temp[i] = 'W';
temp[i+1] = (temp[i+1] == 'W' ? 'B' : 'W');
path[++tot] = i;
}
}
return temp[len] == 'W';
}
int main()
{
scanf("%d",&n);
scanf("%s",all+1);
len = strlen(all+1);
if(checkblack() || checkwhite())
{
printf("%d\n",tot);
for(int i =1 ;i<=tot;++i)
printf("%d ",path[i]);
}
else
printf("-1");
return 0;
}
[Codeforces #608 div2]1272B Blocks的更多相关文章
- [Codeforces #608 div2]1271D Portals
Description You play a strategic video game (yeah, we ran out of good problem legends). In this game ...
- [Codeforces #608 div2]1271C Shawarma Tent
Description The map of the capital of Berland can be viewed on the infinite coordinate plane. Each p ...
- [Codeforces #608 div2]1271A Suits
Description A new delivery of clothing has arrived today to the clothing store. This delivery consis ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
随机推荐
- 一步步动手实现高并发的Reactor模型 —— Kafka底层如何充分利用多线程优势去处理网络I/O与业务分发
一.从<Apeche Kafka源码剖析>上搬来的概念和图 Kafka网络采用的是Reactor模式,是一种基于事件驱动的模式.熟悉Java编程的读者应该了解Java NIO提供了Reac ...
- SystemC中文教程一
SystemC是什么 首先, SystemC不是一门新的语言,而是基于C++开发的library:因此,你所熟悉的C++知识都可以在SystemC建模时使用:理论上来说,SystemC library ...
- day11 作业
# 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), # 要求登录成功一次,后续的函数都无需再输入用户名和密码 # FLAG = False # def login(func): ...
- windows下代码规范检测工具sonarqube安装与使用,含与maven的结合
一.首先下载sonarqube 地址 : https://www.sonarqube.org/downloads/ (最新版本支持java11+,博主下载支持java8的版本7.7), 下载S ...
- Codeforces1300C-Anu Has a Function
定义一个函数f(x,y), f(x,y) = x|y - y,给你一个数列,a1,a2,,an问如何排列能使f(f(f(a1,a2),a3),````,an)答案最大,我们将f(x,y)变形,就是f( ...
- Spring Cloud入门-Nacos实现注册和配置中心(Hoxton版本)
文章目录 摘要 Nacos简介 使用Nacos作为注册中心 安装并运行Nacos 创建应用注册到Nacos 负载均衡功能 使用Nacos作为配置中心 创建nacos-config-client模块 在 ...
- python 通过UDP传输文件
使用一个简单的python脚本将一个本地文件以码流的形式,通过UDP协议发送到对端: import socket import os import stat import struct MAX_P ...
- 延迟加载以及mybatis一级二级缓存
延迟加载 延迟加载:在真正使用数据时才发起查询,不用的时候不查询,又叫按需查询(懒加载) 立即加载:不管用不用,只要调用方法,直接发起查询 表关系:一对多 多对一 一对一 ...
- SqlCommand的ExecuteReader方法----转载
SqlCommand的ExecuteReader方法 原创 小道 2018-08-28 17:32:01 阅读 1353 次 评论 0 条 摘要: 用于执行查询语句,并返回一个DataReader ...
- postgresql shell脚本传递参数并执行sql脚本并
参考: https://stackoverflow.com/questions/7389416/postgresql-how-to-pass-parameters-from-command-line ...