hdu1677 转换LIS

来源:互联网 发布:photoshop6 mac 编辑:程序博客网 时间:2024/06/06 02:37

题目大意:将小的布娃娃套进大的布娃娃总共需要多少个布娃娃。

思路:按宽度作为第一关键字升序排列,高度作为第二关键字降序排列,则高度的LIS就是所求的解。因为宽度是高到低,所以前面的必定能被LIS序列里面的某个数套进去,还有因为20 10不能套进20 10.所以在求LIS是不能用lower_bound(),因为lower_bound()求得是他所处位置但是在等于他本身那个数字的前面。所以要用upper_bound(),就可以了。。


#include <cstdio>#include <string>#include <cstring>#include <fstream>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <iostream>#include <iomanip>using namespace std;#define maxn 20055#define MOD 1000000007#define mem(a) memset(a , 0 , sizeof(a))#define memx(a) memset(a , 0x7f , sizeof(a))#define LL __int64#define INF 999999999struct node{    int w , h;}arr[maxn];int g[maxn];bool cmp(node n1 , node n2){    if(n1.w == n2.w) return n1.h < n2.h;    else return n1.w > n2.w;}int main(){    int t;    scanf("%d" , &t);    while(t--)    {        int n ;        scanf("%d" , &n);        for(int i = 0 ; i < n ; i ++) scanf("%d %d" , &arr[i].w , &arr[i].h);        sort(arr , arr + n , cmp);        memx(g);        int ans = 0;        for(int i = 0 ; i < n ; i ++)        {            int k = upper_bound(g , g + n , arr[i].h) - g;            g[k] = arr[i].h;            ans = max(ans , k+1);        }        printf("%d\n" , ans);    }    return 0;}/*10430 10 20 13 20 14 20 15410 10 20 10 20 10 30 25410 15 20 10 20 30 30 25320 30 40 50 30 40420 30 10 10 30 20 40 50310 30 20 20 30 10410 10 20 30 40 50 39 51*/


0 0
原创粉丝点击