hdu 1677 (dp)
2013-10-20 09:14
316 查看
Nested Dolls
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2153 Accepted Submission(s): 631
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
Source
2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
Recommend
lcy
题意就是把原来的序列划分成最少的集合,集合内的元素满足偏序关系。开始想最大匹配,然后发现时间不够。其实这是一个二维的“导弹拦截系统”,并且这里必须是严格的小于关系。做法是先把原来的元素按宽排序,对于宽相等的,必须按长从大到小排序,然后得到一个新的由长组成的序列。对这个序列,求反向最长不减序列的长度就是答案。原因是排在后面的长对应的宽一定大于等于前面的,而等于的情况,为了保证不会误选,使长从大到小排序。再根据最小集合划分等于最长反链长度,就可以求得答案了。不减序列的求法,就是把原来二分中的lower_bound改成upper_bound即可。
#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <map> #include <vector> #include<queue> #include<set> #include<cmath> using namespace std; typedef long long LL; typedef pair<double,int> P; const int maxn = 20000 + 5; const int INF = 1000000000; struct Node{ int x,y; }p[maxn]; bool cmp(Node a,Node b){ if(a.x == b.x) return a.y > b.y; return a.x < b.x; } int a[maxn]; int dp[maxn]; int n; void solve(){ fill(dp,dp+n,INF); for(int i = 0;i < n;i++){ *upper_bound(dp,dp+n,a[i]) = a[i]; } printf("%d\n",lower_bound(dp,dp+n,INF)-dp); } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i = 0;i < n;i++){ int x,y; scanf("%d%d",&p[i].x,&p[i].y); } sort(p,p+n,cmp); for(int i = 0;i < n;i++) a[i] = p[n-i-1].y; solve(); } return 0; }
相关文章推荐
- hdu----(1677)Nested Dolls(DP/LIS(二维))
- BestCoder Round #52 (div.2) HDU 5418 Victor and World (DP+状态压缩)
- hdu 6148 数位DP(板子 递增递减
- hdu 1011 Starship Troopers 树形背包dp
- hdu 5115 区间dp 狼BUFF
- hdu 2604 Queuing(矩阵优化DP)
- Hdu 1028 Ignatius and the Princess III(整数拆分问题,DP)
- HDU 6199gems gems gems (DP)
- HDU 2089 不要62 (数位DP)
- hdu 1133 Buy the Ticket (dp)
- hdu-2227-Find the nondecreasing subsequences(DP+离散化+树状数组)
- HDU 3709 数位DP
- HDU - 1494 跑跑卡丁车(BFS+DP)
- HDU 4389 X mod f(x)[数位统计dp]
- dp --- hdu 4939 : Stupid Tower Defense
- hdu 2870 dp
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
- hdu 6201 transaction transaction transaction tree dp
- HDU 1227 Fast Food DP
- hdu 1250 树形DP