Codeforces 135C Zero-One(博弈)

33 篇文章 1 订阅

 

C. Zero-One

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.

Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.

The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.

An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.

Input

The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.

Output

Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).

Examples

input

Copy

????

output

Copy

00
01
10
11

input

Copy

1010

output

Copy

10

input

Copy

1?1

output

Copy

01
11

Note

In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.

In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.

 

 

 

【题目链接】点击有惊喜

【题意】有一个长度为n的字符串,字符串由0,1,?组成,?即可能是0也可能是1,有A,B两个选手一次取字符串,每次只能够取一个(如果取完后字符串分成两部分则再拼接起来),A的目的是使的01字符串对应的十进制最小,B的目的是使的01字符串对应的十进制最大,最后剩01串长度为2时截止。

【思路】设one为字符串中1的个数,zero为字符串中0的个数。通过一个例子可以发现以下规律,当字符串中不存在?时,zero>one 最后输出的为"00",当one>zero+1时,输出"11,当zero==one || one == zero+1 的时候,如果字符串的最后一个字符为0,则输出 "10"。

对于当字符串中存在?时,思考的方式也一样。

【代码如下】

 

#include <bits/stdc++.h>
using namespace std;

int main(){
    string str;
    cin >> str;
    int n = str.length();
    int zero=0,one=0,other=0;
    for(int i = 0; i < n; i ++) if(str[i]=='0') zero++; else if(str[i]=='1') one++; else other++;
    int p = n/2-1, q = (n+1)/2-1;
    if(one <= q) puts("00");
    if(str[n-1] != '0' && zero-1 <= p && one+(str[n-1]=='?')-1 <= q) puts("01");
    if(str[n-1] != '1' && zero+(str[n-1]=='?')-1<=p && one-1<=q) puts("10");
    if(zero <= p) puts("11");
    return 0;
}

 

 

 

 

 

这是一道 CodeForces 上的题目,题目编号为 749C,题目名称为 Voting。 题目描述: 有 $n$ 个人参加选举,选出一位领导人。每个人都会投票,你知道了每个人选择谁,并且可以知道选票中作废票和弃权票的数量。如果有一个人获得了半数以上的有效选票(即除去作废票和弃权票的票数),那么他将成为领导人。如果没有任何一个人获得半数以上的有效选票,则选举无效。 现在你可以修改任意数量的作废票或弃权票,使选举有效,并使你支持的候选人成为领导人。你需要最少的修改次数。 输入格式: 第一行包含三个整数 $n, a, b$,分别表示选民的数量,作废票的数量和弃权票的数量。 接下来 $n$ 行,每行包含一个字符串,表示每个人的投票情况。如果该字符串为 “YES” 或 “NO”,表示该选民对应的是弃权票;如果该字符串为 “POLL”, 表示该选民对应的是作废票;如果该字符串为其他字符串,则表示该选民对应的是有效选票,该字符串为该选民的投票对象。 输出格式: 如果无法通过修改使选举有效,则输出 -1;否则输出最少的修改次数,使得选举有效,并且你支持的候选人成为领导人。 数据范围: $1 \leq n \leq 2000, 0 \leq a, b \leq n$ 输入样例 #1: ``` 7 2 1 YES NO POLL YES YES YES NO ``` 输出样例 #1: ``` 1 ``` 样例 #1 解释: 总共有7个选民,其中弃权票有3个,作废票有2个,有效选票有2个。由于选民投票意见分散,无法确定一位领导人,因此选举无效。 我们可以将2张作废票修改为支持你所支持的候选人,这样你所支持的候选人将获得3张有效选票,超过半数,成为领导人。因此修改次数为1。 输入样例 #2: ``` 3 1 0 YES NO YES ``` 输出样例 #2: ``` 0 ``` 样例 #2 解释: 总共有3个选民,其中弃权票有1个,作废票有0个,有效选票有2个。你所支持的候选人获得了2张有效选票,超过半数,成为领导人。由于选民投票意见不分散,选举有效,无需修改任何票。因此修改次数为0。 算法1: (模拟) $O(n)$ 首先计算出除作废票和弃权票之外的票数,如果有一人的得票率超过50%,则选举有效,直接输出0。 否则,需要计算出至少需要修改多少张作废票或弃权票,才能使选举有效,并且支持你所支持的候选人成为领导人。 具体来说,我们可以考虑枚举需要修改的作废票和弃权票的数量,假设需要修改 $i$ 张作废票和 $j$ 张弃权票,使得选举有效。那么,你所支持的候选人需要得到至少 $\lceil \frac{n}{2} \rceil$ 张有效选票。我们可以通过统计当前你所支持的候选人得到的有效选票数 $cnt$,以及当前已经修改的作废票和弃权票的数量 $a'$ 和 $b'$,来判断是否存在一组解 $(i, j)$,使得选举有效。 具体来说,如果当前得票数 $cnt + i \ge \lceil \frac{n}{2} \rceil$,那么选举有效,输出 $i+j$ 即可。如果 $(\lceil \frac{n}{2} \rceil - cnt) \le a' + i \le n-b'-j$,那么也存在一组解 $(i,j)$,使得选举有效,输出 $i+j$ 即可。 如果枚举完所有情况,都无法使选举有效,那么输出 -1。 时间复杂度:$O(n^2)$ C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值