Nested Dolls
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2643 Accepted Submission(s): 785
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
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
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>
using namespace std;
struct Doll{
int w,h;
}d[];
int tt,n,f[];
bool cmp(Doll a, Doll b)
{
if(a.w==b.w) return(a.h>b.h);
else return(a.w<b.w);
}
int LIS()
{
int ftmp[];//长度为i的最小的一个数
memset(ftmp,,sizeof(ftmp));
int tail=;
ftmp[]=;
for(int i=; i<n; i++)
{
int l=,r=tail;
while(l<r-)
{
int mid=(l+r)/;
if(ftmp[mid]<=d[i].h) l=mid;
else r=mid;
}
if(ftmp[r]<=d[i].h) l=r;
if(ftmp[l]<=d[i].h)
ftmp[l+]=d[i].h;
if(l==tail) tail++;
}
return tail;
}
int main()
{
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
for(int i=; 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 ;
}
有Algorithm:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Doll{
int w,h;
}d[];
int tt,n,f[];
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=; 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=; 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 ;
}
要注意的是 <algorithm>中
upper_bound()表示在有序数列中找到第一个>val的iterator
lower_bound()表示在有序数列中找到第一个>=val的iterator