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&&R0&&G0)
其他颜色同理,我们把三种颜色都判一判就好了。
但是我们在一开始牌很多的时候,我们可以兑换出我们一开始没有的颜色,所以这时候,我们瞎搞一下,然后再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;
}