Submission #386460

#TimeUsernameProblemLanguageResultExecution timeMemory
386460cpp219K blocks (IZhO14_blocks)C++14
100 / 100
264 ms41476 KiB
#pragma GCC optimization "Ofast"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 1e5 + 6;
const ll Log2 = 19;
const ll inf = 1e16 + 7;
typedef pair<ll,ll> LL;

ll n,k,a[N],dp[N][101],mn[N],MaxSeg[N],L[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define task "tst"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        //freopen(task".out", "w", stdout);
    }
    cin>>n>>k;
    for (ll i = 1;i <= n;i++) cin>>a[i];
    for (ll i = 0;i <= n;i++)
        for (ll j = 0;j <= k;j++) dp[i][j] = inf;
    ll curmax = 0;
    for (ll i = 1;i <= n;i++) curmax = max(curmax,a[i]),dp[i][1] = curmax;
    for (ll j = 2;j <= k;j++){
        stack<LL> st; /// first = minval    second = index
        for (ll i = j;i <= n;i++){
            ll now = dp[i - 1][j - 1];
            while(!st.empty() && a[st.top().sc] <= a[i]) now = min(now,st.top().fs),st.pop();
            dp[i][j] = now + a[i];
            if (st.size()) dp[i][j] = min(dp[i][j],dp[st.top().sc][j]);
            st.push({now,i});
        }
    }
    cout<<dp[n][k];
}

Compilation message (stderr)

blocks.cpp:1: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    1 | #pragma GCC optimization "Ofast"
      | 
blocks.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
blocks.cpp:13:21: warning: overflow in conversion from 'double' to 'int' changes value from '1.0000000000000008e+16' to '2147483647' [-Woverflow]
   13 | const ll inf = 1e16 + 7;
      |                ~~~~~^~~
blocks.cpp: In function 'int main()':
blocks.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   23 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...