HDOJ 1858

Max Partial Value I

Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 681    Accepted Submission(s): 272


Problem 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.
 

Author
HenryFour@TJU
 

Source
 

Recommend
lcy
 
经典的DP,注意用long long
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1|1
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)

#define mk make_pair
const int inf = 1 << 30;
const int MAXN = 1000000 + 150;
const int maxw = 100 + 20;
const int MAXNNODE = 1000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 20071027
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
const D e = 2.718281828459;
LL a[MAXN];
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    int T , n ;

    cin >> T;
    while(T--)
    {
        scanf("%d" , &n);
        FOR(i , 0 , n)
        {
            scanf("%I64d" , &a[i]);
        }
        int nowstart  , nowend , start , end;
        nowstart = nowend = end = start = 1;
        LL max_num = a[0];
        FOR(i , 1 , n)
        {
            if(a[i - 1] + a[i] >= a[i])
            {
                a[i] = a[i - 1] + a[i];
                nowend = i + 1;
            }
            else
            {
                nowstart = i + 1;
                nowend = i + 1;
            }
            if(max_num < a[i])
            {
                max_num = a[i];
                start = nowstart;
                end = nowend;
            }
        }
        printf("%I64d %d %d\n" , max_num , start , end);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值