Problem
You are lucky enough to own N pet unicorns. Each of your unicorns has either one or two of the following kinds of hairs in its mane: red hairs, yellow hairs, and blue hairs. The color of a mane depends on exactly which sorts of colored hairs it contains:
- A mane with only one color of hair appears to be that color. For example, a mane with only blue hairs is blue.
- A mane with red and yellow hairs appears orange.
- A mane with yellow and blue hairs appears green.
- A mane with red and blue hairs appears violet.
You have R, O, Y, G, B, and V unicorns with red, orange, yellow, green, blue, and violet manes, respectively.
You have just built a circular stable with N stalls, arranged in a ring such that each stall borders two other stalls. You would like to put exactly one of your unicorns in each of these stalls. However, unicorns need to feel rare and special, so no unicorn can be next to another unicorn that shares at least one of the hair colors in its mane. For example, a unicorn with an orange mane cannot be next to a unicorn with a violet mane, since both of those manes have red hairs. Similarly, a unicorn with a green mane cannot be next to a unicorn with a yellow mane, since both of those have yellow hairs.
Is it possible to place all of your unicorns? If so, provide any one arrangement.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with seven integers: N, R, O, Y, G, B, and V.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is IMPOSSIBLE
if it is not possible to place all the unicorns, or a string ofN characters representing the placements of unicorns in stalls, starting at a point of your choice and reading clockwise around the circle. Use R
to represent each unicorn with a red mane, O
to represent each unicorn with an orange mane, and so on with Y
, G
, B
, and V
. This arrangement must obey the rules described in the statement above.
If multiple arrangements are possible, you may print any of them.
Limits
1 ≤ T ≤ 100.
3 ≤ N ≤ 1000.
R + O + Y + G + B + V = N.
0 ≤ Z for each Z in {R, O, Y, G, B, V}.
Small dataset
O = G = V = 0. (Each unicorn has only one hair color in its mane.)
Large dataset
No restrictions beyond the general limits. (Each unicorn may have either one or two hair colors in its mane.)
Sample
Note that the last two sample cases would not appear in the Small dataset.
For sample case #1, there are many possible answers; for example, another is BYBRYR
. Note that BYRYRB
would not be a valid answer; remember that the stalls form a ring, and the first touches the last!
In sample case #2, there are only three stalls, and each stall is a neighbor of the other two, so the two unicorns with yellow manes would have to be neighbors, which is not allowed.
For sample case #3, note that arranging the unicorns in the same color pattern as the Google logo (BRYBGR
) would not be valid, since a unicorn with a blue mane would be a neighbor of a unicorn with a green mane, and both of those manes share blue hairs.
In sample case #4, no two unicorns with yellow manes can be neighbors, and no two unicorns with violet manes can be neighbors.
自我感觉这种题其实不难只是在做的时候容易想复杂了 或者 根本没有正确的思路。。
由于题目的限制条件 G只能与R相连 O只能与B相连 V只能与Y相连
所以根据这个限制条件就可以进行构造 把G都放在R之间 O放在B之间 V放在Y之间
除此之外还有一定的数量限制条件
处理了GOV之后 就可以简单的处理RBY了 他们根据数量 交错分布就好了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("B-large-practice.in","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int n;
string col="RBYGOV";
string imp="IMPOSSIBLE";
int num[6];
int pn[3];
char ch[1010];
string fir()
{
int tot=0;
for(int i=0;i<3;i++)
{
tot+=pn[i];
}
for(int i=0;i<3;i++)
{
if(pn[i]*2>tot)//一个颜色的数量大于另外两个颜色数量的和
return imp;
}
pair<int,char> pa[3];
for(int i=0;i<3;i++)
{
pa[i].first=pn[i];
pa[i].second=col[i];
}
sort(pa,pa+3);
int index=0;
for(int i=0;i<tot;i+=2)
{
while(!pa[index].first)
index++;
ch[i]=pa[index].second;
pa[index].first--;
}
for(int i=1;i<tot;i+=2)
{
while(!pa[index].first)
index++;
ch[i]=pa[index].second;
pa[index].first--;
}
string res="";
for(int i=0;i<tot;i++)
res+=ch[i];
return res;
}
string solve()
{
for(int i=0;i<3;i++)
{
if(num[i]<num[i+3])
return imp;
if(num[i]>0&&num[i]==num[i+3])
{
if(num[i]+num[i+3]!=n)
return imp;
string res="";
for(int j=0;j<num[i];j++)
{
res+=col[i];
res+=col[i+3];
}
return res;
}
else
{
pn[i]=num[i]-num[i+3];
}
}
string str=fir();
if(str==imp)
return imp;
int flag[3];
for(int i=0;i<3;i++)
flag[i]=0;
int len=str.length();
for(int i=0;i<len;i++)
{
char c=str[i];
// cout<<str[i];
printf("%c",c);
for(int j=0;j<3;j++)
{
if(!flag[j]&&str[i]==col[j])
{
flag[j]=1;
for(int k=0;k<num[j+3];k++)
{
c=col[j+3];
printf("%c",c);
c=col[j];
printf("%c",c);
}
}
}
}
return "";
}
int main()
{
// fread;
// fwrite;
int tc;
scanf("%d",&tc);
int cs=1;
while(tc--)
{
scanf("%d",&n);
scanf("%d%d%d%d%d%d",&num[0],&num[4],&num[2],&num[3],&num[1],&num[5]);
printf("Case #%d: ",cs++);
string res=solve();
int len=res.length();
if(len>0)
{
for(int i=0;i<len;i++)
ch[i]=res[i];
ch[len]='\0';
printf("%s\n",ch);
}
else
puts("");
}
return 0;
}