zoukankan      html  css  js  c++  java
  • PAT 1007

    1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

    Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

    Input Specification:

    Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

    Output Specification:

    For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

    Sample Input:
    10 -10 1 2 3 4 -5 -23 3 7 -21 
    Sample Output:
    10 1 4 

    动态规划的经典问题,最大连续子序列和。

    代码

     1 #include <stdio.h>
     2 
     3 int main()
     4 {
     5     int K;
     6     int data[10000];
     7     int i;
     8     int flag;
     9     while(scanf("%d",&K) != EOF){
    10         flag = 0;
    11         for (i=0;i<K;++i){
    12             scanf("%d",&data[i]);
    13             if(data[i] >= 0)
    14                 flag = 1;
    15         }
    16         if(flag == 0){
    17             printf("0 %d %d ",data[0],data[K-1]);
    18             continue;
    19         }
    20         int max_seq = data[0];
    21         int max_s = 0,max_e = 0;
    22         int sum = data[0];
    23         int sum_s = 0,sum_e = 0;
    24         for(i=1;i<K;++i){
    25             if(sum < 0){
    26                 sum = data[i];
    27                 sum_s = sum_e = i;
    28             }
    29             else{
    30                 sum += data[i];
    31                 sum_e = i;
    32             }
    33             if(sum > max_seq){
    34                 max_seq = sum;
    35                 max_s = sum_s;
    36                 max_e = sum_e;
    37             }
    38         }
    39         printf("%d %d %d ",max_seq,data[max_s],data[max_e]);
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    物理删除文件 业务层
    页面在本机可以显示,其它机器不可以看到页面
    我对asp.net并行请求数量的理解
    分布式缓存Memcached
    任意两个对象赋值,用Spring.Objects.ObjectWrapper效率比直接反射还慢?
    在Linux(RHEL5.5)里用mono2.8.2和jexus4.1运行.net3.5下的MVC2.0过程记录
    Nhibernate连接oracle数据库,主键ID用序列生成时连接数据库IO次数分析
    Sqlserver别太信任SysComments表中的text字段
    .net4.0线程池取消执行的实际应用
    spring.net、castle windsor、unity实现aop、ioc的方式和简单区别
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1007.html
Copyright © 2011-2022 走看看