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

不考虑环:直接找非循环子序列maxsum1;

然后考虑循环:也就是两头相加情况,这是找非循环子序列minsum,把总和-minsum又得到一个maxsum2;

但前提的要有负数存在,负数不存在的话就直接是总和了;

否则就是max(maxsum1,maxsum2)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[100005];
int main()
{
int i,j,t,n;scanf("%d",&t);
while(t--){
scanf("%d",&n);
ll maxx=-0x3f3f3f3f,minn=0x3f3f3f3f;
ll sum=0,s=0;
int f=0;
for(i=0;i<n;i++){
scanf("%I64d",&a[i]);
if(a[i]>=0)f=1;
s+=a[i];
sum+=a[i];
maxx=max(maxx,sum);
if(sum<0)sum=0;
}
sum=0;
for(i=0;i<n;i++){
sum+=a[i];
minn=min(minn,sum);
if(sum>0)sum=0;
}
if(f==1)maxx=max(maxx,s-minn);
printf("%I64d\n",maxx);
}
return 0;
}

3927Circular Sequence 思维题(求环形最大子列和)的更多相关文章

  1. HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002

    题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...

  2. ZOJ 4060 - Flippy Sequence - [思维题][2018 ACM-ICPC Asia Qingdao Regional Problem C]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4060 题意: 给出两个 $0,1$ 字符串 $S,T$,现在你有 ...

  3. PATtest1.3:最大子列和

    题目源于:https://pintia.cn/problem-sets/16/problems/663 题目要求:输入一个数列,求其最大子列和. 问题反馈:1.部分C++代码不是很熟练 2.没有仔细读 ...

  4. 01-最大子列和问题(java)

    问题描述:给定N个整数的序列{A1,A2,A3,…,An},求解子列和中最大的值. 这里我们给出{-2,11,-4,13,-5,-2}这样一个序列,正确的最大子列和为20 该题是在数据结构与算法中经常 ...

  5. Task 4.3 求环形数组的最大子数组和

    任务要求:输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.    如果数组A[0]……A[j-1]首尾相邻,允许A[i-1], …… A[n- ...

  6. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  7. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  8. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  9. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  10. BZOJ4401: 块的计数 思维题

    Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...

随机推荐

  1. Java方法定义及调用。

    一.什么是方法 1.Java方法是语句的集合,它们在一起执行一个功能. ①方法是解决一类问题的步骤的有序组合 ②方法包含与类或对象中 ③方法在程序中被创建,在其他地方被引用 2.设计方法的原则:方法的 ...

  2. MAMP PRO 使用指南 (配置nginx 重写)

    https://sawlove.com/mamp-pro-use-for-wp.html 1 location / { 2 if (!-e $request_filename) { 3 rewrite ...

  3. .net code 3.0 获取配置文件 json 和 config 中的值

    using Microsoft.Extensions.Configuration;using Microsoft.Extensions.FileProviders; using System.IO; ...

  4. mysql中char和varchar的区别

    char的长度是不可变的,是定长的, varchar的长度是可变的,不定长的: 但是char的存取速度比varchar快,因为其长度固定,方便存储和查找. char空间换时间,varchar时间换空间 ...

  5. Python学习的第二次总结

    有限循环 for   i   in range()# i自动加一   # for语句中若有break被执行,则跟着for后面的else语句就不会被正常执行:反之亦然 for i in range(3) ...

  6. pytest(5)-自定义用例顺序(pytest-ordering)-后续学习

    前言 测试用例在设计的时候,我们一般要求不要有先后顺序,用例是可以打乱了执行的,这样才能达到测试的效果. 有些同学在写用例的时候,用例写了先后顺序, 有先后顺序后,后面还会有新的问题(如:上个用例返回 ...

  7. 新装Eclipse运行Java程序报错Exception in thread "main" java.lang.UnsupportedClassVersionError

    错误现象:   Exception in thread "main" java.lang.UnsupportedClassVersionError: views/LoginFram ...

  8. SpringCloudEureka上篇

    SpringCloudEureka上篇 本文学习自<<重新定义SpringCloud>> Eureka简介 Eureka是Netflix公司开源的一款服务发现组件,该组件提供的 ...

  9. vm-linux-格式化磁盘以及挂载

    数据盘大小20g 文件系统xfs 分区工具fdisk 1,虚拟机关机状态,打开设置新添加一个20g硬盘 2,重新启动虚拟机,并登陆linux,打开命令窗口 3,对磁盘进行分区 输入 fdisk -l ...

  10. thirty-one

    动态组件 动态切换组件的显示和隐藏 如何实现动态组件的渲染 vue提供了有一个内置的<component>组件,专门用来实现动态组件的渲染.示例代码如下: 使用keep-alive保持状态 ...