题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024
分析:dp,对于第i个数有两种情况,一个是在前一个数所在的组;一个是单独开一组,那么前一个数就不一定需要,此时要找1~(i -1)中最大
1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<string> 6 #include<cstring> 7 #include<algorithm> 8 #include<functional> 9 #include<iomanip> 10 #include<numeric> 11 #include<cmath> 12 #include<queue> 13 #include<vector> 14 #include<set> 15 #include<cctype> 16 #define PI acos(-1.0) 17 const int INF = 0x3f3f3f3f; 18 const int NINF = -INF - 1; 19 const int maxn = 1e6 + 5; 20 typedef long long ll; 21 using namespace std; 22 int m, n; 23 int arr[maxn], dp[maxn], Max[maxn]; 24 int main() 25 { 26 while (scanf("%d%d", &m, &n) != EOF) 27 { 28 int mmax; 29 for (int i = 1; i <= n; ++i) 30 scanf("%d", &arr[i]); 31 memset(dp, 0, sizeof(dp)); 32 memset(Max, 0, sizeof(Max)); 33 for (int i = 1; i <= m; ++i) 34 { 35 mmax = -INF; 36 for (int j = i; j <= n; ++j) 37 { 38 dp[j] = max(dp[j - 1] + arr[j], Max[j - 1] + arr[j]); 39 Max[j - 1] = mmax; 40 mmax = max(mmax, dp[j]); 41 } 42 } 43 printf("%d ", mmax); 44 } 45 return 0; 46 }