(複習)poj 1609 簡單dp

Tiling Up Blocks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5050   Accepted: 1975

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;
}








發佈了91 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章