# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1124121 | luvna | K blocks (IZhO14_blocks) | C++20 | 256 ms | 40660 KiB |
#include<bits/stdc++.h>
#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"
using namespace std;
template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
const int N = 1e5 + 15;
const int INF = 1e9 + 7;
int n, k;
int a[N];
int dp[N][101];
bool past = 0, succ = 1;
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0;i <= n; i++){
for(int j = 0; j <= k; j++) dp[i][j] = INF;
}
dp[0][0] = 0;
for(int j = 1; j <= k; j++){
stack<pii> st;
for(int i = 1; i <= n; i++){
int min_dp = dp[i-1][j-1];
while(!st.empty() && a[st.top().fi] <= a[i]){
minimize(min_dp, st.top().se);
st.pop();
}
minimize(dp[i][j], min_dp + a[i]);
if(st.empty()) minimize(dp[i][j], dp[0][j]);
else minimize(dp[i][j], dp[st.top().fi][j]);
st.push({i,min_dp});
}
}
cout << dp[n][k] << endl;
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |