8VC Venture Cup 2016 - Elimination Round B. Cards 瞎搞

本文介绍了解决Codeforces竞赛中B.Cards问题的方法。通过分析游戏规则,提出了一种策略来确定最终卡片可能的颜色,并提供了实现代码。

B. Cards

题目连接:

http://www.codeforces.com/contest/626/problem/B

Description

Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions:

take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color;
take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color.
She repeats this process until there is only one card left. What are the possible colors for the final card?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards.

The next line contains a string s of length n — the colors of the cards. s contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.

Output

Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order.

Sample Input

2
RB

Sample Output

G

Hint

题意

有三种颜色的牌,现在有两种决策

1.拿出两张不同颜色的牌,可以换取另外一种颜色的牌
2.拿出两张相同颜色的牌,可以得到另外一种颜色的牌

问你最后剩下的牌的颜色的可能性

题解:

我们显然可以一直使用操作2,使得每种颜色的牌至多一张,这时候我们开始判断就好了。

我们最后剩下B的条件是(R>0&&G>0)||(B>0&&R==0&&G==0)

其他颜色同理,我们把三种颜色都判一判就好了。

但是我们在一开始牌很多的时候,我们可以兑换出我们一开始没有的颜色,所以这时候,我们瞎搞一下,然后再check。

大概就是这样了。

代码

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

int x[4];
string s;
string ans;
int flag[4];
void check()
{
    if(x[1]>0&&x[2]>0)flag[3]++;
    if(x[2]>0&&x[3]>0)flag[1]++;
    if(x[1]>0&&x[3]>0)flag[2]++;
}
int main()
{
    srand(time(NULL));
    int n;scanf("%d",&n);
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='R')x[1]++;
        else if(s[i]=='G')x[2]++;
        else x[3]++;
    }
    if(x[1]==0&&x[2]==0)return puts("B");
    if(x[2]==0&&x[3]==0)return puts("R");
    if(x[1]==0&&x[3]==0)return puts("G");
    check();
    for(int i=0;i<10000;i++)
    {
        int p = rand()%3+1,q = rand()%3+1;
        if(p==q)continue;
        if(p>q)swap(p,q);
        if(x[p]>0&&x[q]>0)
        {
            if(p==1&&q==2)
            {
                x[p]--,x[q]--;
                x[3]++;
                check();
                x[p]++,x[q]--;
                x[3]--;
            }
            if(p==1&&q==3)
            {
                x[p]--,x[q]--;
                x[2]++;
                check();
                x[p]++,x[q]--;
                x[2]--;
            }
            if(p==2&&q==3)
            {
                x[p]--,x[q]--;
                x[1]++;
                check();
                x[p]++,x[q]--;
                x[1]--;
            }
        }
    }
    if(flag[3])cout<<"B";
    if(flag[2])cout<<"G";
    if(flag[1])cout<<"R";
    cout<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值