結果

問題 No.1247 ブロック登り
ユーザー mugen_1337
提出日時 2020-10-03 01:49:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 526 ms / 3,000 ms
コード長 2,451 bytes
コンパイル時間 2,065 ms
コンパイル使用メモリ 199,408 KB
最終ジャッジ日時 2025-01-15 01:38:37
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}


ll dp[310][310][310][2];
/*
dp[i][l][r][t]:
    i回移動した,[l,r)決定済み,
    t = 0 => 今,左端にいます
    t = 1 =>     右端 (r-1)にいます
*/

signed main(){
    rep(i,310)rep(j,310)rep(k,310)rep(t,2)dp[i][j][k][t]=-LINF;
    rep(i,310) dp[0][i][i][0]=0,dp[0][i][i][1]=0;
    

    int n,k;cin>>n>>k;
    vector<ll> a(n);
    cin>>a;

    if(k==1){
        rep(i,n){
            ll res=-LINF;
            if(i) chmax(res,a[i-1]);
            if(i<n-1) chmax(res,a[i+1]);
            cout<<res<<endl;
        }
        return 0;
    }

    rep(i,k+1)rep(l,n+1)for(int r=l;r<=n;r++)rep(t,2){
        int w=r-l;// width
        ll mul=k-i;
        // stay
        if(i+2<=k+1 and w>1) chmax(dp[i+2][l][r][t],dp[i][l][r][t]);
        // left to right
        if(t==0 and i+w-1<=k+1 and w>0) chmax(dp[i+w-1][l][r][1],dp[i][l][r][t]);
        // right to left
        if(t==1 and i+w-1<=k+1 and w>0) chmax(dp[i+w-1][l][r][0],dp[i][l][r][t]);
        // left -> left-1
        if(t==0 and l>0) chmax(dp[i+1][l-1][r][t],dp[i][l][r][t]+a[l-1]*mul);
        // right -> right+1
        if(t==1 and r<n) chmax(dp[i+1][l][r+1][t],dp[i][l][r][t]+a[r]*mul);
    }

    

    vector<ll> res(n,-LINF);
    rep(i,k+2)rep(l,n+1)for(int r=l;r<=n;r++){
        // 左端にいます
        int rem=k+1-i;
        if(l+rem<r)    chmax(res[l+rem],dp[i][l][r][0]);
        if(l<=r-1-rem) chmax(res[r-1-rem],dp[i][l][r][1]);
    }
    for(auto &x:res) cout<<x<<endl;
    return 0;
}
0