Codeforces 604C - Alternative Thinking(思维)

从Wi-Fi到蓝牙:手把手教你用Cadence Virtuoso搭建一个2.4GHz锁相环频率综合器(含PFD/CP/VCO模块设计) 本文详细介绍了如何在Cadence Virtuoso中设计一个2.4GHz锁相环频率综合器(PLL),涵盖从系统架构到模块级设计的全流程。重点解析了PFD、CP和VCO等核心模块的设计技巧与仿真验证方法,帮助模拟集成电路设计师掌握高频PLL的实现要点,满足Wi-Fi和蓝牙等无线通信标准的要求。 阅读详情

C. 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
output
5
input
2
01
output
2
Note
In the first sample, Kevin can flip the bolded substring ‘10000011’ and turn his string into ‘10011011’, which has an alternating subsequence of length 5: ‘10011011’.

In the second sample, Kevin can flip the entire string and still have the same score.

题意:
给出一个01串,求最长子串(不必相邻)的长度,要求每一位和相邻的都不一样.
可以对原有的01串的一个区间进行翻转.

解题思路:
翻转不会影响所求子串的长度(01相间的特性不会改变),但是翻转对于有连续相同的来说会使得01相间的长度++(11->10).
记录下连续相同的个数(但是不能大于二).

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
char a[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i = 0;i < n;i++)    cin>>a[i];
    int sum = 1;
    int cnt = 0;
    for(int i = 1;i < n;i++)    if(a[i] != a[i-1])  sum++;else  cnt++;
    cout<<sum+min(cnt,2);
    return 0;
}
自定义LayoutManager实现android-pile-layout滑动卡片堆叠效果 这是我的第一篇博客,总算有个契机开始写博客了,就从自定义LayoutManager开始吧。 前段时间看到一个项目(android-pile-layout),效果特别不错,这是效果图: 效果确实不错,我看了下实现,是用的自定义的ViewGroup实现的,计算什么还搞的挺复杂的,具体的没细看。作者说也尝试过用LayoutManager实现,但是没找到合适的数学模型,好吧,看到数学模型我 阅读详情

相关推荐

数值计算之 插值法(4)切比雪夫零点插值

数值计算之 插值法(4)切比雪夫零点插值前言插值点选取第一类切比雪夫多项式拉格朗日插值多项式的余项切比雪夫零点插值后记 前言 上篇插值法讨论了多项式插值的解,以及龙格现象。本篇将介绍一种在抽取节点时有效降低龙格现象的方法——切比雪夫零点插值。 插值点选取 插值多项式阶数较高时,在取值空间均匀取点,容易出现龙格现象。 即区间边缘的插值结果与原函数差异很大,而区间中央的插值结果相对较好。这表明,高阶多项式插值对区间中央的节点拟合好,而对两端节点拟合效果差。 自然而然会想到,在两端多采样一些节点,在中间少采样一

qq_41035283的博客 1万+

Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/problem/C Description Kevin has just recevied his disappointing results on the USA Identifi...

weixin_34072458的博客 185

Vue3+codemirror6实现公式(规则)编辑器

vue3+codemirror6+ts实现公式编辑器,可以插入标签、公式,也有补全提示

小阮的笔记本 5073

CodeForces 603A Alternative Thinking 题解

【题目大意】: 给你一个字符串,你可以使得某一个连续的01串翻转过来(将0变1,1变0),问最长的01相隔的子序列(可不连续,关于不连续可见样例)的长度为多少(原题及样例见下) Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes inp

初学者 1647

Matlab中eig内置函数转为C语言

本文记录一下如何将MATLAB中的eig函数转为C语言,即通过C语言求解矩阵的特征值与特征向量。[V,D]=eig(Rx)中V为矩阵Rx的特征向量,D为矩阵的特征值。其中Rx维度为12*12的复矩阵。 首先在MATLAB端创建入口函数保存为eigvalue.m: 打开MATLAB Coder,添加入口函数: 3.在定义输入类型窗口中,需要添加输入数据来训练...

