hdu 1677 Nested Dolls 子串
题目:
A - Nested Dolls
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
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
题目大意:
给n个娃娃,让你按照长和宽来嵌套,求最多形成几个娃娃.注:被嵌套的不算一个.长宽不能交换.长等于宽不能交换
题目思路:
1、先将长从大到小排列,使其变成一维问题
2、利用最长单调序列求答案
题目优化:
1、如果只用最长单调子序列求,可能会超时,所以可以二分答案
2、但如果题目时间要求短用二分搜索答案还是可能超时
3、所以采用反最长单调递增子序列( 最长单调非递减子序列)
4、分析:假设长按照单调递减来求,简化为下图.如果前面的高于后面的,后面必然会在他的子序列里面,否则就ans++; 这样就转化为求最长单调非递减子序列
5 55
3 3
2 2
程序:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#include <fstream>
#include <limits>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cassert>
using namespace std;
int b[30000];
struct node
{int x,y;} a[30000];
bool cmp(node a,node b)//按长排序从小到大
{if(a.x!=b.x)return a.x<b.x;else return a.y>b.y;
}
int main()
{int ci,n;scanf("%d",&ci);while(ci--){scanf("%d",&n);for(int i=1; i<=n; i++){scanf("%d%d",&a[i].x,&a[i].y);}sort(a+1,a+n+1,cmp);b[1]=1;int ans=0,time=0,len=1;for(int i=2; i<=n; i++)//最长单调非递减子序列if(a[i].x==a[b[len]].x||a[i].y<=a[b[len]].y){len++;b[len]=i;}else//维护序列数组{for(int j=1; j<=len; j++)if(a[i].y>a[b[j]].y){b[j]=i;break;}}printf("%d\n",len);}
return 0;
}
hdu 1677 Nested Dolls 子串相关推荐
- HDU OJ 1677 Nested Dolls【二分,LIS】
原题连接:http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意:每组测试数据给n个硬币,现在给你这n个硬币的长和高,若一硬币的长和高都小于另一个硬币,则这 ...
- HDU 1277 Nested Dolls
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的 ...
- HDU - 7091 重叠的子串(后缀自动机+set启发式合并+树上倍增)
题目链接:点击查看 题目大意:给定一个只含小写字母的字符串 sss 和 qqq 次询问,每次询问给定一个字符串,以 s[l..r]s[l..r]s[l..r] 的形式给出,判断 sss 中是否存在两个 ...
- 2021CCPC华为云挑战赛:HDU 7091 重叠的子串(SAM + 线段树合并)
重叠的子串 给定一个长度为n(1≤∣s∣≤105)n(1 \le \mid s \mid \le 10 ^ 5)n(1≤∣s∣≤105)的只由小写字母构成的字符串sss,有m,(1≤m≤106)m, ...
- HDU1677 Nested Dolls
/* 这是杭电1677那道题 这算一道动态规划题,说起动态规划,不得不说背包九讲真是大牛呀,每次看都会有些收获 这道题难点之一是将其转化为,求最长单调非递增子序列 记得刚开始看到这道题的时候,没头没脑 ...
- hdu 4160 (Dolls)二分图的最小路径覆盖
关于二分图,让人很头疼啊!归结为一句话,就是看不出来题目是二分图的问题. 也许是对二分图不太熟悉吧!需要多练习! 题目大意:给出n个箱子,每个箱子的参数为长,宽,高:(a,b,c):当且仅当箱子s1, ...
- EOJ1765 Nested Dolls 最长上升子序列
本题我试过很多方法,最开始的思路是,一个娃娃可以套另一个娃娃,这种偏序关系可以建图.找出图中最长的路径,然后把那些点删掉,再找出最长的路径,删掉,直至图中没有点,删除的次数就是答案,可是这样会超时.第 ...
- 杭电OJ分类题目(2)
原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(2) HDU Water~~~ HDU 100 ...
- Competitive Programming 3题解
题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...
最新文章
- 软件开发过程中遇到的问题
- python操作excel-python操作excel(内附python教程分享)
- C#里调用带输出参数的存储过程
- TCP之超时重传机制
- Win10操作系统不能访问共享文件夹
- slice 和 splice 区别
- 全局处理ajax请求时session超时
- html调用python_对Python3 解析html的几种操作方式小结
- JDK源码(19)-Package
- arctime工程文件怎么打开_微信dat后缀的文件怎么打开
- 计算机软件行业绩效,关于软件行业的绩效考核.doc
- 谷歌浏览器不能用_正在用 Chrome 或 Edge 浏览器的你,不能错过这亿个好用插件...
- Scratch编程入门-画图模块1【认识画图模块积木】
- chrome误删书签恢复
- 研究生学术英语写作网课答案
- yum 与pip区别
- 小米笔记本如何开启VT虚拟化
- hadoop错误org.apache.hadoop.yarn.exceptions.YarnException Unauthorized request to start container
- 服务器维修故障诊断思路大全
- 利用pandas进行exceld 的列相加求和