【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin’s string represents Kevin’s score on one of the n questions of the olympiad—’1’ for a correctly identified cow and ‘0’ otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all ‘0’s in that substring to ‘1’s and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin’s results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
input
8
10000011
【题目链接】:http://codeforces.com/contest/604/problem/C
【题解】
先把连续的0串和1串压缩成一位.
这样整个字符串就是符合要求的01间隔出现的串了.记录每一位压缩了几个。
这个串的长度就是初始的间隔01串的长度.
接下来.
考虑以下几种情况.
1.
某一位压缩的个数>=3;
则可以在中间位置进行取反操作(一个数就可以);
这样就把01串的长度增加了2;(而2是一次操作能够增加的最多位数);
所以直接输出就可以了.
2.有连续两位,这两位的压缩位数分别都是2;
即
1100这样的情况
可以变成1010
也是增加2;
所以这种情况也直接输出答案.
3.有两位,这两位压缩数都是2,且它们中间间隔的位,压缩位数都是1。
即
1101011
这种情况2..len-1取反
1010101
可以发现也能增加2;
所以也可以直接输出
4.有一位它的压缩位数为2.则答案增加1
5.全都是1位。答案和原来一样(不会减少的,可以全都取反啊。。这个时候全是1为了哦);
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
char s[MAXN];
vector <pii> a;
int n;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
scanf("%s",s+1);
int i = 1;
bool judge1 = false,judge2 = false;
while (i <= n)
{
int j = i+1;
while (j <=n && s[j] == s[i]) j++;
a.pb(mp(s[i]-'0',j-i));
if (j-i>=3)
judge1 = true;
i = j;
}
int ans1 = a.size();
if (judge1)
{
cout << ans1+2 << endl;
return 0;
}
rep1(i,0,ans1-1)
{
if (i+1<=ans1-1)
{
int ma = max(a[i].se,a[i+1].se),mi = min(a[i].se,a[i+1].se);
if (ma ==2 && mi ==2)
{
cout << ans1+2<<endl;
return 0;
}
else
if (ma==2)
judge2 = true;
}
}
if (ans1==1 && a[0].se==2)
judge2 = true;
rep1(i,0,ans1-1)
if (a[i].se==2 && i+1<=ans1-1)
{
int j = i+1;
while (j <= ans1-1 && a[j].se==1) j++;
if (a[j].se == 2)
{
cout << ans1+2<<endl;
return 0;
}
else
judge2 = true;
}
if (judge2)
cout << ans1+1<<endl;
else
cout << ans1<<endl;
return 0;
}
【33.10%】【codeforces 604C】Alternative Thinking的更多相关文章
- Java Web程序设计笔记 • 【第10章 JSTL标签库】
全部章节 >>>> 本章目录 10.1 JSTL 概述 10.1.1 JSTL 简介 10.1.1 JSTL 使用 10.1.2 实践练习 10.2 核心标签库 10.2. ...
- 【2017.10.13 ROS机器人操作系统】ROS系统常用术语及资源
ROS机器人操作系统是一种后操作系统,提供了类似于软件开发中使用到的中间件的功能. ROS: Robot Operating System 机器人操作系统 Package: 功能包 Stack: 功能 ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 552B】Vanya and Books
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 586D】Phillip and Trains
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 608C】Chain Reaction
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【25.33%】【codeforces 552D】Vanya and Triangles
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【58.33%】【codeforces 747B】Mammoth's Genome Decoding
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- 使用MERGE语句同步表
先建好測试环境: USE TEMPDB GO IF OBJECT_ID('T1') IS NOT NULL DROP TABLE T1 IF OBJECT_ID('T2') IS NOT NULL D ...
- 任务调度(四)——ScheduledExecutorService替代Timer,实现多线程任务调度
上篇博文<任务调度(三)--Timer的替代品ScheduledExecutorService简介>已经对ScheduledExecutorService做了简介.事实上使用Schedul ...
- C#之使用app.config可记录数据,下次打开可读取记录的数据
一.背景 如下图所示,我通过open..按键打开了某个文件,之后我再把app给关闭掉,当再次打开app的时候,在textBox.Text上显示上一次打开的文件路径.通过使用app.config可以保存 ...
- 【Codeforces Round #452 (Div. 2) C】 Dividing the numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] n为偶数. l = 1, r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两 ...
- 优秀Java程序员必备10招
1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 ObjectOriented Programming(面向对象编程)这一概念是必须的.没有 OOPS 的坚实基础,就领会不 ...
- 设计模式六大原则(二):里氏替换原则(Liskov Substitution Principle)
里氏替换原则(LSP)由来: 最早是在 妖久八八 年, 由麻神理工学院得一个女士所提出来的. 定义: 1:如果对每一个类型为 T1的对象 o1,都有类型为 T2 的对象o2,使得以 T1定义的所有程序 ...
- Android控件:RadioButton(单选button)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- Lamp(linux+apache+mysql+php)环境搭建
Lamp(linux+apache+mysql+php)环境搭建 .安装apache2:sudo apt-get installapache2 安装完毕后.执行例如以下命令重新启动apache:sud ...
- bootstrap课程10 从外部引入视频到页面用什么标签
bootstrap课程10 从外部引入视频到页面用什么标签 一.总结 一句话总结:a.iframe标签:b.embed标签:c.video标签 1.bootstrap具有响应式特性的嵌入内容如何实现? ...
- SqlParameter的用法
SqlParameter的用法 关于Sql注入的基本概念,相信不需多说,大家都清楚,经典的注入语句是' or 1=1--单引号而截断字符串,“or 1=1”的永真式的出现使得表的一些信息被暴露出来,如 ...