qscqesze
Published on 2017-09-02 11:31 in 暂未分类 with qscqesze

看错题系列 cf622C C2. Skyscrapers (hard version)

看错题系列 cf622C C2. Skyscrapers (hard version)

看成了 对于所有的i不能满足a[i-1]>a[i]&&a[i]<a[i+1]了,血崩

写了个dp

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
int n,m[maxn];
long long ans,dp[maxn][5][3];
pair<int,pair<int,int> >from[maxn][5][3];
int getval(int pos,int p){
    int z=pos+p;
    if(z<1)return -1;
    if(z>n)return -1;
    return min(m[z],m[pos]);
}
int getlevel(int x,int y){
    if(x<y)return 0;
    if(x==y)return 1;
    return 2;
}
int main(){
    cin>>n;
    n+=2;
    for(int i=3;i<=n;i++){
        cin>>m[i];
    }
    for(int i=3;i<=n;i++){
        for(int j=-2;j<=2;j++){
            for(int k=-2;k<=2;k++){
                for(int z=-2;z<=2;z++){
                    int a1=getval(i-2,j);
                    int a2=getval(i-1,k);
                    int a3=getval(i,z);
                    if(a1==-1||a2==-1||a3==-1||(a1>a2&&a3>a2))continue;
                    if(a2<=a1){
                        if(dp[i][z+2][getlevel(a3,a2)]<dp[i-2][j+2][0]+a3+a2){
                            from[i][z+2][getlevel(a3,a2)]={k+2,{j+2,0}};
                            dp[i][z+2][getlevel(a3,a2)]=max(dp[i][z+2][getlevel(a3,a2)],dp[i-2][j+2][0]+a3+a2);
                        }
                    }
                    if(dp[i][z+2][getlevel(a3,a2)]<dp[i-2][j+2][1]+a3+a2){
                        from[i][z+2][getlevel(a3,a2)]={k+2,{j+2,1}};
                        dp[i][z+2][getlevel(a3,a2)]=max(dp[i][z+2][getlevel(a3,a2)],dp[i-2][j+2][1]+a3+a2);
                    }
                    if(dp[i][z+2][getlevel(a3,a2)]<dp[i-2][j+2][2]+a3+a2){
                        from[i][z+2][getlevel(a3,a2)]={k+2,{j+2,2}};
                        dp[i][z+2][getlevel(a3,a2)]=max(dp[i][z+2][getlevel(a3,a2)],dp[i-2][j+2][2]+a3+a2);
                    }
                }
            }
        }
    }
    pair<int,int> now;
    for(int i=0;i<5;i++){
        for(int j=0;j<3;j++){
            if(ans<dp[n][i][j]){
                ans=dp[n][i][j];
                now={i,j};
            }
        }
    }
    vector<int>Ans;
    int idx=n;
    while(idx>=3){
        Ans.push_back(getval(idx,now.first-2));
        if(idx-1>=3){
            Ans.push_back(getval(idx-1,from[idx][now.first][now.second].first-2));
        }
        now=from[idx][now.first][now.second].second;
        idx-=2;
    }
    for(int i=Ans.size()-1;i>=0;i--){
        cout<<Ans[i]<<" ";
    }
    cout<<endl;
}
posted @ 2020-02-23 23:53  qscqesze  阅读(285)  评论(0编辑  收藏