hdu 1858 Max Partial Value I
2015-11-26 13:40
330 查看
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
让求连续子序列的最大和,并输出相应的起始位置和结束位置;
代码:
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
让求连续子序列的最大和,并输出相应的起始位置和结束位置;
代码:
#include <iostream> using namespace std; int main() { int n,m,i,j,k,y; long long max,p1,p2,now,temp; cin>>n; while(n--) { cin>>m>>temp; now=max=temp; y=k=p1=p2=1; for(i=2;i<=m;i++) { cin>>temp; if(temp>now+temp) { now=temp;k=i;//记录起始位置 } else { now+=temp;y=i;//记录结束位置 } if(now>max) { p1=k;p2=y;max=now;//最大值放到max中,p1为起始位,p2位结束位置; } } cout<<max<<" "<<p1<<" "<<p2<<endl; } return 0; }
相关文章推荐
- hdu 1858 Max Partial Value I
- Android ActivityThread(主线程或UI线程)简介
- iOS---UIScrollView控件介绍
- Machine Learning week 6 quiz: Machine Learning System Design
- Machine Learning week 6 quiz: programming assignment-Regularized Linear Regression and Bias/Variance
- [LeetCode288]Unique Word Abbreviation
- UI基础整理-3
- UI基础整理-2
- UI基础整理-21
- GUI矩形、椭圆、线、框架
- 解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的方法
- Ultra-QuickSort(树状数组+离散化)
- ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)
- easyui--draggable 拖动
- 76 What is the effect of increasing the value of the ASM_POWER_LIMIT parameter? A. The number of DBW
- UITextView 的一些用途
- 两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?
- 从UE3到UE4
- 详解iOS的UI开发中控制器的创建方法
- ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)