Description
给出一个长度为 的序列,每个元素是个二元组,要求选取最长子序列使得二元组第一维严格递增,第二维严格递减
Input
第一行一整数 表示用例组数,每组用例首先输入一整数 表示序列长度,之后 行每行输入二元组的两个元素
Output
输出满足条件的最长子序列长度
Sample Input
3
2
1.0 1.0
1.5 0.0
3
1.0 1.0
1.0 1.0
1.0 1.0
6
1.5 9.0
2.0 2.0
2.5 6.0
3.0 5.0
4.0 2.0
10.0 5.5
Sample Output
2
1
4
Solution
以 表示以第 个元素结尾的满足条件的子序列最长长度,则有以下转移
即为答案,时间复杂度
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=205;
int T,n,dp[maxn];
double a[maxn],b[maxn];
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i],&b[i]),dp[i]=1;
for(int i=2;i<=n;i++)
for(int j=1;j<i;j++)
if(a[j]<a[i]&&b[j]>b[i])
dp[i]=max(dp[i],dp[j]+1);
int ans=dp[1];
for(int i=2;i<=n;i++)ans=max(ans,dp[i]);
printf("%d\n",ans);
}
return 0;
}