GYM 101653 O.Diamonds(dp)

Description

给出一个长度为 n 的序列,每个元素是个二元组,要求选取最长子序列使得二元组第一维严格递增,第二维严格递减

Input

第一行一整数 T 表示用例组数,每组用例首先输入一整数 n 表示序列长度,之后 n 行每行输入二元组的两个元素 a i , b i

( 1 T 100 , 1 n 200 , 0 a i , b i 10.0 )

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

d p [ i ] 表示以第 i 个元素结尾的满足条件的子序列最长长度,则有以下转移

d p [ i ] = m a x ( d p [ j ] ) + 1 , 1 j < i , a j < a i , b j > b i

m a x ( d p [ i ] ) , 1 i n 即为答案,时间复杂度 O ( n 2 )

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;
}

猜你喜欢

转载自blog.csdn.net/v5zsq/article/details/80405000