Codeforces 556A Case of the Zeros and Ones 消除01串

题意:给出一个只由0和1组成的串,每次操作可以选择两个相邻的位置且这两个位置一个为0,一个为1(顺序无所谓)并删去这两个位置得到一个新串,然后可以对新串继续操作。操作次数无限制,令原串的长度不断缩短直至不能再短。求这个最短的长度为多少。

虽然题目不难,不过还是证明一下自己思路的正确性。这里我主要是想说一下证明的思路。直观上看,只要发现两个相邻的是0和1就把它删去,不用考虑不同的删法会不会对后面造成影响。比如有如下串:.......101........ 删掉10和删掉01都无所谓。这里我们证明一个更为直接的结论:即无论怎么删,最后剩下的串的长度一定是固定的。

用反证法。假设存在两种不同的删法使得长度不一样,设第一种删法最后的长度为X,第二种删法最后的长度为Y。首先可以简单的想到,最后剩下来的串必定是只有一个数字组成,即要么全1要么全0(如果存在不同的一定还可以删)。我们假设原串长度为L。设第一种删法操作了a次,第二种删法操作了b次,因为每次操作减少2个长度,所以有

X + 2*a = L

Y + 2*b = L

而每次删的时候都是删去1个0和1,那么按照第一次删法,可求出原串中0和1的个数为X + a 、a。(不一定按顺序,下同),按照第二次删法,原串中0和1的个数为Y + b、b。显然对应的0和1的个数要相等。而a一定不等于b(因为X不等于Y)所以必有

a = Y + b

b = X + a

两式相加得到X + Y = 0,而X和Y都是非负整数,所以必有X = Y = 0,这显然与X不等于Y矛盾。所以最后剩下的串只有一种可能的长度。既然只有一种,那么无论怎么删都无所谓了。

我们用vector来实现。 遍历整个串,依次判断每个数,若容器空直接push,若容器尾部元素与当前数字不同,则pop,否则push。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int MAX = 200005;
int n;
char s[MAX];
vector <char> v;

void input()
{
    scanf("%s", s);
}

void solve()
{
    int lenth = strlen(s);
    v.clear();
    for(int i = 0; i < lenth; i++)
    {
        if(v.size() == 0)
            v.push_back(s[i]);
        else
        {
            if(v[v.size() - 1] != s[i])
                v.pop_back();
            else
                v.push_back(s[i]);
        }
    }
    printf("%d\n", v.size());
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        input();
        solve();
    }

    return 0;
}
时间: 2024-10-19 14:26:58

Codeforces 556A Case of the Zeros and Ones 消除01串的相关文章

CodeForces - 556A Case of the Zeros and Ones

Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any t

Codeforces 556A Case of the Zeros and Ones(消除01)

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

A. Case of the Zeros and Ones----解题报告

A. Case of the Zeros and Ones Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Conside

codeforces 555B Case of Fugitive

题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个桥,给出每个岛屿的位置和桥的长度,问是否可以把n个岛屿连起来? 解题思路: 排序+贪心,对于n个岛屿,相邻的两个之间起点和端点可以转化为n-1个连接桥的长度区间,把区间升序排列. 对于m个桥升序排列,对于每一个桥枚举每个可以合法覆盖的区间,选取最优的,选取的时候满足L<bridge_length<

Problem A CodeForces 556A

Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we

Case of the Zeros and Ones

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of

CodeForces 556A

Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we

CodeForces - 556C Case of Matryoshkas

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls