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. android之简易新闻客户端

    将一个新闻信息保存到一个XML文件中,并将放在服务器下.通过手机客户端来从服务器下载该文件并解析显示. news.xml <?xml version="1.0" encodi ...

  2. 读JS高级——第五章-引用类型 _记录

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. BIEE 配置邮箱服务器

    找个免费邮件服务器126或者163的 登录em地址,点击“部署”——>“邮件”——>“锁定并编辑”——>应用——>激活更改——>重新启动以应用最近更改——>重新启动 ...

  4. 001-编译hadoop-2.5.2总结

    前两天废了很大的劲来对hadoop-2.5.2进行64位系统的手动编译,由于对linux系统环境以及hadoop本身的不熟悉,编译过程中也出现了很多的问题,在此记录一下,对自己以后再次编译和看到此文章 ...

  5. bzoj 1222 DP

    用w[i]表示在A中用了i的时间时在B中最少用多长时间,然后转移就可以了. 备注:这个边界不好定义,所以可以每次用一个cur来存储最优值,然后对w[i]赋值就可以了. /*************** ...

  6. 【BZOJ-1965】SHUFFLE 洗牌 快速幂 + 拓展欧几里德

    1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 541  Solved: 326[Submit][St ...

  7. [NOIP2010] 提高组 洛谷P1540 机器翻译

    题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...

  8. 学习 easyui 之一:easyloader 分析与使用

    http://www.cnblogs.com/haogj/archive/2013/04/22/3036685.html 使用脚本库总要加载一大堆的样式表和脚本文件,在 easyui 中,除了可以使用 ...

  9. 史上最详细的Android Studio系列教程四--Gradle基础

    https://segmentfault.com/a/1190000002439306#articleHeader0

  10. c#中的23种设计模式

    C# 23种设计模式汇总 创建型模式 工厂方法(Factory Method) 在工厂方法模式中,工厂方法用来创建客户所需要的产品,同时还向客户隐藏了哪种具体产品类将被实例化这一细节.工厂方法模式的核 ...