题解:首先跟据长度进行排序,然后就是能最少选出多少个最长递增子序列,答案也就是求此时这个序列的最长递减子序列的长度是多少?这个和这个题基本一样传送门,目前还不会证明
附上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5010;
struct node{
int len,wei;
};
node stick[maxn];
int l[maxn];
bool cmp(node a,node b)
{
if(a.len==b.len){
return a.wei<b.wei;
}
return a.len<b.len;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&stick[i].len,&stick[i].wei);
}
sort(stick,stick+n,cmp);
memset(l,0,sizeof(l));
int length=1;
for(int i=n;i>=1;i--){
if(l[length-1]<stick[i].wei){
l[length++]=stick[i].wei;
}else{
*lower_bound(l,l+length,stick[i].wei)=stick[i].wei;
}
}
printf("%d\n",length-1);
}
return 0;
}