Description

给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,

例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和

为20。

在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该

子序列的第一个和最后一个元素。
 

Input

测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
 

Output

对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元

素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
 

Sample Input

6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
 

Sample Output

20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0

Hint

Hint  Huge input, scanf is recommended.

直接遍历然后不断更新下标和最大值~

//Asimple
//#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
#define INF 0xfffffff
#define mod 1000000
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
#define abs(x) x<0?-x:x
#define srd(a) scanf("%d", &a)
#define src(a) scanf("%c", &a)
#define srs(a) scanf("%s", a)
#define srdd(a,b) scanf("%d %d",&a, &b)
#define srddd(a,b,c) scanf("%d %d %d",&a, &b, &c)
#define prd(a) printf("%d\n", a)
#define prdd(a,b) printf("%d %d\n",a, b)
#define prs(a) printf("%s\n", a)
#define prc(a) printf("%c", a)
using namespace std;
typedef long long ll;
const int maxn = ;
int n, m, num, T, k, len, ans, sum;
int edx, edy, stx, sty;
int dp[];
int a[maxn];void input() {
while( ~srd(n) && n ) {
sum = ;
for(int i=; i<n; i++) {
srd(a[i]);
}
int inds = ;
int inde = , t = ;
int Maxsum = -INF;
int sum = ;
for(int i=; i<n; i++) {
if( sum < ) {
sum = a[i];
t = i;
} else sum += a[i];
if( sum > Maxsum ) {
Maxsum = sum;
inds = t;
inde = i;
}
}
if( Maxsum < ) {
printf("0 %d %d\n", a[], a[n-]);
} else {
printf("%d %d %d\n", Maxsum, a[inds], a[inde]);
}
}
} int main(){
input();
return ;
}

加一个一模一样的题~~就只是不会出现全为负的情况~

http://acm.hdu.edu.cn/showproblem.php?pid=1003

//Asimple
//#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
#define INF 0xfffffff
#define mod 1000000
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
#define abs(x) x<0?-x:x
#define srd(a) scanf("%d", &a)
#define src(a) scanf("%c", &a)
#define srs(a) scanf("%s", a)
#define srdd(a,b) scanf("%d %d",&a, &b)
#define srddd(a,b,c) scanf("%d %d %d",&a, &b, &c)
#define prd(a) printf("%d\n", a)
#define prdd(a,b) printf("%d %d\n",a, b)
#define prs(a) printf("%s\n", a)
#define prc(a) printf("%c", a)
using namespace std;
typedef long long ll;
const int maxn = ;
int n, m, num, T, k, len, ans, sum;
int edx, edy, stx, sty;
int a[maxn]; void input() {
srd(T);
for(int k=; k<=T; k++) {
srd(n);
for(int i=; i<=n; i++) {
srd(a[i]);
}
int inds = ;
int inde = , t = ;
int Maxsum = -INF;
int sum = ;
for(int i=; i<=n; i++) {
if( sum < ) {
sum = a[i];
t = i;
} else sum += a[i];
if( sum > Maxsum ) {
Maxsum = sum;
inds = t;
inde = i;
}
}
printf("Case %d:\n", k);
printf("%d %d %d\n", Maxsum, inds, inde);
if( k < T ) {
printf("\n");
}
}
} int main(){
input();
return ;
}

DP专题训练之HDU 1231 最大连续子序列的更多相关文章

  1. HDU 1231 最大连续子序列 --- 入门DP

    HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...

  2. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. DP专题训练之HDU 2955 Robberies

    打算专题训练下DP,做一道帖一道吧~~现在的代码风格完全变了~~大概是懒了.所以.将就着看吧~哈哈 Description The aspiring Roy the Robber has seen a ...

  4. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  5. HDU 1231 最大连续子序列:水dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 题意: 给你一个整数序列,求连续子序列元素之和最大,并输出该序列的首尾元素(若不唯一,输出首坐标 ...

  6. HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. HDU 1231——最大连续子序列(DP)

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. hdu 1003 hdu 1231 最大连续子序列【dp】

    HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...

  9. HDU 1231 最大连续子序列 (dp)

    题目链接 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,  Nj },其中 1 <= ...

随机推荐

  1. java.util.zip获取Zip文件条目InputStream

    package com.test; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import ja ...

  2. linux建立文件夹软连接

    linux建立文件夹软连接,并强制覆盖 ln -sfn /home/var/log/httpd/logs logs 这将在当前目录下建立logs软连接,指向/home/var/log/httpd/lo ...

  3. 用css实现网站切角效果 使用css3属性:渐变

     都是大量的练习,老师练习乒乓球花了大量时间,十万次一个动作的重复,高中班主任说过,世上没有天才,只是重复的次数多了,自然被认作了天才,小小班的学生之所以厉害是因为他们重复一个知识点次数多,所以没有一 ...

  4. golang map getkeys

    golang 获取map的keys package main import "fmt" import "reflect" func main() { abc : ...

  5. Oracle实战训练——ATM取款机业务

    ATM取款机的数据库模拟开发和实战总结 一.ATM实战开发的简介. 学习了几天的Oracle,开始着手用数据库PL/SQL语言做一个简单的ATM取款机业务,主要是为了巩固数据库的知识,并非真正的去实现 ...

  6. 【转】Linux下apache/httpd服务启动与停止

    apache服务,或者说httpd服务,如何启动,如何开机启动. 转来转去,找不到原文.. 操作系统环境:红帽5,具体如下:# uname -a Linux machine1 2.6.18-164.e ...

  7. 流量三角形:并非简单的"统计学"

    又忙了一周多,今天过来再整理一些东西.国内做产险精算的,准备金的居多,从精算部落中的帖子的跟帖情况可见一斑.既然准备金更容易受到大家的关注,今天再整理一个关于准备金的个人看法,给精算部落收敛一下人气, ...

  8. option(recompile)

    Ref: http://www.cnblogs.com/CareySon/archive/2013/05/04/PlanCacheInSQLServerPart2.html https://msdn. ...

  9. 箭头函数 Arrow Functions/////////////////////zzz

    箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教程曾建议在HTML注释内包裹行内脚本,这样可以避免不支持JS的浏览器误将JS代码显示为文本.你会写这样的代码: < ...

  10. 我的SpringMVC配置

    记住所有导的包都在org.springframework.web.servlet.mvc.annotation.下而不是 org.springframework.web.protlet.mvc.ann ...