結果

問題 No.1536 仕切り直し
ユーザー logxlogx
提出日時 2021-03-23 21:33:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 3,096 bytes
コンパイル時間 4,182 ms
コンパイル使用メモリ 264,276 KB
実行使用メモリ 25,496 KB
最終ジャッジ日時 2023-08-16 16:55:27
合計ジャッジ時間 5,174 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 10 ms
9,836 KB
testcase_05 AC 18 ms
18,140 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 8 ms
9,000 KB
testcase_09 AC 10 ms
10,972 KB
testcase_10 AC 27 ms
25,496 KB
testcase_11 AC 6 ms
6,520 KB
testcase_12 AC 18 ms
18,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/extc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)

/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
    constexpr operator int(){return -int(1e9)-10;}
    constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
    constexpr operator int(){return int(1e9)+10;}
    constexpr operator ll(){return ll(1e18)+10;}
    constexpr auto operator -(){return neginf;}
}inf;

/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}



int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.precision(10);
/*------------------------------------*/
    
    int n,m;cin >> n >> m;
    vector<ll> a(n);
    rep(i,n)cin >> a[i];
    //dp[i][j]=i番目までにj枚使った時のmax.
    //これを復元すればいい.(なお, 一箇所に2枚以上入れるなら一番後ろに回せる, 即ち一箇所にたかだか1枚しか入れないとしてよいことに気をつける.)
    vector<vector<ll>> dp(n,vector<ll>(m+1,-inf));
    rep(j,2){
        dp[0][j]=a[0]*(j ? -1:1);
    }
    rep(i,n-1)rep(j,m+1){
        chmax(dp[i+1][j], dp[i][j]+a[i+1]*(j&1 ? -1:1));
        if(j<m)chmax(dp[i+1][j+1], dp[i][j]+a[i+1]*(j&1 ? 1:-1));
    }
    
    ll val=-inf;
    ll idx=-1;
    rep(j,m+1){
        if(chmax(val, dp.back()[j]))idx=j;
    }
    
    //復元.
    vector<int> ans;
    for(int i=n-2;i>=0;i--){
        if(dp[i+1][idx] == dp[i][idx]+a[i+1]*(idx&1 ? -1:1)){}
        else{
            idx--;
            ans.emplace_back(i+1);
        }
    }
    if(idx)ans.emplace_back(0);
    reverse(ALL(ans));
    for(int x:ans)cout << x << newl;
    rep(i,m-ans.size()){
        cout << n << newl;
    }
}
0