URAL 1501 Sense of Beauty

1501. Sense of Beauty

Time limit: 0.5 second

Memory limit: 64 MB

The owner of a casino for New Russians has a very refined sense of beauty. For example, after a game there remain two piles with the same number of cards on the table, and the owner likes the cards
to be arranged into two piles according to the color: one pile with red cards and the other with black cards. Of course, this is done not by the owner himself, but by a croupier. The owner just likes to watch the process. The croupier takes a card from the
top of one of the initial piles and puts it into one of the new piles; this is repeated until all the cards from the initial piles are transferred. The owner doesn‘t like it if one of the resulting piles grows faster than the other. At each moment the resulting
piles must not differ in size by more than one card; a bigger difference would contradict the owner‘s sense of beauty. Help the croupier to arrange the cards according to the tastes of his owner.

Input

The first line of the input contains the number N of cards in each of the piles (4 ≤ N ≤ 1000). Each of the next two lines contains N digits 0 or 1 describing the piles: 1
denotes a red-suit card and 0 denotes a black-suit card. The cards in a pile are described from the top to the bottom. There are in total N red and N black cards in the two piles.

Output

Output a line containing 2N digits 1 or 2, which describes the process of transferring the cards. Each number shows the number of the pile from which a card is taken. If it is impossible to
perform this task according to the given rules, output "Impossible".

Samples

input output
4
0011
0110
22121112
4
1100
1100
Impossible

题意:

给你两堆牌,牌的颜色只有红色或者黑色。 然后从两堆牌的牌顶来抽牌,每次抽可以选择两堆中的一堆。每次抽完,所得到的牌,红牌和黑牌数量相差必须不超过1。

做法:

因为一共各1000张牌,所以可以dp记忆化搜索。dp[i][j]代表  在第一堆牌抽了i张,第二堆牌抽了j张的情况下, 有没有不违反规则 达到这状态的方法。如果有 dp[i][j]会等于0,1,2,0表示当前多了一个黑牌,1表示当前红黑牌一样多,2表示当前红牌多一张。-2表示没达到这种状态的方法。

然后就是几种转移的方法,都要在dfs(i-1,j)或者 dfs(j,i-1)  可以达到的情况下 才能转移。

int dp[1100][1100];//
int n;

int num1[1100];
int num2[1100];

int bu[1100];
int dfs(int i,int j)
{
	if(~dp[i][j])//不是-1,就返回已经有的值, 记忆化搜索,不然会超时
		return dp[i][j];
	if(i==0&&j==0)
		return dp[i][j]=1;//0 0多 1平  2 1多

	if(i!=0)
	{
		if(dfs(i-1,j)==0&&num1[i]==1)//这里表示,上一个状态 红黑牌中 红牌多,所以这次必须抽黑牌才能转移
		{
			bu[i+j]=1;
			return dp[i][j]=1;
		}
		if(dfs(i-1,j)==1)
		{
			if(num1[i]==1)
			{
				bu[i+j]=1;
				return dp[i][j]=2;
			}
			if(num1[i]==0)
			{
				bu[i+j]=1;
				return dp[i][j]=0;
			}
		}

		if(dfs(i-1,j)==2&&num1[i]==0)
		{
			bu[i+j]=1;
			return dp[i][j]=1;
		}
	}
	if(j!=0)
	{
		if(dfs(i,j-1)==0&&num2[j]==1)
		{
			bu[i+j]=2;
			return dp[i][j]=1;
		}

		if(dfs(i,j-1)==1)
		{
			if(num2[j]==1)
			{
				bu[i+j]=2;
				return dp[i][j]=2;
			}
			if(num2[j]==0)
			{
				bu[i+j]=2;
				return dp[i][j]=0;
			}
		}

		if(dfs(i,j-1)==2&&num2[j]==0)
		{
			bu[i+j]=2;
			return dp[i][j]=1;
		}
	}
	return dp[i][j]=-2;
}
char n1[1100];
char n2[1100];
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%s%s",n1,n2);
		memset(dp,-1,sizeof dp);
		for(int i=1;i<=n;i++)
		{
			num1[i]=n1[i-1]-'0';
			num2[i]=n2[i-1]-'0';
		}

		if(dfs(n,n)!=-2)
		{
			for(int i=1;i<=2*n;i++)
				printf("%d",bu[i]);

			puts("");
		}
		else
			puts("Impossible");

	}
	return 0;
}
时间: 2024-11-06 22:38:00

URAL 1501 Sense of Beauty的相关文章

记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

题目传送门 1 /* 2 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 3 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操作必然是1红1黑 4 考虑三种情况:a[]连续两个颜色相同,输出11:b[]连续两个相同,输出22: 5 a[x] != b[y], 输出12:否则Impossible 6 详细解释:http://blog.csdn.net/jsun_moon/article/details/10254

URAL 1501. Sense of Beauty(记忆化搜索 dfs)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1501 The owner of a casino for New Russians has a very refined sense of beauty. For example, after a game there remain two piles with the same number of cards on the table, and the owner likes the car

URAL1501——DFS——Sense of Beauty

Description The owner of a casino for New Russians has a very refined sense of beauty. For example, after a game there remain two piles with the same number of cards on the table, and the owner likes the cards to be arranged into two piles according

THE SENSE OF BEAUTY

#include<stdio.h> int main() { char my_picture[100][120] = { " ,.:;j", " ,: i. .,:;ff", " : . .; i .,:itj", " :i ii :...ii,,;tt", " i : j . i:. ; t ...,;.:;t;", " ; . . ;i t :..:.;: ", &quo

Mini Stirling engine

So I spent 5 or 6 hours last night trying to hook up a mini Stirling engine with the gearbox of a Tamiya track module. Haven't been staying up late for a while. I wrapped up around 4 am. Well, guess i under estimated the 'engineering' difficulty. -Pr

Vocabulary_03

abandon vt.遗弃,放弃,抛弃  n.放纵:沉溺 abandon doing sth. 放弃做某事 abandon one's home 离弃家园 abandon oneself to 放纵自己,使自己沉溺 with abandon 放任,无拘无束 dance with wild/gay abandon 狂放地跳舞 abandon 指遗弃以前感兴趣或者负有责任的人或物,强调永远和完全的放弃 desert 强调“违背誓言.命令.责任.义务”等,多用于贬义,强调玩忽职守,见死不救,临阵脱逃或

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