Submission #956192

#TimeUsernameProblemLanguageResultExecution timeMemory
956192wibulordK blocks (IZhO14_blocks)C++14
100 / 100
94 ms3152 KiB
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define fi first
#define se second
#define pb emplace_back
#define pii pair<int, int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x >> (i)) & 1)
#define sz(s) (int)((s).size())
#define all(a) a.begin(), a.end()
#define uni(a) (sort(all(a)), a.resize(unique(all(a))-a.begin()))
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i)
#define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--)
using namespace std;

template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;}
template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;}

const int MOD = (int)1e9 + 7;
const int mod = 998244353;
const int N = 1e5 + 1, M = 2; 

/*
     /\_/\
    (= ._.)
    / >?  \>$
*/
int n, s;
int a[N], l[N], mn[N], dp[N][M];

void sol(void){
    cin >> n >> s;
    FOR(i, 1, n) cin >> a[i]; 
    FOR(i, 1, n) for(l[i] = i-1; l[i] > 0 && a[l[i]] <= a[i];) l[i] = l[l[i]]; 
    memset(dp, 0x3f, sizeof(dp)); dp[1][1] = a[1];
    FOR(i, 2, n) dp[i][1] = max(dp[i-1][1], a[i]);  
    FOR(k, 2, s){  
        mn[k-1] = dp[0][0];
        FOR(i, 1, n) dp[i][k&1] = dp[0][0];
        FOR(i, k, n){
            mn[i] = dp[i-1][(k-1)&1];
            for(int j = i-1; j > l[i]; j = l[j]) mn[i] = min(mn[i], mn[j]);
            dp[i][k&1] = min(dp[l[i]][k&1], mn[i] + a[i]);
        }
    }
    cout << dp[n][s&1] << '\n';
}

signed main(void){
    #define TASK "nhap"
    if(fopen(TASK".inp", "r")){
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int t = 1;
    // cin >> t;
    while(t--) sol();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...