HDU 1677
2014-08-06 23:32
246 查看
Nested Dolls
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2643 Accepted Submission(s): 785
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case there should be one line of output containing the minimum number of nested dolls possible.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
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> #include <vector> using namespace std; struct Doll{ int w,h; }d[20005]; int tt,n,f[20005]; 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=0; 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=0; 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 0; }
View Code
要注意的是 <algorithm>中
upper_bound()表示在有序数列中找到第一个>val的iterator
lower_bound()表示在有序数列中找到第一个>=val的iterator
相关文章推荐
- hdu 1677 (dp)
- HDU 1677:Nested Dolls
- hdu 1677
- HDU 1677 LIS
- HDU 1677 Nested Dolls (贪心)
- 【HDU 1275 && HDU 1677】 两题类似,经典 DP。
- hdu 1677 Nested Dolls(贪心+LIS模板题)
- hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】
- HDU 1677 Nested Dolls
- HDU 1677 Nested Dolls
- UVA 11368 & POJ 3636 & HDU 1677 Nested Dolls(贪心 + 二分LIS)
- hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】
- hdu 1677 Nested Dolls 子串
- HDU 1677 Nested Dolls
- hdu 1677 Nested Dolls (Greedy + Treap)
- HDU 1677 二分法进行判断
- HDU 1677 Nested Dolls(排序+二分)
- HDU 1677 与1257类似 排序 + 二分
- hdu 1677 Nested Dolls LIS + 动态规划
- hdu----(1677)Nested Dolls(DP/LIS(二维))