# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
231173 | infinite_iq | K blocks (IZhO14_blocks) | C++14 | 15 ms | 8960 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 547
//#define mp make_pair
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll inf=1e18;
const ll mod=1e9+7;
const ld pai=acos(-1);
ll n , k ;
ll a[100009] ;
ll dp [100009][109] ;
int main () {
scanf("%lld%lld",&n,&k) ;
for ( int i = 1 ; i <= n ; i ++ ) scanf("%lld",&a[i]) ;
for ( int i = 1 ; i <= n ; i ++ ) {
dp [i][1] = max ( dp [i-1][1] , a[i] ) ;
}
dp [0][1] = inf ;
for ( int i = 0 ; i <= n ; i ++ ) {
for ( int j = 2 ; j <= k ; j ++ ) {
dp [i][j] = inf ;
}
}
for ( int j = 2 ; j <= k ; j ++ ) {
stack < pll > st ;
for ( int i = 1 ; i < j ; i ++ ) st .push ( { i , dp [i][j-1] } ) ;
for ( int i = j ; i <= n ; i ++ ) {
ll val1 = inf , val2 = inf ;
while ( st . size () ) {
ll id = st.top ().fi , crnt = st.top().se ;
val1 = min ( val1 , crnt ) ;
if ( a [id] > a[i] ) break ;
st .pop () ;
}
if ( st . size () ) val2 = dp [st.top().fi][j] ;
st .push ( { i , min ( val1 , dp[i][j-1] ) } ) ;
val1 += a[i] ;
dp [i][j] = min ( val1 , val2 ) ;
}
}
cout << dp[n][k] << endl ;
}
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... |