BOJ 396

原创 2012年03月24日 19:39:34

Problem K. Alice's Piano
Description
Alice has a piano which can plays nice music, but it's different from other
pianos. It has two rows of keys, and the method of playing it is also quite
special. When Alice plays piano, she has to put her two hands on the leftside
of the two rows of keys respectively (without touching the leftmost keys of
the two rows). And then her hands move on the keys from left to right one by
one. Each time she can move one of her hands by one key, either on the
above row or the below row. She has to keep the difference between the
number of black keys and white keys she has already touched no more than
K to make sure the music is beautiful. When the music is end, her two hands
should be both on the rightmost of the piano keyboard.
Now Alice wants to know whether she can play nice music, given the
description of the piano.
Suppose the number of keys of the two rows are both N. And two strings are
given to describe the keyboard. “1” stands for black keys while “0” for white
ones.
Input
There are multiple cases, end by EOF.
For each case, the first line contains two integers N(3 <= N <= 1000) and K
(0 <= K <= 1000), with two 0-1 strings which are described above.
Output
If she can play the music, please output an answer string of length 2N which
has the minimum lexicographic order. In the answer string, “1” represents
move on the above row while “2” represents the below row. If she cannot,
just output "Poor Alice".
Sample Input
4 1
0011
0110
4 1
1100
1100
Sample Output
22121112
Poor Alice
Hint
Take the first sample for explaining:
Suppose Alice puts her left hand on the above row of keys, and right on the
below one, the answer string stands for the following process:
1. move right hand to the 1st key of the below row of keys.
2. move right hand to the 2nd key of the below row of keys.
3. move left hand to the 1st key of the above row of keys.
4. move right hand to the 3rd key of the below row of keys.
5. move left hand to the 2nd key of the above row of keys.
6. move left hand to the 3rd key of the above row of keys.
7. move left hand to the 4th key of the above row of keys.
8. move right hand to the 4th key of the below row of keys.

这个题是动态规划,代码很容易看懂

#include <stdio.h>
#include <string.h>
#define inf -10000000
char up[1010],dw[1010];
int dp[1010][1010][2];
int n,k;
int min(int a,int b){
	return a>b?b:a;
}
int main()
{
	int i,j,path[2010],x,y,dir;
	while(scanf("%d %d",&n,&k)!=EOF){
		scanf("%s%s",&up[1],&dw[1]);
		memset(dp,0,sizeof(dp));
		dp[0][0][0]=dp[0][0][1]=0;
		for(i=0;i<=n;i++)
			for(j=0;j<=n;j++){
				if(i==0 && j==0)continue;
				if(i!=0){
					if(dp[i-1][j][0]!=inf)
						dp[i][j][0]=dp[i-1][j][0];
					else
						dp[i][j][0]=dp[i-1][j][1];
					if(dp[i][j][0]!=inf){
						if(up[i]=='0')
							dp[i][j][0]--;
						else if(up[i]=='1')
							dp[i][j][0]++;
						if(dp[i][j][0]>k || dp[i][j][0]<-k)
							dp[i][j][0]=inf;
					}
					if(j==0)
						dp[i][0][1]=inf;
				}
				if(j!=0){
					if(dp[i][j-1][1]!=inf)
						dp[i][j][1]=dp[i][j-1][1];
					else
						dp[i][j][1]=dp[i][j-1][0];
					if(dp[i][j][1]!=inf){
						if(dw[j]=='0')
							dp[i][j][1]--;
						else if(dw[j]=='1')
							dp[i][j][1]++;
						if(dp[i][j][1]>k || dp[i][j][1]<-k)
							dp[i][j][1]=inf;
					}
					if(i==0)
						dp[0][j][0]=inf;
				}
		}
		memset(path,0,sizeof(path));
		if(dp[n][n][1]!=inf || dp[n][n][0]!=inf){
			x=n,y=n;
			if(dp[n][n][1]!=inf)
				dir=1;
			else
				dir=0;
			for(i=1;i<=n*2;i++){
				if(dir==0){
					x--;
					path[i]=1;
					if(dp[x][y][1]!=inf)
						dir=1;
					else
						dir=0;
				}
				else if(dir==1){
					y--;
					path[i]=2;
					if(dp[x][y][1]!=inf)
						dir=1;
					else
						dir=0;
				}
			}
			for(i=2*n;i>0;i--)
				printf("%d",path[i]);
			printf("\n");
		}
		else
			printf("Poor Alice\n");
	}
	
	return 0;
}


 

 

BOJ 396~400. 整合

0396.
  • samuelwyf
  • samuelwyf
  • 2014年07月16日 11:46
  • 470

<leetcode>396 Rotate Function

396 Rotate Function Given an array of integers A and let n to be its length. Assume Bk to be an arr...
  • Vipin_Pei
  • Vipin_Pei
  • 2016年09月14日 16:57
  • 612

396经济类联考数学真题分析

微积分   1 函数极限连续 求函数定义域221   1.1   求极限(等价无穷小、洛必达法则)331,222,231     2 导数   2.1导数定义 321,421,436(可导必连续...
  • whjwindow
  • whjwindow
  • 2015年05月23日 19:28
  • 477

我的考研总结(总分396,工科)

昨天复试结果出来了,是公费(浙大计算机)。这样我的考研生活算是真正结束了。今天把我的考研经历贴出来,算是做一个总结,也希望能对大家有所帮助。先说一下,我是外校考浙大计算机系,本科在一所二流学校,学校排...
  • fishstudio
  • fishstudio
  • 2004年08月24日 07:45
  • 3316

【Leetcode】396. Rotate Function

题目链接: 题目: Given an array of integers A and let n to be its length. Assume Bk to be an array o...
  • yeqiuzs
  • yeqiuzs
  • 2016年09月11日 21:15
  • 2655

Codeforces Round #396 (Div. 2)

A. Mahmoud and Longest Uncommon Subsequence传送门: http://codeforces.com/contest/766/problem/A题目大意:给出两...
  • infinity_edge
  • infinity_edge
  • 2017年02月08日 12:43
  • 217

如何计算我的“五险一金”

  工资是绝大多数人的收入来源,而工资带来的社保缴纳,个人所得税征收又和每个人息息相关。人们对于每月发到手里的钱比较清楚,但税收和社保似乎模糊了很多,理财从工资开始,为大家介绍我国现阶段的一些社保和税...
  • renlang_7
  • renlang_7
  • 2008年10月13日 20:01
  • 2890

codeforces #396

A:题意:两个字符串,找一个字符串是其中一个字串不是另一个,最长多长tip:除非一模一样的两个串,不然长的那个就是#include #include #include const int max...
  • zjy2015302395
  • zjy2015302395
  • 2017年02月15日 07:55
  • 129

BOJ 387

直接模拟即可,最近码力太渣多敲敲 #include #include #include #include using namespace std; char map[10][10]; int...
  • waitfor_
  • waitfor_
  • 2013年03月17日 23:14
  • 1088

Leetcode 396(Java)

Leetcode 396(Java)
  • catttbao
  • catttbao
  • 2017年04月06日 20:56
  • 97
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:BOJ 396
举报原因:
原因补充:

(最多只允许输入30个字)