标签:
5 8 2 7 3 1 1 100
Case 1: 3 Case 2: 0
#include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 5050; int a[maxn], sum[maxn], dp[maxn], h[maxn]; int main() { int n, cnt = 0; while ( scanf ( "%d", &n ) != EOF ) { memset ( sum, 0, sizeof ( sum ) ); for ( int i = 1; i <= n; i++ ) { scanf ( "%d", &a[i] ); h[i] = max ( h[i - 1], a[i] ); sum[i] = sum[i - 1] + a[i]; } memset ( dp, INF, sizeof ( dp ) ); dp[0] = 0; //当前0或1摞书非降序时需要最少装订次数为0 dp[1] = 0; for ( int i = 2; i <= n; i++ ) { for ( int j = i - 1; j >= 0; j-- ) { if ( h[j] <= sum[i] - sum[j] ) {//当前j个中最大高度大于等于从j到i的总书高,进行状态转移 if ( dp[i] > dp[j] + i - j - 1 ) { dp[i] = dp[j] + i - j - 1; //更新dp h[i] = sum[i] - sum[j]; //更新前i摞书中最高的书高高度 break; } } } } printf ("*Case %d: %d\n",++cnt, dp[n] ); } return 0; } /* 6 5 5 2 3 5 5 */
nyoj 1216——整理图书 CF 229D—— Towers——————【dp+贪心】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4690614.html