zoukankan      html  css  js  c++  java
  • TZOJ 3927 Circular Sequence(环形最大子段和)

    描述

    Given a sequence with n elements, if the last element is also adjacent to the first element of the sequence, the sequence is called “circular sequence”.
    Now, here comes a simple problem: given a circular sequence, you should find a segment of the sequence, such that it contains at least one element, and the sum of the elements is as large as possible. 

    输入

    Input may contain several test cases. The first line is a single integer t, the number of test cases below. Each test case is composed of two lines. The first line of each test case is a positive integer n (1<=n<=100000), denoting the number of elements in the circular sequence, and the second line has n integers; the absolute value of each integer is no more than 100000. 

    输出

    For each test case, output the maximum segment sum in a single line. 

    样例输入

    2
    2
    -1 -2
    3
    1 -2 3

    样例输出

    -1
    4

    题意

    n个数,求环形最大字段和。

    题解

    答案分两种情况:

    1.答案不穿越两端,相当于是普通的最大子段和,直接写【ans1】。

    2.答案穿越两端,相当于是前一段+后一段,那么相当于总和-普通的最小子段和【ans2】。

    代码

     1 import java.io.OutputStream;
     2 import java.io.IOException;
     3 import java.io.InputStream;
     4 import java.io.PrintWriter;
     5 import java.util.Arrays;
     6 import java.util.StringTokenizer;
     7 import java.io.IOException;
     8 import java.io.BufferedReader;
     9 import java.io.InputStreamReader;
    10 import java.io.InputStream;
    11 
    12 public class Main {
    13 
    14     public static void main(String[] args) {
    15         InputStream inputStream = System.in;
    16         OutputStream outputStream = System.out;
    17         InputReader in = new InputReader(inputStream);
    18         PrintWriter out = new PrintWriter(outputStream);
    19         TaskA solver = new TaskA();
    20         int T = in.nextInt();
    21         for (int i = 1; i <= T; i++)
    22             solver.solve(i, in, out);
    23         out.close();
    24     }
    25 
    26     static class TaskA {
    27         private long[] a;
    28         public void solve(int testNumber, InputReader in, PrintWriter out) {
    29             int n = in.nextInt();
    30             a = new long[n + 5];
    31             long AC = 0, min = 0, max = 0, p = (long)-1e9;
    32             boolean positive = true;
    33             for (int i = 1; i <= n; i++) {
    34                 int x = in.nextInt();
    35                 p = Math.max(p, x);
    36                 a[i] = a[i-1] + x;
    37                 if (x >= 0)
    38                     positive = false;
    39             }
    40             // 全是负数
    41             if (positive) {
    42                 System.out.println(p);
    43                 return;
    44             }
    45             for (int i = 1; i <= n; i++) {
    46                 // 不跨越,普通最大子段和
    47                 long ans1 = a[i] - min;
    48                 // 跨越,总和-普通最小子段和
    49                 long ans2 = a[n] - (a[i] - max);
    50                 min = Math.min(min, a[i]);
    51                 max = Math.max(max, a[i]);
    52                 AC = Math.max(AC, Math.max(ans1, ans2));
    53             }
    54             System.out.println(AC);
    55         }
    56     }
    57 
    58     static class InputReader {
    59         public BufferedReader reader;
    60         public StringTokenizer tokenizer;
    61 
    62         public InputReader(InputStream stream) {
    63             reader = new BufferedReader(new InputStreamReader(stream), 32768);
    64             tokenizer = null;
    65         }
    66 
    67         public String next() {
    68             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
    69                 try {
    70                     tokenizer = new StringTokenizer(reader.readLine());
    71                 } catch (IOException e) {
    72                     throw new RuntimeException(e);
    73                 }
    74             }
    75             return tokenizer.nextToken();
    76         }
    77 
    78         public int nextInt() {
    79             return Integer.parseInt(next());
    80         }
    81     }
    82 }
  • 相关阅读:
    Python中 sys.argv[]的用法简明解释
    Python--文件操作和集合
    Python--各种参数类型
    Python-- 字符串格式化 (%操作符)
    Python--绝对路径和相对路径
    Python3.x和Python2.x的区别
    关于ArrayList使用中的注意点
    Sun公司建议的Java类定义模板
    SWT组件之Table浅析
    mysql的相关命令整理(二)
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/12288653.html
Copyright © 2011-2022 走看看