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. 服务器网站报错:由于扩展配置问题无法提供您请求的页面,请添加MIME映射,针对mp4,flv文件类型无法打开。

    确保IIS正常 服务器增加mp4格式的MIME 类型映射设置的具体步骤是: “开始” > “控制面板” > “管理工具” >“Internet 信息服务(IIS管理器)”,“MIME ...

  2. 多个TableView的练习

    效果图: 左边图片的代码: // // SecViewController.m // UI__多个TableView练习 // // Created by dllo on 16/3/17. // Co ...

  3. OneZero第一次会议(非正式)

    会议时间:2016年3月20日 15:50~16:50 会议成员:冉华(http://www.cnblogs.com/ranh941/) 张敏(http://www.cnblogs.com/zhang ...

  4. hdu4081 次小生成树

    题意:有n个点,n-1条边.现在徐福可以让一条边无消耗建立,即魔法边.B表示除魔法边之外的的其他边的消耗值和,A表示这条魔法边相连的2个集合中都选一点,这两点的最大值,现在要求A/B最大. 方法:因为 ...

  5. 金融自助设备WOSA/XFS规范介绍

    wosa(全称是windows开放式系统体系结构 windows open system architecture),是微软公司提出的一种在windows操作系统下的软件架构. wosa/xfs是基于 ...

  6. 【ZOJ 3502】Contest

    题 题意 n个问题,解决的顺序影响正确的概率,无论之前解决的问题是否答对,当前问题 j 答对概率为max{a[i][j]} (i为解决过的问题).求答对题目的最大期望和对应的答题顺序.T组测试,T ( ...

  7. ElasticSearch插件安装Head、Kopf与Bigdesk

    ElasticSearch-Head ElasticSearch-Head 是一个与Elastic集群(Cluster)相交互的Web前台. ES-Head的主要作用 它展现ES集群的拓扑结构,并且可 ...

  8. POJ2135 Farm Tour

      Farm Tour Time Limit: 2MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description ...

  9. java 文件读取大全

    1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**      * 以字节为单位读取文件,常用 ...

  10. ASP.NET MVC4中调用WEB API的四个方法

    http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...