SPOJ 3943 Nested Dolls

来源:互联网 发布:大数据对零售业 编辑:程序博客网 时间:2024/06/06 06:48

【SPOJ 3943 Nested Dolls】

Time Limit: 156MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu 

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 hewonders if there is another way of nesting them so he will end up withfewer nested dolls? After all, that would make his collection even more magnificent! He unpacks each nested doll and measures the widthand height of each contained doll. A doll with width w1 and height h1 willfit in another doll of width w2 and height h= if and only if w1 < w2 and h1 < h2. Can you help him calculate the smallest number of nested dollspossible to assemble from his massive list of measurements?

Input】On the first line of input is a single positive integer 1 ≤ t ≤ 20 specifyingthe 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 dollsin 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.【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

Output】For each test case there should be one line of output containing the minimumnumber of nested dolls possible.

【SAMPLE OUTPUT】

1

2

3

2

【Hint】Time limit: 0.156s-0.259sSource limit: 50000B Memory limit: 1536MB Cluster: Cube (Intel G860)Languages: All except: ERL JS NODEJS PERL 6 VB.netResource: Nordic 2007

【题目中文大意】现在n(<=20000)个俄罗斯套娃,每个都有宽度wi和高度hi(均小于10000),要求w1<w2并且h1<h2的时候才可以合并,问最少能剩几个。

【题目思路】题目要求wi>wj && hi>hj才能合并。 那么可以先贪心使这个序列满足wi>wj,那么不满足的就是hi<=hj,如果wi==wj,那么就满足排序hi<hj,这样就相当于是在排序后h序列中找出一个LIS(最长不下降子序列)**LIS**可以用DP(我记得我写过dp的LIS,,这是一个坑,记得填坑!!),O(n)查找,,,这里讲O(logn)二分查找也可以用upper_bound(),不过这个他是每一个都加入,再找,感觉看的不是很懂,感兴趣的同学给个友链友链点这里

代码

//wi>wj hi>hj 那么可以先贪心使这个序列满足wi>wj,,如果wi==wj,那么就满足hi<hj
//然后再求LIS(最长不下降子序列),就可以求出有多少个不满足条件了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define cle(x) memset(x,0,sizeof(x))
#define clemax(x) memset(x,127,sizeof(x))
using namespace std;
const int N=20020;
struct ii{
 int w,h;
 ii(int w=0,int h=0):w(w),h(h){ }
 bool operator <(const ii &a)const{
  if(w^a.w)return w>a.w;
  return h<a.h;
 }
}child[N];
int last[N],ans,n;
int find(int x){
 int l=1,r=ans;
 int tmp=0;
 while(l<=r){
  int mid=(l+r)>>1;
  if(last[mid]<=x)tmp=mid,l=mid+1;//找到最后一个小于等于他的
  else r=mid-1;
 }
 return tmp;
}
int T;
int main(){
 freopen("spoj.in","r",stdin);
 freopen("spoj.out","w",stdout);
 scanf("%d",&T);
 for(int o=1;o<=T;o++){
  scanf("%d",&n);
  cle(child),clemax(last);
  ans=0;
  for(int i = 1; i<= n; i++)
   scanf("%d%d",&child[i].w,&child[i].h);
  sort(child+1,child+n+1);
  for(int i = 1; i<= n; i++)//求LIS也就是所剩下的
  {
   int tmp=find(child[i].h);
   if(last[tmp+1]>child[i].h)last[tmp+1]=child[i].h;
   ans=max(ans,tmp+1);
  }
  printf("%d\n",ans);
 } 
}


0 0
原创粉丝点击