http://ac.jobdu.com/problem.php?pid=1011
来源:互联网 发布:查士丁尼瘟疫 知乎 编辑:程序博客网 时间:2024/05/01 18:51
- 题目描述:
- 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和为20。现在增加一个要求,即还需要输出该子序列的第一个和最后一个元素。
- 输入:
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( K< 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
- 输出:
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
- 样例输入:
6-2 11 -4 13 -5 -210-10 1 2 3 4 -5 -23 3 7 -2165 -8 3 2 5 01103-1 -5 -23-1 0 -20
- 样例输出:
20 11 1310 1 410 3 510 10 100 -1 -20 0 0
- #include<iostream>
- #include<cstdio>
- #include<memory.h>
- #include<cstdlib>
- using namespace std;
- #define MAX 0XFFFFFFF;
- int a[10001];
- int main(){
- //freopen("in.txt", "r", stdin);
- int n;
- while(cin>>n, n){
- bool flag = false;
- for(int i=0;i<n;++i){
- scanf("%d", a+i);
- if(a[i]>=0)
- flag = true;
- }
- if(!flag){
- printf("%d %d %d\n", 0, a[0], a[n-1]);
- }else{
- int max = -MAX;
- int sum = 0;
- int s=0, start=0, end;
- for(int i=0;i<n;++i){
- sum += a[i];
- if(sum>max){
- max = sum;
- start = s;
- end = i;
- }
- if(sum<0){
- sum = 0;
- s = i+1;
- }
- }
- printf("%d %d %d\n", max, a[start], a[end]);
- }
- }
- //fclose(stdin);
- return 0;
- }
- http://ac.jobdu.com/problem.php?pid=1011
- http://ac.jobdu.com/problem.php?pid=1008
- http://ac.jobdu.com/problem.php?pid=1009
- http://ac.jobdu.com/problem.php?pid=1007
- http://ac.jobdu.com/problem.php?pid=1033
- http://ac.jobdu.com/problem.php?pid=1005
- http://ac.jobdu.com/problem.php?pid=1006
- http://ac.jobdu.com/problem.php?pid=1003
- http://ac.jobdu.com/problem.php?pid=1035
- http://ac.jobdu.com/problem.php?pid=1032
- http://ac.jobdu.com/problem.php?pid=1030
- http://ac.jobdu.com/problem.php?pid=1029
- http://ac.jobdu.com/problem.php?pid=1028
- http://ac.jobdu.com/problem.php?pid=1027
- http://ac.jobdu.com/problem.php?pid=1026
- http://ac.jobdu.com/problem.php?pid=1025
- http://ac.jobdu.com/problem.php?pid=1024
- http://ac.jobdu.com/problem.php?pid=1023
- http://ac.jobdu.com/problem.php?pid=1016
- http://ac.jobdu.com/problem.php?pid=1014
- Round #145(Div 2)
- http://ac.jobdu.com/problem.php?pid=1013
- http://ac.jobdu.com/problem.php?pid=1012
- http://ac.jobdu.com/problem.php?pid=1011
- http://ac.jobdu.com/problem.php?pid=1010
- 餐饮经营思考(一)——利润&成本
- Problem 1607 Greedy division from http://acm.fzu.edu.cn/problem.php?pid=1607
- Problem 1582 众数问题 from http://acm.fzu.edu.cn/problem.php?pid=1582
- Problem 1478 环形整数串 from http://acm.fzu.edu.cn/problem.php?pid=1478
- Visual CertExam Suite v3 0 1 Cracked-BLiZZARD
- ZOJ Problem Set - 3661 Palindromic Substring
- JPEG图片存储格式及原理