Poj 1609 Tiling Up Blocks (LIS--可以相等)

来源:互联网 发布:网络强国是哪几个 编辑:程序博客网 时间:2024/05/20 16:44

题目链接:http://poj.org/problem?id=1609


Tiling Up Blocks
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5720 Accepted: 2227

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

33 21 12 354 22 43 31 15 50

Sample Output

23*

Source


题目大意:堆积木,看能堆多高,下面的L M必须大于上面的 L M                                          

解析:LIS (可以相等)


代码1: (n^2)

#include<iostream>#include<algorithm>#include<map>#include<stack>#include<queue>#include<set>#include<string>#include<cstdio>#include<cstring>#include<cctype>#include<cmath>#define N 40009using namespace std;const int inf = 0x3f3f3f3f;const int mod = 1e9 + 7;const double eps = 1e-8;const double pi = acos(-1.0);typedef long long LL;typedef struct{    int u, v;}Q;Q a[N];int cmp(Q m, Q n){    return m.u == n.u ? m.v < n.v : m.u < n.u;}int dp[N];/*int Seach(int l, int r, int k){    int m = (l + r) >> 1;    if(m == 0 && k > ans[m] && k < ans[1]) return 1;    if(m == 0) return 0;    if(ans[m] >= k && ans[m - 1] < k) return m;    if(k > ans[m]) return Seach(m + 1, r, k);    return Seach(l, m, k);}*/int main(){    int i, j, n;    while(~scanf("%d", &n))    {        if(n == 0) {printf("*\n"); continue;}        for(i = 1; i <= n; i++)        {            scanf("%d%d", &a[i].u, &a[i].v);        }        sort(a + 1, a + 1 + n, cmp);        int ans = 0;        for(i = 1; i <= n; i++)        {            dp[i] = 1;            for(j = 1; j < i; j++)            {                if(dp[i] < dp[j] + 1 && a[i].u >= a[j].u && a[i].v >= a[j].v)                    dp[i] = dp[j] + 1;            }            ans = max(dp[i], ans);        }        printf("%d\n", ans);    }    return 0;}

代码2:(nlog(n))

#include<iostream>#include<algorithm>#include<map>#include<stack>#include<queue>#include<set>#include<string>#include<cstdio>#include<cstring>#include<cctype>#include<cmath>#define N 40009using namespace std;const int inf = 0x3f3f3f3f;const int mod = 1e9 + 7;const double eps = 1e-8;const double pi = acos(-1.0);typedef long long LL;typedef struct{    int u, v;}Q;Q a[N];int ans[N];int cmp(Q m, Q n){    return m.u == n.u ? m.v < n.v : m.u < n.u;}int main(){    int i, n;    while(~scanf("%d", &n))    {        if(n == 0) {printf("*\n"); continue;}        for(i = 1; i <= n; i++)        {            scanf("%d%d", &a[i].u, &a[i].v);        }        sort(a + 1, a + 1 + n, cmp);        memset(ans, 0x3f, sizeof(ans));        for(i = 1; i <= n; i++)        {            *upper_bound(ans, ans + n, a[i].v) = a[i].v;        }        int r = lower_bound(ans, ans + n, inf) - ans;        printf("%d\n", r);    }    return 0;}



0 0