洛谷 P1115最大子段和

洛谷P1115最大子段和

代码

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    private static int[][] direct={{0,1},{1,0},{-1,0},{0,-1}};
    private static AWriter writer = new AWriter(System.out);
    private static AReader reader = new AReader(System.in);
    private static long n;
    private static double INFINITY=Double.POSITIVE_INFINITY;
    private static int a[];
    public static void main(String[] args) throws IOException {
        int n=reader.nextInt();
        a=new int[n];
        for(int i=0;i<n;i++)
            a[i]=reader.nextInt();
        int ans=0;
        int maxAns=-0x3f3f3f;
        for(int i=0;i<n;i++){
            ans+=a[i];
            if(ans>maxAns)
                maxAns=ans;
            if(ans<0){
                ans=0;
            }
        }
        writer.println(maxAns);
        writer.close();
        reader.close();
    }
}

class AReader implements Closeable {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public AReader(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
        tokenizer = new StringTokenizer("");
    }

    private String innerNextLine() {
        try {
            return reader.readLine();
        } catch (IOException ex) {
            return null;
        }
    }

    public boolean hasNext() {
        while (!tokenizer.hasMoreTokens()) {
            String nextLine = innerNextLine();
            if (nextLine == null) {
                return false;
            }

            tokenizer = new StringTokenizer(nextLine);
        }

        return true;
    }

    public String nextLine() {
        tokenizer = new StringTokenizer("");
        return innerNextLine();
    }

    public String next() {
        hasNext();
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.valueOf(next());
    }

    public long nextLong() {
        return Long.valueOf(next());
    }

    public double nextDouble() {
        return Double.valueOf(next());
    }

    public BigInteger nextBigInteger() {
        return new BigInteger(next());
    }

    @Override
    public void close() throws IOException {
        reader.close();
    }
}

// Fast writer for ACM By Azure99
class AWriter implements Closeable {
    private BufferedWriter writer;

    public AWriter(OutputStream outputStream) {
        writer = new BufferedWriter(new OutputStreamWriter(outputStream));
    }

    public void print(Object object) throws IOException {
        writer.write(object.toString());
    }

    public void println(Object object) throws IOException {
        writer.write(object.toString());
        writer.write("\n");
    }

    @Override
    public void close() throws IOException {
        writer.close();
    }
}

思路

超级经典的一道题,还有 o ( n log ⁡ n ) o(n\log n) o(nlogn)的动态规划法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值