您的位置:首页 > 其它

uva 11368 - Nested Dolls(sort+greedy+binary_search)

2014-03-25 13:37 363 查看

G: Nested Dolls

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 secondsmallest, 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 eachnested 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 andheight h2 if and only if w1 < w2 and h1 < h2 . Can you help him calculate thesmallest 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 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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: