zoukankan      html  css  js  c++  java
  • 1049 最大子段和

    1049 最大子段和

    基准时间限制:秒 空间限制:131072 KB 

    N个整数组成的序列a[1],a[2],a[3],,a[n],求该序列如a[i]+a[i+1]++a[j]的连续子段和的最大值。当所给的整数均为负数时和为0

    例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13。和为20

    Input

    1行:整数序列的长度N2 <= N <= 50000)

    2 - N + 1行:N个整数(-10^9 <= A[i] <= 10^9

    Output

    输出最大子段和。

    Input示例

    6

    -2

    11

    -4

    13

    -5

    -2

    Output示例

    20

    import java.util.Scanner;
    
    public class Main1 {
        static int n;
        static long a[];
        static long maxSum(int x,int y){ //返回数组在左闭右开区间[x,y)中最大连续和
            long v,L;
            long R,maxs;
            if(y-x==1)return a[x];
            int m=x+(y-x)/2;    //分治第一步;划分成[x,m)和[m,y);
            maxs=Math.max(maxSum(x, m),maxSum(m,y)); //分治第二部:递归求解
            v=0;L=a[m-1];
            for(int i=m-1;i>=x;i--) L=Math.max(L, v+=a[i]);   //分治第三步: 合并(1)--从分界点开始往左的最大连续和L
            v=0;R=a[m];
            for(int i=m;i<y;i++) R=Math.max(R, v+=a[i]);  //分治第三步: 合并(2)--从分界点开始往右的最大连续和R
            return Math.max(maxs, L+R); //把子问题的解与L和R比较
            
        }
        
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext()){
                
                n=sc.nextInt();
                a=new long[n+1];
                for(int i=0;i<n;i++){
                    a[i]=sc.nextLong();
                    
                }
                long ans=maxSum(0,n);
                System.out.println(ans<0? 0:ans);
            }
            sc.close();
    
        }
    
    }

    --------------------------------

    贴一组超时的

    --------------------------------

    import java.util.Scanner;
    
    public class Main {
        
        static int n;
        static long s[];
        static long a[];
        static long maxSum(){
            long maxsum=0;
            for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
            for(int i=1;i<=n;i++)
                for(int j=i;j<=n;j++)
                    maxsum=Math.max(maxsum, s[j]-s[i-1]);
            
            return maxsum;
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext()){
                 n=sc.nextInt();
                a=new long[n+1];
                s=new long[n+1];
                for(int i=1;i<=n;i++){
                    
                    a[i]=sc.nextInt();
                }
                long ans=maxSum();
                System.out.println(ans<0? 0:ans);
                
            }
            sc.close();
    
        }
    
    }
  • 相关阅读:
    VOA 2009/11/02 DEVELOPMENT REPORT In Kenya, a Better Life Through Mobile Money
    2009.11.26教育报道在美留学生数量创历史新高
    Java中如何实现Tree的数据结构算法
    The Python Tutorial
    VOA HEALTH REPORT Debate Over New Guidelines for Breast Cancer Screening
    VOA ECONOMICS REPORT Nearly Half of US Jobs Now Held by Women
    VOA ECONOMICS REPORT Junior Achievement Marks 90 Years of Business Education
    VOA 2009/11/07 IN THE NEWS A Second Term for Karzai; US Jobless Rate at 10.2%
    Ant入门
    Python 与系统管理
  • 原文地址:https://www.cnblogs.com/watchfree/p/5348677.html
Copyright © 2011-2022 走看看