Nested Dolls

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2643    Accepted Submission(s): 785

Problem Description
Dilworth is the world’s most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls of different sizes of which the smallest doll is contained in the second smallest, and this doll is in turn contained in the next one and so forth. One day he wonders if there is another way of nesting them so he will end up with fewer nested dolls? After all, that would make his collection even more magnificent! He unpacks each nested doll and measures the width and height of each contained doll. A doll with width w1 and height h1 will fit in another doll of width w2 and height h2 if and only if w1 < w2 and h1 < h2. Can you help him calculate the smallest number of nested dolls possible to assemble from his massive list of measurements?
 
Input
On the first line of input is a single positive integer 1 <= t <= 20 specifying the number of test cases to follow. Each test case begins with a positive integer 1 <= m <= 20000 on a line of itself telling the number of dolls in the test case. Next follow 2m positive integers w1, h1,w2, h2, . . . ,wm, hm, where wi is the width and hi is the height of doll number i. 1 <= wi, hi <= 10000 for all i.
 
Output
For each test case there should be one line of output containing the minimum number of nested dolls possible.
 
Sample Input
4
3
20 30 40 50 30 40
4
20 30 10 10 30 20 40 50
3
10 30 20 20 30 10
4
10 10 20 30 40 50 39 51
 
Sample Output
1
2
3
2
求最长不下降子序列,顺便试试使用了强大的STL来求
 
有两种算法,
  一种是按照w从小到大,w相同则按h从小到大排,依次入队,每次找到前面最大的且能被当前玩偶a装下的玩偶b,删除b,插入a,保证h有序,需要用Treap实现。还不会这么强大的数据结构。
  另一种是按照w从小到大,w相同则按h从大到小排,从尾部向前开始找最长“不下降”子序列,即找出最多两两不能装下的玩偶数,可以证明,最少的合法的玩偶合并方法中,这中间每一个都不能够被放在同一个玩偶合并方法中
 
无Algorithm:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct Doll{
int w,h;
}d[];
int tt,n,f[];
bool cmp(Doll a, Doll b)
{
if(a.w==b.w) return(a.h>b.h);
else return(a.w<b.w);
}
int LIS()
{
int ftmp[];//长度为i的最小的一个数
memset(ftmp,,sizeof(ftmp));
int tail=;
ftmp[]=;
for(int i=; i<n; i++)
{
int l=,r=tail;
while(l<r-)
{
int mid=(l+r)/;
if(ftmp[mid]<=d[i].h) l=mid;
else r=mid;
}
if(ftmp[r]<=d[i].h) l=r;
if(ftmp[l]<=d[i].h)
ftmp[l+]=d[i].h;
if(l==tail) tail++;
}
return tail;
}
int main()
{
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
for(int i=; i<n; i++)
scanf("%d%d",&d[i].w,&d[i].h);
sort(d,d+n,cmp);
reverse(d,d+n);
printf("%d\n",LIS());
}
return ;
}

有Algorithm:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Doll{
int w,h;
}d[];
int tt,n,f[];
bool cmp(Doll a, Doll b)
{
if(a.w==b.w) return(a.h>b.h);
else return(a.w<b.w);
}
int LIS()
{
vector<int> ftmp;//长度为i的最小的一个数
for(int i=; i<n; i++)
{
vector<int>::iterator it=upper_bound(ftmp.begin(),ftmp.end(),d[i].h);
if(it!=ftmp.end())
*it=d[i].h;
else
ftmp.push_back(d[i].h);
}
return ftmp.size();
}
int main()
{
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
for(int i=; i<n; i++)
scanf("%d%d",&d[i].w,&d[i].h);
sort(d,d+n,cmp);
reverse(d,d+n);
printf("%d\n",LIS());
}
return ;
}

要注意的是 <algorithm>中

  upper_bound()表示在有序数列中找到第一个>val的iterator

  lower_bound()表示在有序数列中找到第一个>=val的iterator

HDU 1677的更多相关文章

  1. hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. HDU 1677 Nested Dolls

    过了之后感觉曾经真的做过这样的类型的题. 之前一直非常疑惑二级排序的优先级问题,如今发现二级排序真的没有绝对的优先级. 对于此题,若按W排序,则有1到i件物品的W均小于等于第i+1件物品(设为A)的W ...

  3. 8.3 LIS LCS LCIS(完结了==!)

    感觉这个专题真不好捉,伤心了,慢慢啃吧,孩纸 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28195#overview 密码  ac ...

  4. LCS,LIS,LCIS

    网站:CSUST 8月3日(LCS,LIS,LCIS) LCS:      以下讲解来自:http://blog.csdn.net/yysdsyl/article/details/4226630 [问 ...

  5. HDU 1257 最少拦截系统【LIS】

    题意:类似于套娃娃,问最少需要多少个拦截系统. 思路: 假设已经有m个导弹拦截序列 r1:x11>=x12>=x13>=...>=x1n r1:x21>=x22>= ...

  6. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

  7. [转] HDU 题目分类

    转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...

  8. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  9. HDU 1277 Nested Dolls

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的 ...

随机推荐

  1. object to primitive in javascript

    例1: var a={};  alert(a); //[object Object]; 例2: var a={ toString:function(){ return 1; } } alert(a); ...

  2. NodeJS - Express 4.0下使用app.dynamicHelpers错误

    在NodeJS - Express 4.0下使用app.dynamicHelpers发生错误: app.dynamicHelpers({ ^ TypeError: Object function (r ...

  3. java 把URL中的中文转换成utf-8编码

    private static final String QUERY = "餐饮"; String sr = URLEncoder.encode(QUERY); System.out ...

  4. c++ 从标注输入流读取行

    #include <string.h> #include <iostream> #include <vector> #include <stdio.h> ...

  5. Matlab中find函数的使用

    一.问题来源 看到了 min_score_pos = find(A0_scores==min(A0_scores), 1); [r,c] = find(X,k),返回X中第k个非零元素的行列位置. 二 ...

  6. matlab中函数fscanf

    matlab中函数fscanf matlab中函数fscanf在文件读取方面的实例如下: 从文件中有格式地读数据 fscanf 语法1:[a,count]=fscanf(fid,format,size ...

  7. linux cmake 安装mysql5.5.11,以及更高版本

    1.下载mysql5.5.12和cmake wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.12-linux2.6-i686.tar.gz ...

  8. Unity3D Development模式下的一个小问题

    今天客户提交了一个反馈,说测试版本的应用在按下电源键的时候屏幕变黑,然后重新按下电源键启动的时候发现没有出现屏幕锁屏的情况,直接回到应用界面. 我这边看了一下,发现如果装了360之类的手机助手就没这个 ...

  9. SSH Tunneling Explained

    转载: http://chamibuddhika.wordpress.com/2012/03/21/ssh-tunnelling-explained/ March 21, 2012 by Buddhi ...

  10. POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)

    笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...