HDU1003 最大连续子序列
2024-09-05 02:47:51
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 324393 Accepted Submission(s): 77146
Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
Sample Output
Case 1:14 1 4Case 2:7 1 6
【题意】
给定一个序列a[1],a[2],a[3],.....,a[n],来计算子序列的最大和。例如,给定序列(6,-1,5,4,-7),这个序列中的最大和是 6 +(-1)+ 5 + 4 = 14。
输入的第一行包含一个整数T(1<=T<=20),它表示测试用例的数量。然后是T行,每一行以N开头(1<=N<=100000),然后是N个整数(所有整数都在-1000到1000之间)
对于每个测试用例,输出两行。第一行是“Case #:”,#表示测试用例的数量。第二行包含三个整数,序列中的最大和,子序列的起始位置,子序列的结束位置。如果有多个结果,输出第一个。在两种情况之间输出空行。
【代码】
#include <stdio.h>
#include <stdlib.h>
int main(){
int i,j,n,t;
scanf("%d",&t);
for(i=;i<=t;i++){
int *a,first=,last=,sum=,tmp=,max=-;
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(j=;j<n;j++){
scanf("%d",&a[j]);
sum += a[j];
if(sum>max){
max=sum;
first=tmp;
last=j+;
}
if(sum<){
sum=;
tmp=j+;
}
}
printf("Case %d:\n%d %d %d\n",i,max,first,last);
if(i!=t)printf("\n");
a=NULL;
}
return ;
}
最新文章
- JavaScript模板引擎artTemplate.js——template()方法
- VmWare为Fedora虚拟机扩展磁盘
- SQLite数据库的基本操作
- js typeof
- 31. Flatten Binary Tree to Linked List
- iOS 使用两个tableview的瀑布流
- ios7 导航栏 手势 右划 自动返回 相关
- spring注解使用
- 怎样在Android SDK 下查看应用程序输出日志的方法
- 查看源码Vim+Cscope
- Altium Designer 09 (Protel)总线使用方法(解决导入PCB无网络标号问题)
- WinXP 无线提示“区域中找不到无线网络”的一种可能原因!
- UVa 483 - Word Scramble
- js原生之设计模式开篇介绍
- Intent的属性及Intent-filter配置——实例Action、Data属性启动系统Activity
- DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate Date Dimension)
- 检查硬件变化的命令kudzu
- nova boot from volume代码分析
- MySQL学习笔记(一)Ubuntu16.04中MySQL安装配置(5.6优化、错误日志、DNS解决)
- flutter插件汇总2
热门文章
- spring lookup method 注入
- Autodesk Maya 2019 安装
- rabbitmq 配置多个消费者(转载)
- BZOJ 3812 主旋律 (状压DP+容斥) + NOIP模拟赛 巨神兵(obelisk)(状压DP)
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things
- django post请求 403错误解决方法
- struts1 action之间的跳转
- Comet OJ - Contest #3 (A 比赛 加强版)二分答案
- LibreOJ #113. 最大异或和
- svn 外部引用别的项目文件