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. 【转】android 内存泄漏相关收藏博客。

    关于android内存泄漏的研究   博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基 ...

  2. VMware vSphere HyperVisor安装过程记录

      作者:sdjnzqr 出处:http://www.cnblogs.com/sdjnzqr/ 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须在文章中给出原文 ...

  3. 客户端服务端通信protocol

    这个协议不知我在上面耗费了多长时间,也有人问过我咋回事,这个protocol不长,但对于我来说理解起来很费劲,今天回来看看忽然看懂了(80%),只能说不知看了多少遍 其实这些东西应该在来的一个月这样子 ...

  4. Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi

    Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi This spring, Kaggle hosted two competitions w ...

  5. PAT-乙级-1047. 编程团体赛(20)

    1047. 编程团体赛(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 编程团体赛的规则为:每个参赛队由若 ...

  6. [Unity3D+算法]一小时做个2048

    原地址:http://blog.csdn.net/dingxiaowei2013/article/details/36462749 048是继FlappyBird之后另一个比较热的轻量级的手游,简单易 ...

  7. Cent Os 常用操作

    开放端口 编辑iptables文件(/etc/sysconfig/iptables) -A INPUT -m state --state NEW -m tcp -p tcp --dport xx端口号 ...

  8. POJ3267The Cow Lexicon

    http://poj.org/problem?id=3267 题意 : 给你一个message,是给定字符串,然后再给你字典,让你将message与字典中的单词进行匹配,输出要删掉多少字母. 思路 : ...

  9. C#中的文件操作

    在.NET Framework 中进行的所有输入和输出工作都要用到流(stream) 有两种类型的流: 输出流:当向某些外部目标写入数据时,就要用到输出流(将数据写入到文件中). 输入流:用于将数据读 ...

  10. 欧拉工程第63题:Powerful digit counts

    题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...