线性dp测试总结

· · 算法·理论

线性dp测试

第一题

这题我们和之前一样,可是变成了两个元素,那么我们先对a_i排序,把原本的二分比较一个元素,改乘两个元素比较注意:if的条件之间要用||连接

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n;
int dp[N];
struct node{
    int l,r;
    bool operator<(const node &x)const{
        if(l==x.l){
            return r<x.r;
        }
        return l<x.l;
    }
}a[N],b[N];
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].l>>a[i].r;
        b[i].l=1e9;
        b[i].r=1e9;
    }
    sort(a+1,a+n+1);
    int cnt=1;
    for(int i=1;i<=n;i++){
        int l=1,r=n,pos=0;
        while(l<=r){
            int mid=l+(r-l)/2;
            if(b[mid].l>=a[i].l||b[mid].r>=a[i].r){
                pos=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        if(b[pos].l>=a[i].l||b[pos].r>=a[i].r)b[pos]=a[i];
        dp[i]=pos;
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}

第三题

我们可以对每个零件排序,如果零件的大小有上升趋势就可以了就需要用时,那么问题就转换为了求最长上升子序列

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+5;
int n;
struct node{
    int w,l;
    bool operator<(const node &x)const{
        if(w==x.w)return l>x.l;
        return w>x.w;
    }
}a[N];
vector<int>b;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].w>>a[i].l; 
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++){
        int x=a[i].l;
        auto pos=lower_bound(b.begin(),b.end(),x);
        if(pos==b.end()){
            b.push_back(x); 
        }else{
            b[pos-b.begin()]=x;
        }
    }
    cout<<b.size();
    return 0;
}

第四题

这题的第一问我们会,可是第二问怎么办呢,那么就需要一个定理,名叫Dilworth定理,这个定理就解决第二个问题,把第二个问题变成了求最长上升子序列,那么就可以解决这道题了

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+5;
int a[N];
vector<int>b,c;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int total=0;
    while(cin>>a[++total]);
    total--;
    for(int i=1;i<=total;i++){
        int x=a[i];
        auto pos=upper_bound(b.begin(),b.end(),x,greater<int>());
        if(pos==b.end()){
            b.push_back(x);
        }else{
            b[pos-b.begin()]=x;
        }
    }
    for(int i=1;i<=total;i++){
        int x=a[i];
        auto pos=lower_bound(c.begin(),c.end(),x);
        if(pos==c.end()){
            c.push_back(x);
        }else{
            c[pos-c.begin()]=x;
        }
    }
    cout<<b.size()<<'\n'<<c.size();
    return 0;
}

第五题

这题就是普通的dp题

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+5;
int n;
int x,y,z;
int dp[N][2];
string s;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>x>>y>>z>>s;
    n=s.size();
    s=" "+s;
    memset(dp,0x3f,sizeof(dp));
    dp[0][0]=0;
    dp[0][1]=z;
    for(int i=1;i<=n;i++){
        for(int j=0;j<2;j++){
            int cost;
            if(s[i]=='A'){
                cost=(j==0?y:x); 
            }else{
                cost=(j==0?x:y);
            }
            dp[i][j]=min(dp[i][j],dp[i-1][j]+cost);
            dp[i][j^1]=min(dp[i][j^1],dp[i-1][j]+z+cost); 
        }
    }
    cout<<min(dp[n][1],dp[n][0]);
    return 0;
}

第六题

这题让我们求最长上升子序列,且这段序列是连续的自然数,这题依然可以用dp

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
int n;
int a[N];
map<int,int>dp;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int max_len=0,ans=0;
    for(int i=1;i<=n;i++){
        int x=a[i];
        if(dp.count(x-1)){
            dp[x]=max(dp[x],dp[x-1]+1);
        }else{
            dp[x]=max(dp[x],1ll);
        }
        if(max_len<dp[x]){
            max_len=dp[x];
            ans=x;
        }
    }
    cout<<max_len<<'\n';
    int s=ans-max_len+1;
    for(int i=1;i<=n&&s<=ans;i++){
        if(a[i]==s){
            cout<<i<<" ";
            s++;
        }
    } 
    return 0;
}