POJ2479&POJ2593

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=2479

思路:前后加,一直取最大数,如果和小于0则清零,算出前后2边的各个和的最大值,最后平均一下,判断从哪里把数组分为2段。这题的IO很恶心,用Scanner会超时,必须使用BufferedReader + InputStreamReader + readLine +split才AC,-_-!

╭∩╮(︶︿︶)╭∩╮b4下...

 

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=2593

思路:和上面题一样。。输入换了种方式,n也小了几倍,不知道IO情况怎么样,反正我java用了700多ms。。

  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. public class Main {
  4.     int t;
  5.     int l;
  6.     int[] a;
  7.     String[] s;
  8.     int[] left;
  9.     int[] right;
  10.     int max = 0;
  11.     int temp;
  12.     public Main() throws Exception {
  13.         BufferedReader read = new BufferedReader(new InputStreamReader(
  14.                 System.in));
  15.         t = Integer.parseInt(read.readLine());
  16.         for (int i = 0; i < t; i++) {
  17.             read.readLine();
  18.             l = Integer.parseInt(read.readLine());
  19.             a = new int[l];
  20.             left = new int[l];
  21.             right = new int[l];
  22.             s = read.readLine().split(" ");
  23.             for (int j = 0; j < l; j++) {
  24.                 a[j] = Integer.parseInt(s[j]);
  25.             }
  26.             search();
  27.             System.out.println(max);
  28.         }
  29.     }
  30.     public void search() {
  31.         temp = a[0];
  32.         left[0] = temp;
  33.         for (int i = 1; i < l; i++) {
  34.             if (temp < 0) {
  35.                 temp = 0;
  36.             }
  37.             temp += a[i];
  38.             if (temp > left[i - 1]) {
  39.                 left[i] = temp;
  40.             } else {
  41.                 left[i] = left[i - 1];
  42.             }
  43.         }
  44.         temp = a[l - 1];
  45.         right[0] = temp;
  46.         for (int i = l - 2, k = 1; i >= 0; i--, k++) {
  47.             if (temp < 0) {
  48.                 temp = 0;
  49.             }
  50.             temp += a[i];
  51.             if (temp > right[k - 1]) {
  52.                 right[k] = temp;
  53.             } else {
  54.                 right[k] = right[k - 1];
  55.             }
  56.         }
  57.         max = left[0] + right[l - 2];
  58.         for (int i = 0; i < l - 1; i++) {
  59.             if (left[i] + right[l - i - 2] > max) {
  60.                 max = left[i] + right[l - i - 2];
  61.             }
  62.         }
  63.     }
  64.     public int getLength(int m, int n) {
  65.         max = Integer.MIN_VALUE;
  66.         temp = 0;
  67.         for (int i = m; i < n; i++) {
  68.             temp += a[i];
  69.             if (temp > max) {
  70.                 max = temp;
  71.             }
  72.             if (temp < 0) {
  73.                 temp = 0;
  74.             }
  75.         }
  76.         return max;
  77.     }
  78.     public static void main(String[] args) throws Exception {
  79.         new Main();
  80.     }
  81. }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值