怒刷DP之 HDU 1024

Max Sum Plus Plus
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. 

Given a consecutive number sequence S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (1 ≤ i ≤ j ≤ n). 

Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(im, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x is not allowed). 

But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(i x, j x)(1 ≤ x ≤ m) instead. ^_^ 
 

Input

Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n
Process to the end of file. 
 

Output

Output the maximal summation described above in one line. 
 

Sample Input

1 3 1 2 3 2 6 -1 4 -2 3 -2 3
 

Sample Output

6 8
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using    namespace    std;
 6 
 7 const    int    SIZE = 1000006;
 8 const    int    INF = 0x7ffffff0;
 9 long long     DP[SIZE][2];
10 int        S[SIZE];
11 
12 long long    max(long long a,long long b);
13 int    main(void)
14 {
15     int        n,m,i_0,i_1;
16     long long    box,ans,temp;
17 
18     while(scanf("%d%d",&m,&n) != EOF)
19     {
20         memset(DP,0,sizeof(DP));
21         ans = -INF;
22         i_0 = 0;
23         i_1 = 1;
24 
25         for(int i = 1;i <= n;i ++)
26             scanf("%d",&S[i]);
27         for(int j = 1;j <= m;j ++)
28         {
29             for(int i = 1;i <= n;i ++)
30             {
31                 DP[i][i_1] = i - 1 < j ? -INF : DP[i - 1][i_1];
32                 if(i == 1)
33                 {
34                     temp = -INF;
35                     for(int k = j - 1;k < i;k ++)
36                         temp = max(temp,DP[k][i_0]);
37                 }
38                 else
39                     temp = temp > DP[i - 1][i_0] ? temp : DP[i - 1][i_0];
40                 DP[i][i_1] = (temp > DP[i][i_1] ? temp : DP[i][i_1]) + S[i];
41                 if(j == m)
42                     ans = ans > DP[i][i_1] ? ans : DP[i][i_1];
43             }
44             swap(i_0,i_1);
45         }
46         printf("%lld\n",ans);
47     }
48 }
49 
50 long long    max(long long a,long long b)
51 {
52     return    a > b ? a : b;
53 }

 

posted @ 2015-09-07 17:15  Decouple  阅读(216)  评论(0编辑  收藏  举报