HDU 1677
Nested Dolls
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2643 Accepted Submission(s): 785
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
2
3
2
#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的更多相关文章
- hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1677 Nested Dolls
过了之后感觉曾经真的做过这样的类型的题. 之前一直非常疑惑二级排序的优先级问题,如今发现二级排序真的没有绝对的优先级. 对于此题,若按W排序,则有1到i件物品的W均小于等于第i+1件物品(设为A)的W ...
- 8.3 LIS LCS LCIS(完结了==!)
感觉这个专题真不好捉,伤心了,慢慢啃吧,孩纸 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28195#overview 密码 ac ...
- LCS,LIS,LCIS
网站:CSUST 8月3日(LCS,LIS,LCIS) LCS: 以下讲解来自:http://blog.csdn.net/yysdsyl/article/details/4226630 [问 ...
- HDU 1257 最少拦截系统【LIS】
题意:类似于套娃娃,问最少需要多少个拦截系统. 思路: 假设已经有m个导弹拦截序列 r1:x11>=x12>=x13>=...>=x1n r1:x21>=x22>= ...
- HDU——PKU题目分类
HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...
- [转] HDU 题目分类
转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- HDU 1277 Nested Dolls
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的 ...
随机推荐
- 【转】android 内存泄漏相关收藏博客。
关于android内存泄漏的研究 博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基 ...
- VMware vSphere HyperVisor安装过程记录
作者:sdjnzqr 出处:http://www.cnblogs.com/sdjnzqr/ 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须在文章中给出原文 ...
- 客户端服务端通信protocol
这个协议不知我在上面耗费了多长时间,也有人问过我咋回事,这个protocol不长,但对于我来说理解起来很费劲,今天回来看看忽然看懂了(80%),只能说不知看了多少遍 其实这些东西应该在来的一个月这样子 ...
- Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi
Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi This spring, Kaggle hosted two competitions w ...
- PAT-乙级-1047. 编程团体赛(20)
1047. 编程团体赛(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 编程团体赛的规则为:每个参赛队由若 ...
- [Unity3D+算法]一小时做个2048
原地址:http://blog.csdn.net/dingxiaowei2013/article/details/36462749 048是继FlappyBird之后另一个比较热的轻量级的手游,简单易 ...
- Cent Os 常用操作
开放端口 编辑iptables文件(/etc/sysconfig/iptables) -A INPUT -m state --state NEW -m tcp -p tcp --dport xx端口号 ...
- POJ3267The Cow Lexicon
http://poj.org/problem?id=3267 题意 : 给你一个message,是给定字符串,然后再给你字典,让你将message与字典中的单词进行匹配,输出要删掉多少字母. 思路 : ...
- C#中的文件操作
在.NET Framework 中进行的所有输入和输出工作都要用到流(stream) 有两种类型的流: 输出流:当向某些外部目标写入数据时,就要用到输出流(将数据写入到文件中). 输入流:用于将数据读 ...
- 欧拉工程第63题:Powerful digit counts
题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...