跳过正文
  1. Posts/

codeforces #334 div 2 C. Alternative Thinking

·517 字·2 分钟

题意:给定一个01串。要进行一次变换:选一段连续的非空的字串,将这段串的0和1反转(0变成1,1变成0) 然后问能得到的最长的0,1交替的序列的长度是多少(不一定连续)

比赛的时候想出来两种会将答案增加的可能情况。一种是10000001 中间有大于等于3个的连续字符,这样可以把中间反转一下,答案会+2 另外一种是 1001001 这样。。有至少两段的连续两个以上的相同字符被另一个字符隔开的情况。只要将1001001变成1010101。答案还是会+2。。。然后发现这两种情况实际上可以统一起来。即:有至少两段的连续相同字符。 注意000 也算有两段。 如果有两段或者以上,那么答案+2.

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月02日 星期三 00时36分47秒
 4File Name :code/cf/#334/C.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E5+7;
34char a[N];
35int n;
36
37char tow(char ch)
38{
39    if (ch=='0') return '1';
40    if (ch=='1') return '0';
41}
42int main()
43{
44	#ifndef  ONLINE_JUDGE
45	freopen("code/in.txt","r",stdin);
46  #endif
47
48	scanf("%d",&n);
49	scanf("%s",a);
50	int cnt =  0;
51	for ( int i = 0  ; i < n-1 ; i++)
52	    if (a[i]==a[i+1])
53		cnt++;
54
55    if (cnt>=2) cnt = 2;
56
57    char tar = a[0];
58	tar = tow(tar);
59	int ans = 1;
60	for ( int i = 1 ; i < n ; i++ )
61	{
62//	    printf("%d %c\n",i,tar);
63	    if (a[i]==tar)
64	    {
65		ans++;
66		tar = tow(tar);
67	    }
68	}
69
70	printf("%d\n",ans+cnt);
71
72
73  #ifndef ONLINE_JUDGE
74  fclose(stdin);
75  #endif
76    return 0;
77}

相关文章

codeforces #332 div 2 C. Day at the Beach

·561 字·2 分钟
C. Day at the Beach time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.

codeforces #332 div 2 B. Spongebob and Joke

·651 字·2 分钟
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick’s personal stuff and found a sequence _a_1, _a_2, …, a__m of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence _f_1, _f_2, …, f__n of length n and for each number a__i got number b__i = f__a__i. To finish the prank he erased the initial sequence a__i.

codeforces #332 div 2 A. Patrick and Shopping

·646 字·2 分钟
1#include <cstdio> 2#include <iostream> 3#include <cmath> 4using namespace std; 5long long d1,d2,d3; 6int main() 7{ 8cin>>d1>>d2>>d3; 9long long ans = 999999999999; 10ans = min(ans,d1+d2+d3); 11ans = min (ans,d1*2+d2*2); 12ans = min (ans,d1*2+2*d3); 13ans = min(ans,d2*2+2*d3); 14cout<<ans<<endl; 15return 0; 16} time limit per test 1 second

codeforces 589 B - Layer Cake

·698 字·2 分钟
B - Layer Cake **Time Limit:**6000MS **Memory Limit:**524288KB 64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 589B Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n rectangular cake layers. The length and the width of the i-th cake layer were a__i and b__i respectively, while the height of each cake layer was equal to one.