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. OC的@property 和 @synthesize id

    学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...

  2. 微信公众账号怎么获取微信原始ID

    阅读号获取如下,服务号不确定,见图: 进入你的微信公众账号的地址(https://mp.weixin.qq.com ),登录之后进入如下

  3. Apache Spark探秘:三种分布式部署方式比较

    转自:链接地址: http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/     目 ...

  4. MVC3 Model Binding验证方式

    1.使用ModelState在Action中进行验证 [HttpPost] public ViewResult MakeBooking(Appointment appt) { if (string.I ...

  5. jquery的一些用法

    一.选择器 单选按钮:$(this).find(".answer").find("input[name='answer_" + id + "']:ch ...

  6. vc6命令行编译工程方法

    查vc++ 6.0 的 msdn找到下面的命令: msdev FileName [/MAKE "ProjectName – ConfigName | ALL"] [/REBUILD ...

  7. Nginx正确记录post日志的方法

    Nginx正确记录post日志的方法 事实上可以很简单,这取决于把 access_log 放在哪个 location 里面. 一,放到包含fastcgi_pass或proxy_pass的Locatio ...

  8. gcc / g++ 编译选项

    g++ -Wall -m64 -W -O2 a.cpp b.cpp -o a 用下面的命令编译,生成libtriangle.so  先生成动态库 g++ -g -fpic -shared -o lib ...

  9. 六步实现Rest风格的API

    Rest的作者认为计算机发展到现在,最大的成就不是企业应用,而是web,是漫漫无边的互联网web世界.Web能有这么大的成就,它值得我们研究.所以Rest的作者仔细研究了Web,按照Web的世界一些关 ...

  10. 由CHAR(2)引发的BUG

    我们在设计数据库标志位字段时,为考虑其扩展性,一般会设置为CHAR(2),例如 FLAG CHAR(2),这样我们就需要注意了,如果你给字段 FLAG赋值为‘0’,它在数据库中的真实情况是‘0+空格’ ...