Submission #285428

#TimeUsernameProblemLanguageResultExecution timeMemory
285428loideptrai1K blocks (IZhO14_blocks)C++14
100 / 100
305 ms42312 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int,int> #define pll pair<ll,ll> #define name "" #define inout freopen(name".inp","r", stdin); freopen(name".out","w",stdout); using namespace std; const int N=1e5+5; const int MOD=1e9+7; const int base=257; const int M=1e6+1; const int INFI=1e9; int a[N], dp[N][105]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //inout; //precomputecombinatorics(); //precomputehashing(); int n,k; 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]=INFI; } } dp[0][0]=0; for (int j=1; j<=k; j++){ deque <pii> d; d.clear(); for (int i=j; i<=n; i++){ int minv=dp[i-1][j-1]; while (d.size() && a[d.back().first]<=a[i]){ minv=min(minv,d.back().second); d.pop_back(); } int pos=0; if (d.size()){ pos=d.back().first; } dp[i][j]=min(dp[pos][j],minv+a[i]); //cout << i << " " << j << " " << dp[pos][j] << "\n"; d.push_back(pii(i,minv)); } } cout << dp[n][k]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...