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

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

Sample Output

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

Hint

  1. 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的,则就加上。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=;
  7. int a[maxn];
  8. int main(){
  9. int n;
  10. while(scanf("%d",&n)!=EOF){
  11. if(!n)
  12. break;
  13. bool flag=true;
  14. // memset(a,0,sizeof(a));
  15. for(int i=;i<=n;i++){
  16. scanf("%d",&a[i]);
  17. if(a[i]>=)
  18. flag=false;
  19. }
  20. if(flag){
  21. printf("0 %d %d\n",a[],a[n]);
  22. continue;
  23. }
  24. int ans,sum,head,tail,ans_head,ans_tail;
  25. ans=sum=a[];
  26. ans_head=ans_tail=head=tail=;
  27. for(int i=;i<=n;i++){
  28. if(sum>){
  29. sum+=a[i];
  30. tail=i;
  31. }
  32. if(sum<=){
  33. sum=a[i];
  34. head=tail=i;
  35. }
  36. if(sum>ans){
  37. ans=sum;
  38. ans_head=head;
  39. ans_tail =tail;
  40. }
  41. }
  42.  
  43. printf("%d %d %d\n",ans,a[ans_head],a[ans_tail]);
  44.  
  45. }
  46. return ;
  47. }
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

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

Sample Output

Case 1: 14 1 4
Case 2: 7 1 6
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=;
  7. int a[maxn];
  8. int main(){
  9. int n;
  10. int t;
  11. int cnt=;
  12. scanf("%d",&t);
  13. while(t--){
  14. cnt++;
  15. scanf("%d",&n);
  16. // bool flag=true;
  17. // memset(a,0,sizeof(a));
  18. for(int i=;i<=n;i++){
  19. scanf("%d",&a[i]);
  20.  
  21. }
  22. int ans,sum,head,tail,ans_head,ans_tail;
  23. ans=sum=a[];
  24. ans_head=ans_tail=head=tail=;
  25. for(int i=;i<=n;i++){
  26. if(sum>=){
  27. sum+=a[i];
  28. tail=i;
  29. }
  30. if(sum<){
  31. sum=a[i];
  32. head=tail=i;
  33. }
  34. if(sum>ans){
  35.  
  36. ans=sum;
  37. ans_head=head;
  38. ans_tail =tail;
  39. }
  40. }
  41. printf("Case %d:\n",cnt);
  42. printf("%d %d %d\n",ans,ans_head,ans_tail);
  43.  
  44. if(t!=)
  45.  
  46. printf("\n");
  47.  
  48. }
  49. return ;
  50. }
 

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. 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  2. Quartz.net的cron表达式

    写在前面 前面有一篇文章用到了quartz.net,在设置定时时间的时候,使用了cron表达式,这里记录几种常见设置方式,方便对照使用. 详情 在这篇文章:Quartz.Net在windows服务中的 ...

  3. Javascript 使用小案例

    十四.cookie相关 1 <!DOCTYPE html> <html> <head> <script> function setCookie(cnam ...

  4. OC----简单的购物系统----

    今天下午OC上机考试,虽然考试的时候没写完, 但是课下写完了. main.m #import <Foundation/Foundation.h> #import "Shops.h ...

  5. poj1679 次小生成树

    prim方法:先求过一遍prim,同时标记使用过得边.然后同时记录任意2点间的最大值. 每次加入一条新的边,会产生环,删去环中的最大值即可. #include<stdio.h> #incl ...

  6. 【ZOJ 1221】Risk

    题 题意 给你20个城市的相邻关系,求给定任意两个城市的最短距离 分析 求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的. 代码 #include<stdio.h> ...

  7. RequestMethod 相关

    Http协议的Delete和Put方法是做什么的?怎么用? RequestMethod 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 一般来说,Web服务器默认的只支持Pos ...

  8. jquery中datagrid中getSelected和getSelections的应用

    http://blog.sina.com.cn/s/blog_8e50ede90101fff9.html 刚开始使用jquery的datagrid就知道如果要对特定的一行进行编辑,可以是 $('#on ...

  9. tp 多语言支持

    tp支持多语言 通过get来改变语言的 http://localhost/tp/index.php/Admin/User/add/hl/zh-cn http://localhost/tp/index. ...

  10. git工作量统计

    #!/bin/bash function count() { local insert=0 local delete=0 while read line ;do current=`echo $line ...