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

#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  阅读(520)  评论(0编辑  收藏  举报