Tiling Up Blocks
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 * Source |
题目大意:
有n个数对,求这n个数对的最长上升序列长度。
解题思路:
一开始竟然没有往LIS方向想,实在是没学好白书上的dp。
不过最后还是想到了LIS的思路,虽然在看了 别人的题解之前都没想起这是LIS的思路。
具体步骤就是先排序,这里是按照l第一排序,然后是按照m第二排序,
dp[i]就表示以第i个数对结尾,长度最大为多少。
没遍历一个i就遍历完i 之前的所有下标,找到一个最大的加一就可以作为i 的值了。
值的一说的是,原来走了一个弯路,就是dp[i]表示的是遍历到第i 个数时长度最大为多少,
因为不再是“以第i个数对结尾了”,需要另外用top存储结尾的m,
但是这样做有个问题,出在下面第一份代码的注释里面,有解说
下面是第一份ac代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 1000005
using namespace std;
int n;
struct node{
int l,m;
};
node box[10015];
int dp[10015];
int top[1015];
int comp(const node &a,const node &b)
{
if(a.l==b.l) return a.m<b.m;
else return a.l<b.l;
}
int main()
{
while(1){
scanf("%d",&n);
if(!n){
printf("*\n");
break;
}
for(int i=0;i<n;i+=1){
int l,m;
scanf("%d %d",&l,&m);
box[i].l=l,box[i].m=m;
dp[i]=1;
}
sort(box,box+n,comp);
dp[0]=1;
top[0]=box[0].m;
int res=0;
for(int i=1;i<n;i+=1){
top[i]=box[i].m;
for(int j=0;j<i;j+=1){
if(box[i].m>=box[j].m){
dp[i]=max(dp[i],dp[j]+1);
}
/*if(top[i]>=top[j]){
dp[i]=max(dp[i],dp[j]+1);
}
else if(dp[i]<dp[j]){
更新了的top[i]的值,是在后面将可能会用到的top[j]的值之前的,
到时候就会出现把用较前(l较小)的top[i]放在较后(大)的top[j]之上的情况
就是这里出错
top[i]=top[j];
dp[i]=dp[j];
}*/
}
res=max(res,dp[i]);
}
printf("%d\n",res);
}
return 0;
}
下面是第二种方法,采用一步一步比较加和的方式求解,也比较好懂,直接看代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 1000005
using namespace std;
int n;
int num[115][115];
int dp[115][115];
int main()
{
while(1){
int maxl=0,maxm=0;
scanf("%d",&n);
if(!n){
printf("*\n");
break;
}
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
for(int i=0;i<n;i+=1){
int l,m;
scanf("%d %d",&l,&m);
maxl=max(maxl,l);
maxm=max(maxm,m);
num[l][m]+=1;
}
int res=0;
for(int i=1;i<=maxl;i+=1){
for(int j=1;j<=maxm;j+=1){
dp[i][j]=max(dp[i-1][j],dp[i][j-1])+num[i][j];
}
}
printf("%d\n",dp[maxl][maxm]);
}
return 0;
}