zoukankan      html  css  js  c++  java
  • HDU 1231 最大连续子序列

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

    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
    Huge input, scanf is recommended.
    动态规划,注意变量初始化值
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <queue>
     7 #include <set>
     8 #include <map>
     9 #include <string>
    10 #include <cmath>
    11 #include <stdlib.h>
    12 #define MAXSIZE 10005
    13 using namespace std;
    14 
    15 int n;
    16 int a[MAXSIZE];
    17 
    18 
    19 int main()
    20 {
    21     //freopen("caicai.txt","r",stdin);
    22     while(scanf("%d",&n)!=EOF && n)
    23     {
    24         int i,j,flag = 0;
    25         for(i = 0;i<n;i++)
    26             scanf("%d",&a[i]);
    27         for(i = 0;i<n;i++)
    28         {
    29             if(a[i]>=0)
    30             {
    31                 flag = 1;
    32                 continue;
    33             }
    34         }
    35         if(!flag)
    36             printf("0 %d %d
    ",a[0],a[n-1]);
    37         else{
    38 
    39             int sum = a[0],b = a[0];
    40             int p =0;//不需要q标记终点位置,终点是i
    41             int m = 0,t=n-1;
    42             for(i = 1;i<n;i++)
    43             {
    44 
    45                 if(b<0)
    46                 {
    47                     b = a[i];
    48                     p = i;
    49                 }
    50                 else
    51                 {
    52                     b += a[i];
    53                 }
    54                 if(sum<b)
    55                 {
    56                     sum = b;
    57                     m = p;t = i;
    58                 }
    59             }
    60             sum = max(sum,a[0]);
    61             printf("%d %d %d
    ",sum,a[m],a[t]);
    62         }
    63     }
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    Redhat各个版本和内核对照
    Java8 lambda表达式总结
    conda 安装指定版本的指定包
    git初始化的几句shell
    MYsqli 绑定插入与查询实例
    按天去除重复数据,为0则取0,否则取最大的那个值
    存储过程,循环插入1000条记录
    主表如何统计在附表中的出现次数?
    Invalid argument supplied for foreach()
    二十、mysql mysqldump备份工具
  • 原文地址:https://www.cnblogs.com/caitian/p/5818973.html
Copyright © 2011-2022 走看看