HDU-1858 Max Partial Value I
Time Limit: 5000 ms / Memory Limit: 65535 kb
Description
HenryFour has a number of stones which have different values from -4444 to 4444. He puts N stones in a line and wants to find the max partial value of these N stones.
Assume the values of the N stones in line are: v1, v2, v3, v4, …, vN. The partial vaule of stones from Lth stone to Rth stone (1 ≤ L ≤ R ≤ N) is the sum of all the stones between them. i.e. PartialV(L, R) = v[L] + v[L+1] + …. + v[R] (1 ≤ L ≤ R ≤ N)
Since the number of stones (N) is very very large, it is quite difficult for HenryFour to find the max partial value. So could you develop a programme to find out the answer for him?
Input
There are several test cases in the input data. The first line contains a positive integer T (1 ≤ T ≤ 14), specifying the number ot test cases. Then there are T lines. Each of these T lines contains a positive number N followed by N integers which indicate the values of the N stones in line.
1 ≤ N ≤ 1,000,000
-4444 ≤ v[i] ≤ 4444
Output
Your program is to write to standard output. For each test case, print one line with three numbers seperated by one blank: P L R. P is the max partial value of the N stones in line. L and R indicate the position of the partial stones. If there are several Ls and Rs that have the same value PartialV(Li, Ri) = P, please output the minimum pair. For pair (Li, Ri) and (Lj, Rj), we define (Li, Ri) < (Lj, Rj) if and only if: Li < Lj or (Li == Lj and Ri < Rj)
Sample Input
3
4 32 -39 -30 -28
8 1 2 3 -10 1 -1 5 1
10 14 -12 -8 -13 3 5 42 -24 -32 -12
Sample Output
32 1 1
6 1 3
50 5 7
Hint
Huge input and output,scanf and printf are recommended.
Source
HDU 2007 Programming Contest - Final
求连续子序列的最大和
连简单的入门dp都不会
学习下别人的解法,理解起来倒还是很容易的。
用一个pos数组存储当前dp值是从哪个地方开始加的,一直加到这
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
LL a[N];
LL dp[N];
int pos[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i = 1;i <= n;++i)
{
scanf("%lld",&a[i]);
pos[i] = i;
dp[i] = a[i];
}
for(int i = 2;i <= n;++i)
{
if(a[i] <= dp[i - 1] + a[i]){
dp[i] = dp[i - 1] + a[i];
pos[i] = pos[i - 1];
}
}
LL MAX = -inf;
int p;
for(int i = 1;i <= n;++i)
{
if(MAX < dp[i]){
MAX = dp[i];
p = i;
}
}
printf("%lld %d %d\n",MAX,pos[p],p);
}
return 0;
}