C - 最大连续子序列

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description:

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.
 状态转移方程:sum = sum > 0 ? sum + a[i] : a[i] ; 第一个sum是前i个数的和,后面的两个sum是前(i-1)个是的和;如果i前面的和是小于0的,那么加上第i个数肯定比i要小,所以只取第i个数即 a[i],如果是大于0的,则就加上。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(!n)
break;
bool flag=true;
// memset(a,0,sizeof(a));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>=)
flag=false;
}
if(flag){
printf("0 %d %d\n",a[],a[n]);
continue;
}
int ans,sum,head,tail,ans_head,ans_tail;
ans=sum=a[];
ans_head=ans_tail=head=tail=;
for(int i=;i<=n;i++){
if(sum>){
sum+=a[i];
tail=i;
}
if(sum<=){
sum=a[i];
head=tail=i;
}
if(sum>ans){
ans=sum;
ans_head=head;
ans_tail =tail;
}
} printf("%d %d %d\n",ans,a[ans_head],a[ans_tail]); }
return ;
}
D - Max Sum

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description:

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

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output

Case 1: 14 1 4
Case 2: 7 1 6
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn];
int main(){
int n;
int t;
int cnt=;
scanf("%d",&t);
while(t--){
cnt++;
scanf("%d",&n);
// bool flag=true;
// memset(a,0,sizeof(a));
for(int i=;i<=n;i++){
scanf("%d",&a[i]); }
int ans,sum,head,tail,ans_head,ans_tail;
ans=sum=a[];
ans_head=ans_tail=head=tail=;
for(int i=;i<=n;i++){
if(sum>=){
sum+=a[i];
tail=i;
}
if(sum<){
sum=a[i];
head=tail=i;
}
if(sum>ans){ ans=sum;
ans_head=head;
ans_tail =tail;
}
}
printf("Case %d:\n",cnt);
printf("%d %d %d\n",ans,ans_head,ans_tail); if(t!=) printf("\n"); }
return ;
}
 

HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)的更多相关文章

  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. 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 ...

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

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

  5. DP专题训练之HDU 1231 最大连续子序列

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

  6. HDU 1231 最大连续子序列(水题)

    题目链接: 传送门 最大连续子序列 Time Limit: 1000MS     Memory Limit: 32768 K Description 给定K个整数的序列{ N1, N2, ..., N ...

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

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

  8. HDU 1231 最大连续子序列 (动态规划)

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

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

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

随机推荐

  1. 用wcf实现带有“秒传”功能的网盘

    写在前面 前面记录过这样一个关于“秒传”的实现思路,在这篇就弄了一个简单的demo实现了一下,当中有很多业务仍没考虑,只是将“秒传”的实现思路,用代码实现了一下. 关于秒传,可以参考这篇文章:何为“秒 ...

  2. javascript继承(八)-封装

    这个系列主要探讨的是javascript面向对象的编程,前面已经着重介绍了一下js的继承,下面想简单的说一下js如何实现封装的特性. 我们知道面向对象的语言实现封装是把成员变量和方法用一个类包围起来, ...

  3. Javascript基础系列之(七)函数(定义和调运函数)

    函数是一个可以随时运行的语句,简单说,函数是完成某个功能的一组语句,它接受0或者多个参数. 函数的基本语法如下 function functionName([arg0,arg1,......argN] ...

  4. 今天学习到的关于mysql数据库的linux命令

    1. 登录mysql数据库: mysql -uroot -p 2.安装会提示的mysql的数据库软件:mycli sudo apt-get install mycli 3.安装依赖包: sudo ap ...

  5. 大概了解了flexbox

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. Java算法-堆排序

    package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; public class HeapSor ...

  7. js获取select改变事件

    js获取select改变事件onchage前的值 和 onclick事件 <select id="wupin_id" name="wupin_id" on ...

  8. BZOJ-2037 Sue的小球 DP+费用提前

    似乎很早时学长考过很类似的? 2037: [Sdoi2008]Sue的小球 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 558 Solved: 300 ...

  9. 【bzoj1857】 Scoi2010—传送带

    http://www.lydsy.com/JudgeOnline/problem.php?id=1857 (题目链接) 题意 给出两条线段AB和CD,在AB上的速度为P,在CD上的速度为Q,在AB,C ...

  10. 洛谷P1134 阶乘问题

    题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...