1024

1 /*Description: to
2 *
3 * Author: Vincent
4 *
5 * Date:2010/04/
6 * Contact:agilely@126.com
7 * Zju CS Lab
8
9 Problem Description
10 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.
11
12 Given a consecutive number sequence S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (1 ≤ i ≤ j ≤ n).
13
14 Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx is not allowed).
15
16 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(ix, jx)(1 ≤ x ≤ m) instead. ^_^
17
18
19
20 Input
21 Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
22 Process to the end of file.
23
24
25
26 Output
27 Output the maximal summation described above in one line.
28
29
30
31 Sample Input
32 1 3 1 2 3
33 2 6 -1 4 -2 3 -2 3
34
35
36 Sample Output
37 6
38 8
39
40
41 */
42  //Accepted 1024 515MS 2248K
43 /*给出n个数字:S1, S2, S3, S4 ... Sx, ... Sn
44 定义函数sum(i, j) = Si + ... + Sj (1 ≤ i ≤ j ≤ n)
45 给你数m,找出m对i、j使得sum(i1,j1)+sum(i2,j2)+...+sum(im, jm)最大
46 注意每对的i、j在数组上覆盖的区域不能彼此重复
47 Sample Input 输入m、n和n个数 Sample Output
48 1 3 1 2 3 就一对数,转换为最大子序列问题 6
49 2 6 -1 4 -2 3 -2 3 其中4 -2 3和3的和最大 8 */
50 #include<iostream>
51 using namespace std;
52 const int len=1000001;
53 int ls[len];
54 int no[len];
55 int num[len];//存放原输入数据
56 int ml[len];
57 int mn[len];
58 int *last,*now,*ex,*maxl,*maxn;
59 int main()
60 {
61 int i,j;
62 int m,n;
63 int maxb,ans;
64 while(scanf("%d%d",&m,&n)!=EOF)
65 {
66 last=ls;now=no;maxl=ml;maxn=mn;
67 for(i=1;i<=n;i++)
68 scanf("%d",&num[i]);
69 last[1]=num[1];
70 for(i=2;i<=n;i++)
71 {
72 if(last[i-1]>0) last[i]=last[i-1]+num[i];
73 else last[i]=num[i];
74 }
75 maxl[1]=last[1];
76 for(i=2;i<=n;i++)
77 {
78 if(last[i]>maxl[i-1])maxl[i]=last[i];
79 else maxl[i]=maxl[i-1];
80 }
81 for(i=2;i<=m;i++)
82 {
83 maxn[i]=now[i]=last[i-1]+num[i];
84 for(j=i+1;j<=n;j++)
85 {
86 maxb=maxl[j-1];
87 if(now[j-1]>maxb) now[j]=now[j-1]+num[j];
88 else now[j]=maxb+num[j];
89 if(now[j]>maxn[j-1]) maxn[j]=now[j];
90 else maxn[j]=maxn[j-1];
91 }
92 ex=last;last=now;now=ex;
93 ex=maxl;maxl=maxn;maxn=ex;
94 }
95 ans=last[m];
96 for(i=m+1;i<=n;i++)
97 if(last[i]>ans) ans=last[i];
98 printf("%d\n",ans);
99 }
100 return 0;
101 }
102
103

 

posted @ 2010-04-15 23:24  にんじゃ  阅读(24921)  评论(0)    收藏  举报