Codeforces 603A Alternative Thinking

题意:给你一个01串,必须替换一次,且替换的为子串。问换完后,最大01串长度。

 1 #include <bits/stdc++.h>
 2 typedef long long ll;
 3 using namespace std;
 4 int main()
 5 {
 6     int n,sum = 1;
 7     string s;
 8     cin >> n >> s;
 9     char last = s[0];
10     for(int i = 1 ; i < n ; i ++)
11     {
12         if(last != s[i])
13         {
14             sum ++;
15             last = s[i];
16         }
17     }
18     if(sum >= n - 1)
19         cout << n << endl;
20     else
21         cout << sum + 2 << endl;
22     return 0;
23 }

时间: 2024-10-26 22:48:45

Codeforces 603A Alternative Thinking的相关文章

Codeforces 604C Alternative Thinking

题目链接:http://codeforces.com/problemset/problem/604/C 题意:给一个01串,你可以指定起点,终点,翻转此区间一次.要求翻转后,0,1交替出现的子串长度最长,输出长度. 思路:头开始想想,感觉很麻烦,其实多写几个例子,想一想,就那么三种情况. 想要使翻转后,符合题意的子串长度增加.要么就是有连续相同的元素(11:增加1,111:增加2),要么就是可以交换的类型(0011:增加2). 连续的元素大于3也只能增加2. ×××要注意的一点是:1100001

Codeforces Round #296 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/527 A. Playing with Paper time limit per test:2 seconds memory limit per test:256 megabytes One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm ?×? b mm sheet

codeforces 的20道C题

A - Warrior and Archer CodeForces - 595C n  偶数  然后n个数字 A B 轮流取一个 A让差变小B让差变大 直到最后2 个   求的是最小剩下的差 最后剩下的 L R  相距 n/2    求一下最小的就行 #include <iostream> #include <cstdio> #include <cmath> #include <map> #include <algorithm> #include

Codeforces Round #346 (Div. 2) (659A,659B,659C,659D(几何叉乘),659E(并查集))

Round House 题目链接: http://codeforces.com/problemset/problem/659/A 解题思路: The answer for the problem is calculated with a formula ((a?-?1?+?b)  n + n)  n + 1. Such solution has complexity O(1). There is also a solution with iterations, modelling every o

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store