POJ 1609 简单dp

Tiling Up Blocks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5929 Accepted: 2316

Description

Michael The Kid receives an interesting game set from his grandparent as his birthday gift. Inside the game set box, there are n tiling blocks and each block has a form as follows: 

Each tiling block is associated with two parameters (l,m), meaning that the upper face of the block is packed with l protruding knobs on the left and m protruding knobs on the middle. Correspondingly, the bottom face of an (l,m)-block is carved with l caving dens on the left and m dens on the middle. 
It is easily seen that an (l,m)-block can be tiled upon another (l,m)-block. However,this is not the only way for us to tile up the blocks. Actually, an (l,m)-block can be tiled upon another (l',m')-block if and only if l >= l' and m >= m'. 
Now the puzzle that Michael wants to solve is to decide what is the tallest tiling blocks he can make out of the given n blocks within his game box. In other words, you are given a collection of n blocks B = {b1, b2, . . . , bn} and each block bi is associated with two parameters (li,mi). The objective of the problem is to decide the number of tallest tiling blocks made from B. 

Input

Several sets of tiling blocks. The inputs are just a list of integers.For each set of tiling blocks, the first integer n represents the number of blocks within the game box. Following n, there will be n lines specifying parameters of blocks in B; each line contains exactly two integers, representing left and middle parameters of the i-th block, namely, li and mi. In other words, a game box is just a collection of n blocks B = {b1, b2, . . . , bn} and each block bi is associated with two parameters (li,mi). 
Note that n can be as large as 10000 and li and mi are in the range from 1 to 100. 
An integer n = 0 (zero) signifies the end of input.

Output

For each set of tiling blocks B, output the number of the tallest tiling blocks can be made out of B. Output a single star '*' to signify the end of 
outputs.

Sample Input

3
3 2
1 1
2 3
5
4 2
2 4
3 3
1 1
5 5
0

Sample Output

2
3
*
题意: 给你一些积木吧,告诉你它左部分有x个凸点,中间有y个凸点,每层积木高为1,求垒起来最高为多少,
注意垒起来的前提是上边的x>=下边的x 并且上边的y>=下边的y。
 
思路:看懂垒起来的前提就很容易了,排序,然后dp就可以了,很简单很简单
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 10005

using namespace std;

struct node
{
	int left,mid;
}a[N];

int dp[N];
int n;

bool cmp(node a,node b)
{
	if(a.left==b.left) return a.mid>b.mid;
	else return a.left>b.left;
}

int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		if(n==0)
		{
			printf("*\n");
			break;
		}
		memset(dp,0,sizeof(dp));
	for(i=1;i<=n;i++)
	{
		scanf("%d %d",&a[i].left,&a[i].mid);
	}
	sort(a+1,a+n+1,cmp);
	for(i=1;i<=n;i++)
	{
		int ans=0;
		for(j=1;j<i;j++)
		{
			if(a[j].left>=a[i].left&&a[j].mid>=a[i].mid)
			{
				ans=max(ans,dp[j]);
			}
		}
		dp[i]+=max(dp[i],ans+1);
	}
	int maxx=-1;
	for(i=1;i<=n;i++) if(dp[i]>maxx) maxx=dp[i];
	printf("%d\n",maxx);
	}
	
	
	return 0;
} 
当然还有一种dp写法更快,更好,直接上代码吧。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 105

using namespace std;

int a[N][N],dp[N][N];
int n;

int main()
{
	int i,j,x,y;
	while(~scanf("%d",&n))
	{
		if(n==0)
		{
			printf("*\n");
			break;
		}
		memset(a,0,sizeof(a));
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++)
		{
			scanf("%d %d",&x,&y);
			a[x][y]++;
		}
		for(i=1;i<=100;i++)
		{
			for(j=1;j<=100;j++)
			{
				dp[i][j]=max(dp[i-1][j],dp[i][j-1])+a[i][j];
			}
		}
		printf("%d\n",dp[100][100]);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值