HDU 1024 Max Sum Plus Plus
最大m段子段和,滚动数组
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 const int N = 1010000; 9 const int INF = 2147483640; 10 int n, m, a[N], dp[N], bes[N]; 11 12 int main() 13 { 14 while (~scanf("%d%d", &m, &n)) 15 { 16 int ans = -INF; 17 for (int i = 1; i <= n; ++i) 18 scanf("%d", &a[i]); 19 for (int i = 1; i <= n; ++i) 20 for (int j = 1; j <= m; ++j) 21 dp[j] = -INF, bes[j] = -INF; 22 for (int i = 1; i <= n; ++i) 23 { 24 for (int j = 1; j <= m && j <= i; ++j) 25 dp[j] = max(dp[j], bes[j - 1]) + a[i]; 26 for (int j = 1; j <= m && j <= i; ++j) 27 bes[j] = max(bes[j], dp[j]); 28 ans = max(ans, dp[m]); 29 } 30 printf("%d\n", ans); 31 } 32 return 0; 33 }