首页 > 其他分享 >CodeForces - 626B Cards (全排列&模拟)

CodeForces - 626B Cards (全排列&模拟)

时间:2023-05-08 21:31:40浏览次数:74  
标签:exchange int CodeForces cards 626B Cards green 气球 card

Time Limit: 2000MS

 

Memory Limit: 262144KB

 

64bit IO Format: %I64d & %I64u

CodeForces - 626B


Cards



Submit Status




Description




Catherine has a deck of n

  • 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



2RB





Output



G





Input



3GRG





Output



BR





Input



5BBBBB





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.

//题意:

给你n个气球,这n个气球有三种颜色,蓝色(B),绿色(G),红色(R),现在玩一个游戏,每次从n个气球中拿出来两个,如果这两个气球颜色不同,那么这两个气球可以合并成一个第三种颜色的气球,如果这两个气球颜色相同,那么这两个气球可以合并成一个同种颜色的气球,问这样一直合并,直到最后一个。问最后能合并出哪几种颜色的气球。

//思路:

先将n个颜色不同的气球的颜色转换成数字,并将其存在一个数组a中,对a数组进行全排列,并对每一种情况进行合并,直到最后。(当合并出来的气球颜色已经等于3时,直接结束,节省时间,因为总共就3 种颜色)。

 

//HAIT:

首先得说一下输出格式,有的输入会对应多种输出,但要输出按字母升序排列(WA了3次)。。。

另外用我的思路写有一个bug,那就是当输入为(4     BGBG)时,我的输出是BG,但答案应该是BGR,在这块WA了8次。。。但加了一个特殊判断就过了^_^(在下面代码中会标注)。。。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
#define N 100010
#define M 1000000007
using namespace std;
char s[210];
int a[210];
int b[210];
int vis[5];
int c[5];
int d[5];
int chang(char c)
{
	if(c=='B') return 1;
	if(c=='G') return 2;
	if(c=='R') return 3;
}
void ch(int x)
{
	if(x==1) printf("B");
	else if(x==2) printf("G");
	else if(x==3) printf("R");
	
}
int main()
{
	int n,i,j,k;
	while(scanf("%d",&n)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		memset(d,0,sizeof(d));
		scanf("%s",s);
		int kk=0;
		for(i=0;i<n;i++)
		{
			a[i]=chang(s[i]);
			if(!vis[a[i]])
			{
				vis[a[i]]=1;
				kk++;
			}	
			d[a[i]]++;
		}
		if((kk==2)&&(d[1]==2&&d[2]==2)||(d[1]==2&&d[3]==2)||(d[2]==2&&d[3]==2))//加一个特殊判断 
		{
			printf("BGR\n");
			continue;
		}
		sort(a,a+n);
		int k=0;
		memset(vis,0,sizeof(vis));
		do
		{
			for(i=0;i<n;i++)
				b[i]=a[i];
//			for(i=0;i<n;i++)
//				printf("%d ",b[i]);
//			printf("\n");
			for(i=1;i<n;i++)
			{
				if(b[i]==b[i-1])
					b[i]=b[i-1];
				else
				{
					b[i]=(b[i]+b[i-1])%4;
					if(!b[i])
						b[i]=2;
				}
			}
//			printf("%d\n",b[n-1]);
			if(!vis[b[n-1]])
			{
				vis[b[n-1]]=1;
				c[k++]=b[n-1];
			}
			if(k==3)
				break;		
		}while(next_permutation(a,a+n));
		sort(c,c+k);
		for(i=0;i<k;i++)
			ch(c[i]);
		printf("\n");
	}
	return 0;
}

 




标签:exchange,int,CodeForces,cards,626B,Cards,green,气球,card
From: https://blog.51cto.com/u_16079508/6256255

相关文章

  • CodeForces - 597A Divisibility (模拟)
    TimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uCodeForces-597ADivisibilitySubmit StatusDescriptionFindthenumberof k-divisiblenumbersonthesegment [a, b].Inotherwordsyouneedtofindthenumberofsuchintegerv......
  • Codeforces Round 871 (Div. 4)
    A.LoveStory#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongintread(){intx=0,f=1,ch=getchar();while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();if......
  • Codeforces Round 871 (Div. 4)
    A.LoveStory题意:给定n个长度为10的字符串,问其与codeforces字符串的对应下标字母不同的个数。分析:对于每个字符串从前往后依次和“codeforces”对应字符比较然后统计不同字母数即可code:#include<bits/stdc++.h>usingnamespacestd;intmain(){ std::ios::sync_wit......
  • Codeforces Round 871 (Div. 4)
    CodeforcesRound871(Div.4)A-LoveStory#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=1e5+5,INF=0x3f3f3f3f,Mod=1e6;constdoubleeps=1e-6;typedeflonglongll;i......
  • Codeforces 871 div4(重拳出击)
    Codeforces871div4ABC简单题D题意每次操作可以将当前的数分成两份,一份是\(\frac{1}{3}\),一份是\(\frac{2}{3}\),问当前数n可否进行若干次操作,最终出现一份大小为m的片。递归一下就好了,数据最大才\(10^7\)代码voiddfs(intx){ if(x==m){flag=1;return;} if(flag)re......
  • Codeforces Round 870 (Div. 2)
    CodeforcesRound870(Div.2)A-TrustNobody思路:枚举每一种说谎人数x,若a[i]大于x则说谎人数加一,判断最后说谎总人数是否为x,若是则输出x,结束枚举;若没有满足的x则-1#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;......
  • Codeforces 1817F - Entangled Substrings(SA)
    为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?一种SA做法,本质上和SAM做法等价,但是说来也丢人,一般要用到SAM的题我都是拿SA过的/wul考虑将\(ac\)看作一个整体。记\(\text{occ}(S)\)为\(S\)出现位置的集......
  • Codeforces Round 848 (Div. 2)C
    B.TheForbiddenPermutation一定要注意题目中说的是对于alli满足才算不好的,我们做的时候只要破坏一个i这个a就不算好的了,被这一点坑了,没注意到all。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=2e5+10;intp[N],a[N];vo......
  • Codeforces Round 856 (Div. 2)C
    C.ScoringSubsequences思路:我们想要找到满足的最大值的长度最长的的区间,因为单调不减,所以更大的数一定在最大值的里面包含,所以我们用两个指针维护这样一个满足当前i的最大值区间,当新来一个数,这时我们答案里面一定要包含这个数,我们看能否保持这个长度,能不能保持需要看j指针所指......
  • Codeforces——870
    A.TrustNobody题目大意给你一个长度为\(n\)的数组\(a\),\(a\)中每个元素\(a_i\)表示当前人认为\(n\)个人中至少有\(a_i\)个人说谎,让你找出说谎的人的个数。思路:枚举说谎人数\(x\),遍历\(a\)数组,对于当前\(a_i\),如果有\(a_i\geqx\),那么显然第\(i\)个人在说谎,记录人数看是否等......