zoukankan      html  css  js  c++  java
  • 【原】 POJ 2593 Max Sequence 动态规划 解题报告

    http://poj.org/problem?id=2593

    Maximum Sequence
    求数组两段不重叠的连续子数组的最大和
    详见2479

    Description

    Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N).

    clip_image001

    You should output S.

    Input

    The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.

    Output

    For each test of the input, print a line containing S.

    Sample Input

    5

    -5 9 -5 11 20

    0

    Sample Output

    40

       1: #include <stdio.h>
       2: #include <iostream>
       3:  
       4: using namespace std ;
       5:  
       6:  
       7: const int N = 100000 ;
       8:  
       9: int a[N] ;
      10: int maxsofar[2][N] ;
      11:  
      12: void run2593()
      13: {
      14:     int n,val ;
      15:     int i,j ;
      16:     int maxendinghere ;
      17:     int max ;
      18:     int sum ;
      19:  
      20:     while(  scanf( "%d", &n ) && n!=0 )
      21:     {
      22:         scanf( "%d", &a[0] ) ;
      23:         maxendinghere = a[0] ;
      24:         maxsofar[0][0] = a[0] ;
      25:         for( i=1 ; i<n ; ++i )
      26:         {
      27:             scanf( "%d", &a[i] ) ;
      28:             maxendinghere = std::max( a[i] , maxendinghere+a[i] ) ;
      29:             maxsofar[0][i] = std::max( maxsofar[0][i-1] , maxendinghere ) ;
      30:         }
      31:  
      32:         maxendinghere = a[n-1] ;
      33:         maxsofar[1][n-1] = a[n-1] ;
      34:         max = maxsofar[0][n-2] + a[n-1] ;
      35:         for( i=n-2 ; i>0 ; --i )
      36:         {
      37:             maxendinghere = std::max( a[i] , a[i]+maxendinghere ) ;
      38:             maxsofar[1][i] = std::max( maxsofar[1][i+1] , maxendinghere ) ;
      39:             sum = maxsofar[0][i-1] + maxsofar[1][i] ;
      40:             max = max>sum ? max : sum ;
      41:         }
      42:         printf( "%d\n" , max ) ;
      43:     }
      44: }
  • 相关阅读:
    8.池化内存分配
    7.netty内存管理-ByteBuf
    6.ChannelPipeline
    5.接入客户端连接
    4.Netty执行IO事件和非IO任务
    3.NioEventLoop的启动和执行
    2.NioEventLoop的创建
    1.netty服务端的创建
    微信订阅号开发初探
    jmeter自动化脚本编写
  • 原文地址:https://www.cnblogs.com/allensun/p/1872029.html
Copyright © 2011-2022 走看看