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. [转]推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler

    在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自 ...

  2. 从程序员到CTO的Java技术路线图(我爱分享)

    在技术方面无论我们怎么学习,总感觉需要提升自已不知道自己处于什么水平了.但如果有清晰的指示图供参考还是非常不错的,这样我们清楚的知道我们大概处于那个阶段和水平. Java程序员 高级特性 反射.泛型. ...

  3. Quartz2之入门示例【转】

    原文地址:http://liuzidong.iteye.com/blog/1118992 环境:XP+Myeclipse6.5+JDK1.6 quartz官网:http://www.quartz-sc ...

  4. vs2010 2013 2015+ 必备插件精选(15个)

    转 http://www.spersky.com/post/vsPlugins.html 我目前主要用的是Hide Main Page——公司配给的电脑屏幕分辨率好小,还是1366*768的,去掉头可 ...

  5. JQuery导航选择特效

    一.实现效果 1.初始化效果:未添加样式和特效 2.添加CSS样式 3.最终效果 二.JQuery代码 <!--编写JQuery代码--> <script type="te ...

  6. Mac上安装django

    参考:https://docs.djangoproject.com/en/1.9/topics/install/#installing-official-release 升级pip sudo pip ...

  7. css控制段落

    <p></p>标签:一段话或者段落适宜于用p标签: 段落缩进:text-indent:50px: 文字方向:text-align:center居中.left往左显示.right ...

  8. WP8.1 C#代码 添加/获取Grid.ColumnDefinitions/RowDefinitions

    WP8.1: ColumnDefinitions和RowDefinitions的道理是相同的,语法顺序是一样的,只不过是将ColumnDefinitions换成RowDefinitions而已 获取并 ...

  9. mysql和CSV

    1.mysql导入和导出数据可以通过mysql命令或者mysqldump来完成.mysqldump可以导入和导出完整的表结构和数据.mysql命令可以导入和导出csv文件. 1.mysql支持导入和导 ...

  10. 一个js获取数组下标的函数

    或许,面试的时候,你可能会被问到怎么模拟字符串的indexOf()函数获取数组的下标,这里是个人实现的一个小函数,里面包含的知识点挺多的,假如是新手,看不懂的地方可以百度,这里就不做详细介绍了,当然, ...