您的位置:首页 > 其它

pku1609 Tiling Up Blocks (DP题)

2009-07-02 19:02 330 查看
Tiling Up Blocks

Time Limit:
1000MS
Memory Limit:
10000K
Total Submissions:
1937
Accepted:
755
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

*

#include <iostream>

#include <stdlib.h>

using namespace std;

int lm[10000][2];

int dp[10000];

int cmp(const void *a,const void *b)

{

int *aa = ((int *)a),*bb = ((int *)b);

if(aa[0] != bb[0])

{

return aa[0] - bb[0];

}

else

{

return aa[1] - bb[1];

}

}

int solution(int n)

{

int i,j;

for(i = 0;i < n;i++)

{

dp[i] = 1;

}

for(i = 1;i < n;i++)

{

for(j = 0;j < i;j++)

{

if(lm[j][1] <= lm[i][1] && dp[i] < dp[j] + 1)

{

dp[i] = dp[j] + 1;

}

}

}

return dp[i - 1];

}

int main(void)

{

int n;

while(1)

{

cin>>n;

if(n != 0)

{

for(int i = 0;i < n;i++)

{

cin>>lm[i][0]>>lm[i][1];

}

qsort(lm,n,sizeof(lm[0]),cmp);

cout<<solution(n)<<endl;

}

else

{

cout<<'*'<<endl;

break;

}

}

return 0;

}

动态规划还是不行。。。这么水的DP都搞了半天。。。。。。。主要是这几步

if(lm[j][1] <= lm[i][1] && dp[i] < dp[j] + 1)

这里要<=,如果只是<那不行

还有一点就是dp[i]数组初始化的时候都要为1因为每一个至少有1就是他自己。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: