uva 11368 - Nested Dolls(sort+greedy+binary_search)
2014-03-25 13:37
363 查看
G: Nested Dolls |
Input
On the first line of input is a single positive integer 1t20 specifyingthe number of test cases to follow. Each test case begins with a positive integer 1m20000 ona line of itself telling the number of dolls in the test case. Next follow2m positive integers w1, h1, w2, h2,..., wm, hm ,where wi is the width and hi is the height of doll number i. 1wi, hi10000 forall 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
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>using namespace std;struct doll{int w , h;doll(int a = 0 , int b = 0){w = a , h = b;}};vector<doll> D , ans;int m;bool cmp(doll d1 , doll d2){if(d1.h == d2.h){return d1.w<d2.w;}return d1.h>d2.h;}void ini(){ans.clear();D.clear();}void readcase(){int w , h;scanf("%d" , &m);for(int i = 0; i < m; i++){scanf("%d%d" , &w , &h);D.push_back(doll(w , h));}}int Binary_search(int w){int l = 0 , r = ans.size()-1;while(l < r){int mid = (l+r)/2;if(ans[mid].w <= w) l = mid+1;else r = mid;}return l;}void computing(){sort(D.begin() , D.end() , cmp);ans.push_back(D[0]);for(int i = 1; i < D.size(); i++){int tem = Binary_search(D[i].w);//cout << ans[tem].w << endl;if(ans[tem].w > D[i].w) ans[tem].w = D[i].w;else ans.push_back(D[i]);}printf("%d\n" , ans.size());}int main(){int t;scanf("%d" , &t);while(t--){ini();readcase();computing();}return 0;}[/code]
相关文章推荐
- UVA 11368 & POJ 3636 & HDU 1677 Nested Dolls(贪心 + 二分LIS)
- Why std::binary_search of std::list works, sorta ...(转载)
- 区间dp(uva10304-Optimal Binary Search Tree)
- 算法学习笔记之——priority queue、heapsort、symbol table、binary search trees
- UVa 10304 - Optimal Binary Search Tree
- STL_Algorithm6--find, find_if, sort, binary_search
- UVA - 10304 Optimal Binary Search Tree
- uva 1264 Binary Search Tree 计数
- UVaLive 4847 - Binary Search Tree (与BST有关的计数问题)
- map queue sort binary_search
- UVa 10304 Optimal Binary Search Tree(区间DP)
- The bubble sort and Binary search
- DP优化——四边形不等式 uva 10304 - Optimal Binary Search Tree
- UVA - 10304 Optimal Binary Search Tree
- UVA 10304 Optimal Binary Search Tree (区间dp)
- uva-10304 Optimal Binary Search Tree(区间dp)
- UVA 10304 Optimal Binary Search Tree
- UVA 10304 Optimal Binary Search Tree
- UVA 10304 - Optimal Binary Search Tree
- 关于sort、binary_search