u011441044的博客 3521

分组背包问题(MCKP)三大算法对比:贪心、Dyer-Zemel和动态规划谁更强?

本文深入对比了分组背包问题(MCKP)的三大主流算法:贪心算法、Dyer-Zemel算法和动态规划。通过分析各自的原理、实现及性能表现,揭示了贪心算法速度快但非最优,动态规划精确但受限于规模,而Dyer-Zemel算法则在效率与最优性间取得了最佳平衡,是解决中等至大规模MCKP问题的首选方案。

vscode6remote的博客 234

ACM-思维题-CF334div2 C Alternative Thinking 找规律

这里有题面 给你一个01串,可以将其中的部分连续子串进行反转(0变1,1变0),问最长01交替子串? 一开始看错题了,以为只能0变为1,特判了好多,结果发现其实就是: 先计算这个串里 最大(可不连续)的01交替子串 + 2(最多可以增加的数量) 最后和总长度比较,取最小 #include &amp;lt;bits/stdc++.h&amp;gt; using namespace std; i...

CS_W98的博客 854

Python 爬虫:获取网页数据的 5 种方法

requests:适用于简单的静态网页抓取,易于使用。requestslxml:适合需要高效解析大规模网页内容的情况,支持 XPath 和 CSS 选择器。Selenium:适用于动态网页(JavaScript 渲染)的抓取,模拟浏览器行为获取动态数据。Scrapy:强大的爬虫框架,适合大规模的网页抓取任务,支持异步请求和高级功能。PyQuery:基于 jQuery 语法,适合快速开发,提供简洁的 CSS 选择器语法。

想看什么文章都可以私信给我,综合性博客。 5524

2018网易交错01串

