# [BZOJ1150]-[CTSC2007]数据备份Backup-dp凸优化

me怕是废了

## 题目

BZOJ1150传送门

### 下面是代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

int N , K , a[100005] , MX ;
struct Data{
long long fee ;
int k ;
Data( const long long _ = 0 , const int __ = 0 ):fee(_) , k(__){} ;
bool operator < ( const Data &A ) const {
return fee < A.fee || ( fee == A.fee && k < A.k ) ;
}
} f[100005] ;

Data check( long long delta ){
Data best = Data( 0 , 0 ) ;
for( int i = 2 ; i <= N ; i ++ ){
f[i] = Data( 0 , 0 ) ;
if( delta + a[i] < 0 )
f[i] = Data( best.fee + a[i] + delta , best.k + 1 ) ;
best = min( best , f[i-1] ) ;
f[i] = min( f[i] , best ) ;
} return f[N] ;
}

void solve(){
long long lf = -MX , rg = 0 , ans = 0 ;
while( lf <= rg ){
long long mid = ( lf + rg ) / 2 ;
Data res = check( mid ) ;
if( res.k == K )
printf( "%lld\n" , res.fee - 1ll * K * mid ) , exit( 0 ) ;
if( res.k > K ) lf = mid + 1 ;
else rg = mid - 1 , ans = mid ;
} Data res = check( ans ) ;
printf( "%lld\n" , res.fee - 1ll * K * ans ) ;
}

int main(){
scanf( "%d%d" , &N , &K ) ;
for( int i = 1 ; i <= N ; i ++ ) scanf( "%d" , &a[i] ) ;
MX = a[N] ;
for( int i = N ; i ; i -- ) a[i] -= a[i-1] ;
solve() ;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120