# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
32219 | chonka | K blocks (IZhO14_blocks) | C++98 | 693 ms | 87832 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.
#include<iostream>
#include<stdio.h>
#include<stack>
#include<cstring>
using namespace std ;
#define inf 1000000007
#define MAXN 100007
#define MAXK 107
int n , k ;
int a[ MAXN ] ;
long long dp[ MAXN ][ MAXK ] ;
long long vals[ MAXN ] ;
long long pref[ MAXN ] ;
void input ( ) {
scanf ( "%d%d" , &n , &k ) ;
int i ;
for ( i = 1 ; i <= n ; i ++ ) {
scanf ( "%d" , &a[ i ] ) ;
}
}
void solve ( ) {
stack < pair < int , int > > s ;
int sz ;
int i , j ;
for ( i = 0 ; i <= n ; i ++ ) {
for ( j = 0 ; j <= k ; j ++ ) {
dp[ i ][ j ] = inf ;
}
}
dp[ 0 ][ 0 ] = 0 ;
for ( j = 1 ; j <= k ; j ++ ) {
while ( s.empty ( ) == false ) { s.pop ( ) ; }
s.push ( make_pair ( inf , inf ) ) ;
for ( i = 1 ; i <= n ; i ++ ) {
long long aux = dp[ i - 1 ][ j - 1 ] ;
while ( s.empty ( ) == false && s.top( ).first <= a[ i ] ) {
aux = min ( aux , 1LL * s.top().second ) ;
s.pop ( ) ;
}
dp[ i ][ j ] = min ( 1LL * s.top( ).first + s.top ( ).second , aux + a[ i ] ) ;
if ( aux + a[ i ] < s.top ( ).first + s.top ( ).second ) {
s.push ( make_pair ( a[ i ] , aux ) ) ;
}
}
}
printf ( "%lld\n" , dp[ n ][ k ] ) ;
}
int main ( ) {
input ( ) ;
solve ( ) ;
return 0 ;
}
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... |