/* * 如果一个01串任意两个相邻位置的字符都是不一样的,我们就叫这个01串为交错01串。例如: "1","10101","0101010"都是交错01串。小易现在有一个01串s,小易想找出一个最长的连续子串,并且这个子串是一个交错01串。小易需要你帮帮忙求出最长的这样的子串的长度是多少。 输入描述:输入包括字符串s,s的长度length(1 ≤ length ≤ 50),字符串中只包含'0'和...

qq_41438016的博客 595

MMRotate 详细使用教程

MMRotate 详细使用教程 做旋转目标检测,怎么可以不知道如何使用MMRotate呢! github链接: https://github.com/open-mmlab/mmrotate 1 安装 创建虚拟环境并激活 conda create -n mmrotate python=3.7 -y conda activate mmrotate 安装 PyTorch 和 torchvision conda install pytorch torchvision -c pytorch 注意:确保

Sylvia_Lan的博客 1万+

基于STM32、HAL库的TCA9555PWR I/O扩展器驱动程序设计

define TCA9555_ADDR_BASE 0x20 // 基础地址 (A0=A1=A2=0)* @param direction: 方向 (0=输出, 1=输入)* @param direction: 方向 (0=输出, 1=输入)* @param polarity: 极性 (0=正常, 1=反转)* @param polarity: 极性 (0=正常, 1=反转)

corlin6688的博客 532

Codeforces 604 C Alternative Thinking【思维】

C. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kevin has just recevied his disappointing results on the

mengxiang000000 1329

C++(Qt)软件调试---Windows下Qt使用dump定位崩溃位置(1)

在C++/Qt程序崩溃时,往往很难找到崩溃的原因,如果是稳定复现的问题还好,但是多数情况遇见的问题是随机出现的,这时想要快速找到问题并解决就需要通过dump文件定位崩溃位置。

mahuifa的博客 1万+

CodeForces 604C Alternative Thinking(思维题)

题目链接:点击打开链接 题意:给出01字符串。可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所有的位取反。求经过处理后最多会得到多少次01变换(可以不连续)。 思路:思维题。模拟可以发现,增加01变换的方式有大致三种,一是在连续相同的字符串(这种子串记为s)内部找子串取反,这种能够增加01变换的s长度要大于等于3,如10001->10101;二是在s与其相邻非s取相应的部分取反,如

不忘初心 487

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

CodeForces - 604C Alternative Thinking (思维)

题目链接:http://codeforces.com/problemset/problem/604/C点击打开链接 C. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input

xuejye的博客 478

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Alternative Thinking CodeForces - 604C(思维)

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 sc

SunMoonVocano的博客 424

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Alternative Thinking(CodeForces - 604C)(思维)

Alternative Thinking(CodeForces - 604C)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

reset的博客 531

codeforces604C Alternative Thinking (脑洞大开 )

题目连接:http://codeforces.com/contest/604/problem/C  C. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standar

hkh746392783的专栏 1013

Alternative Thinking CodeForces - 604C

传送门 题意:给出一个01字符串,有一次可以翻转一段01子串的机会。求最长的交替子序列。 思路:当时我想的是,分别统计一开始为a[i]==1和 a[i]==0 的情况,取最大值,这么想多余了,可以不分。还有就是翻转子串的情况,当时也没有考虑的很清楚,其实就2种情况,2个连续,和3个连续。 #include<bits/stdc++.h> using namespace std; int n; string a; int sum1,sum2; bool flag1,flag2; int len;

qq_43721152的博客 175

【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 ...

AWCXV 82

Codeforces Round #334 (Div. 2) 604C Alternative Thinking(脑洞)

C. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kevin has just recevied his disappo

GKHack's Blog 825

cf #334 C. Alternative Thinking (思维题)

题目:http://codeforces.com/contest/604/problem/C 题意:给定长为n的01串,将一段连续的区间的值取反,求最长的01交替的串的长度(不一定要连续)。 分析:将一段连续的区间的值取反后,区间内最长交替串的长度不变,影响的只有两边的连接。所以,长度最多只能增加2。把所有可能增加交替长度的情况考虑一下就好了。 代码: #include using na

w20810的专栏 643

Codeforces Round #334 (Div. 2) C. Alternative Thinking

题目链接:http://codeforces.com/contest/604/problem/C 大致题意:给定一个01串以及其长度,求此串中符合形如“0101“或”101“这种01交错的序列(不必连续)的长度,并且你可以选定这个串中1段连续的子串进行反转(将0反转为1,将1反转为0),力求反转之后的串中所符合题意的序列的长度最大,并输出其长度。 (题目中”he decides that he...

weixin_30376509的博客 71

cf#334-C. Alternative Thinking-规律题

给出长度n的零一串,  一个最长的01交替串称为一个 合法串-(不必连续) 现在让你翻转一个连续区间,求翻转后的合法串的 最大长度。 先求出翻转前的合法串长度,    可以发现 对于一个区间,翻转后必然可以增加 合法串的2个长度(当加2后的串长度超过了n,那么表示原子串已经达到最长了,不需要再翻转) 因此答案是max(ans+2,2) #include #incl

缺氧 739

Codeforces 603A Alternative Thinking(字符串规律)

@(K ACMer)题意: 给你一个01字符串,你必须翻转其中的一个子串,问执行该操作后该字符串的最长交替子序列长度为多少? 分析 首先显然翻转整个字符串该字符串的01子序列长度不变,所以最次也是不变不会减小 然后我们队字符串进行压缩把连续的0和,连续的1压缩为一个,并记录它们各自压缩前的连续字符的个数。 显然交替子序列的个数最多增加两个,我们定义"00","11"这样的字符串叫做对。发现

JIBANCANYANG 1069

CF 604C Alternative Thinking#贪心

(~ ̄▽ ̄)~* #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN=100005; char s[MAXN]; int main() { int n; scanf("%d",&n); ...

weixin_30404405的博客 153
上一篇: Codeforces 604B - Alan Wake(二分)
下一篇: Codeforces 615B - Longtail Hedgehog(DP)
Snakeflute
博客等级 码龄11年 14粉丝 172原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值