CodeForces 626B CodeForces 626B【暴力】
2016-03-07 19:01
309 查看
B. Cards
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
626B
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
Input
Output
Input
Output
Input
Output
Hint
In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card.
In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and
a red card for a blue card, then exchange the blue card and remaining green card for a red card.
In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards.
题目:
给出若干个三种颜色的卡片,有两种操作
1,两张相同颜色的能合并成一张
2,两张不同颜色的能合并成另外一种颜色
经过多次操作使得只剩下一种颜色,按字典序输出所有可能出现的结果
找规律的题目,确实伤不起....
规律如下:
1,如果只有一种颜色,不用说了,就是这一种
2,如果有一种颜色有0个,一种颜色有1个,分情况考虑:
如果第三种颜色只有1 个,最终结果只可能是颜色为0的那种
如果第三种颜色有多个,最终结果可以是前两种的任意一个(可以自己动手试试)
3,否则的话,三种颜色都可以组成
规律是推导出来了,只是代码写起来实在麻烦,想了好久,才想出来优化的办法......貌似是简化了一点...
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char table[]={"BGR"};
void slove(char s[],int n)
{
sort(s,s+n);
int x[3]={0},vis[3]={1,1,1};// 1 为输出,0,为不输出
for(int i=0;i<3;++i)
{
x[i]=upper_bound(s,s+n,table[i])-lower_bound(s,s+n,table[i]);
}
for(int i=0;i<3;++i)//双循环枚举所有情况
{
for(int j=0;j<3;++j)
{
if(i==j)
{
continue;
}
if(x[i]==0&&x[j]==0)//其中任意两个为0 的只输出第三个
{
vis[i]=vis[j]=0;
break;
}
else if(x[i]==0&&x[j]==1)//一个0,一个1的
{
if(x[3-i-j]==1)//另外一个只有1个,那么输出为零的那种
{
vis[j]=vis[3-i-j]=0;
}
else if(x[3-i-j]>1)//有多个
{
vis[3-i-j]=0;
}
}
}
}
for(int i=0;i<3;++i)
{
if(vis[i])
{
printf("%c",table[i]);
}
}
printf("\n");
}
int main()
{
//freopen("shuju.txt","r",stdin);
int n;char s[205];
while(~scanf("%d",&n))
{
scanf("%s",s);
slove(s,n);
}
return 0;
}
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
626B
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
Input
2 RB
Output
G
Input
3
GRG
Output
BR
Input
5 BBBBB
Output
B
Hint
In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card.
In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and
a red card for a blue card, then exchange the blue card and remaining green card for a red card.
In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards.
题目:
给出若干个三种颜色的卡片,有两种操作
1,两张相同颜色的能合并成一张
2,两张不同颜色的能合并成另外一种颜色
经过多次操作使得只剩下一种颜色,按字典序输出所有可能出现的结果
找规律的题目,确实伤不起....
规律如下:
1,如果只有一种颜色,不用说了,就是这一种
2,如果有一种颜色有0个,一种颜色有1个,分情况考虑:
如果第三种颜色只有1 个,最终结果只可能是颜色为0的那种
如果第三种颜色有多个,最终结果可以是前两种的任意一个(可以自己动手试试)
3,否则的话,三种颜色都可以组成
规律是推导出来了,只是代码写起来实在麻烦,想了好久,才想出来优化的办法......貌似是简化了一点...
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char table[]={"BGR"};
void slove(char s[],int n)
{
sort(s,s+n);
int x[3]={0},vis[3]={1,1,1};// 1 为输出,0,为不输出
for(int i=0;i<3;++i)
{
x[i]=upper_bound(s,s+n,table[i])-lower_bound(s,s+n,table[i]);
}
for(int i=0;i<3;++i)//双循环枚举所有情况
{
for(int j=0;j<3;++j)
{
if(i==j)
{
continue;
}
if(x[i]==0&&x[j]==0)//其中任意两个为0 的只输出第三个
{
vis[i]=vis[j]=0;
break;
}
else if(x[i]==0&&x[j]==1)//一个0,一个1的
{
if(x[3-i-j]==1)//另外一个只有1个,那么输出为零的那种
{
vis[j]=vis[3-i-j]=0;
}
else if(x[3-i-j]>1)//有多个
{
vis[3-i-j]=0;
}
}
}
}
for(int i=0;i<3;++i)
{
if(vis[i])
{
printf("%c",table[i]);
}
}
printf("\n");
}
int main()
{
//freopen("shuju.txt","r",stdin);
int n;char s[205];
while(~scanf("%d",&n))
{
scanf("%s",s);
slove(s,n);
}
return 0;
}
相关文章推荐
- calico学习(1)
- Android开发小知识-XML文件解析之SAX(Simple API for XML)
- Linux 内核的文件 Cache 管理机制介绍
- 专访邓凡平:Android开发路上的快速学习之道
- 51nod1305 Pairwise Sum and Divide
- 为啥requests和urllib,urllib2,urllib3的URL编码行为存在这种差异?
- Grunt
- python 登录并爬取淘宝信息
- 软件工程课随笔1-认识软件工程
- 二维数组的传参问题!!!
- plat_led应用程序
- 剑指offer-二叉树镜像
- R学习笔记-3 用plyr包实现分组求平均
- 三层学习------实践篇
- STM32F051 IAP源代码分享
- 禁止屏幕随手机旋转变化
- dp学习——最大字段和
- php魔术方法——属性重载方法
- 出差(十三)
- CODE[VS] 1068 乌龟棋