动态规划(题型)

最长上升子序列

在这里插入图片描述

 int t=0;
        rep(i,1,n){
            f[i]=1;
            for(int j=1;j<i;j++)
                if(c[i]>=c[j])
                    f[i]=max(f[i],f[j]+1);
            t=max(t,f[i]);
        }

由于DP版本复杂度为 O ( N 2 ) O(N^2) O(N2),不符合题目要求,所以使用二分

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define endl '\n'
#define ms(x, n) memset(x,n,sizeof (x));
using ll = long long;
const int N = 1e6 + 10, mod = 998244353;

int T,n;
int a[N],b[N],c[N],f[N];

int main(){
    cin>>T;

    while(T--){
        ms(f,0);
        rep(i,1,n) a[i]=b[i]=c[i]=0;
        cin>>n;
        
        rep(i,1,n){
            int x,y;
            cin>>x>>y;
            if(x==1 && y==0) a[i]=1;
            else if(x==0 && y==1) a[i]=3;
            else a[i]=2;
        }

        rep(i,1,n){
            cin>>b[i];
            c[i]=a[b[i]];
        }

        int t=0;
        q[1]=-0x3f3f3f3f;
        rep(i,1,n){
            int l=0,r=t+1;
            while(l+1<r){
                int mid=l+r>>1;
                if(q[mid]>c[i]) r=mid;
                else l=mid;
            }
            t=max(t,r);
            q[r]=c[i];
        }
        cout<<n-t<<endl;
    }
    return 0;
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值