1050 循环数组最大子段和

  1. 1 秒
  2. 131,072 KB
  3. 10 分
  4. 2 级题
 
N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连续的子段和的最大值(循环序列是指n个数围成一个圈,因此需要考虑a[n-1],a[n],a[1],a[2]这样的序列)。当所给的整数均为负数时和为0。
例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13。和为20。
 
 

输入

第1行:整数序列的长度N(2 <= N <= 50000)
第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9)

输出

输出循环数组的最大子段和。

输入样例

6
-2
11
-4
13
-5
-2

输出样例

20
对于横跨1,n的子段,其实就是总和sum-Min子段和;最后取max即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
int a[maxn];
ll sum = 0;
ll MAX() {
ll maxx = -inf;
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (ans + 1ll * a[i] > 0) {
ans += 1ll * a[i];
maxx = max(maxx, ans);
}
else {
ans = 0;
maxx = max(maxx, 0ll);
}
}
return maxx;
} ll MIN() {
ll minn = inf;
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (ans + 1ll * a[i] < 0) {
ans += 1ll * a[i];
minn = min(minn, ans);
}
else {
ans = 0;
minn = min(minn, 0ll);
}
}
return minn;
} int main()
{
// ios::sync_with_stdio(0);
n = rd();
for (int i = 1; i <= n; i++) {
a[i] = rd(); sum += 1ll * a[i];
}
printf("%lld\n", max(MAX(), sum - MIN()));
return 0;
}

51 Nod 1050 dp的更多相关文章

  1. 51 Nod 1007 dp

    1007 正整数分组 1 秒 131,072 KB 10 分 2 级题   将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1, ...

  2. 51 nod 1055 最长等差数列(dp)

    1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 N个不同的正整数,找出由这些数组成的最长的等差数列.     例如:1 3 5 6 8 9 ...

  3. 51 nod 1610 路径计数(Moblus+dp)

    1610 路径计数 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   路径上所有边权的最大公约数定义为一条路径的值. 给定一个有向无环图.T次修改操作,每次修改一 ...

  4. 51 nod 1766 树上的最远点对(线段树+lca)

    1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题   n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...

  5. 51 nod 1439 互质对(Moblus容斥)

    1439 互质对 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有n个数字,a[1],a[2],…,a[n].有一个集合,刚开 ...

  6. 51 nod 1495 中国好区间

    1495 中国好区间 基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是& ...

  7. 51 nod 1427 文明 (并查集 + 树的直径)

    1427 文明 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 难度:6级算法题   安德鲁在玩一个叫“文明”的游戏.大妈正在帮助他. 这个游 ...

  8. 51 nod 1421 最大MOD值

    1421 最大MOD值 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以 ...

  9. 51 nod 1681 公共祖先 (主席树+dfs序)

    1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...

随机推荐

  1. 无法访问windows安装服务。发生这种情况的可能是您在安全模式下运行windows,或是没有正确安装windows安装,。请与技术支持人员联系以获得帮助。

    解决办法: 1.命令提示符下输入:msiexec/regserver 2.在“管理工具”→“服务”中启动windows Installer 程序员的基础教程:菜鸟程序员

  2. [c++] final override keyword

    the two keyword are aimed at virtual function final final function must be a virtual funtion , final ...

  3. 如何写摘要(abstract)

  4. 简单引入logback

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  5. 编写高质量代码改善C#程序的157个建议——建议105:使用私有构造函数强化单例

    建议105:使用私有构造函数强化单例 单例指一个类型只生成一个实例对象.单例的一个简单实现如下所示: static void Main(string[] args) { Singleton.Insta ...

  6. 【Linux】GDB查看栈信息(转)

    在调试程序的过程中,查看程序的函数调用堆栈是一项最基本的任务,几乎所有的图形调试器都支持这项特性. GDB调试器当然也支持这一特性,但是功能更加灵活和丰富. GDB将当前函数的栈帧编号为0,为外层函数 ...

  7. python字符串字典列表互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  8. 解读DbContext中的三种【EagerLoad,LazyLoad,ExplicitLoading】加载模式

    一:立即加载,延迟加载,显式加载 1. 立即加载 Student Domain{ Teacher theacher} using (SchoolDBEntities db = new SchoolDB ...

  9. 构造复杂Lambda困惑之学会用LinqPad和Linqer实现Sql 和 Lambda之间的互转

    一:linq的话我们可能会遇到两个问题: 1. 我们的linq出现性能低下的时候,如果优化???? 我们写的linq所生成的sql是无法控制的... (要做性能优化,必须预先知道sql会生成啥样的?? ...

  10. Android-主题

    主题分为两种: 第一种:使用Android系统提供的主题    第二种:自己定义主题 样式 与 主题 区分理解 样式是控制(View的子类风格)控件风格 或者 是(ViewGroup的子类风格)布局风 ...