51Node 1065----最小正子段和
2016-07-02 18:35
369 查看
51Node 1065----最小正子段和
N个整数组成的序列a[1],a[2],a[3],…,a
,从中选出一个子序列(a[i],a[i+1],…a[j]),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的。
例如:4,-1,5,-2,-1,2,6,-2。-1,5,-2,-1,序列和为1,是最小的。
Input
Output
Input示例
Output示例
N个整数组成的序列a[1],a[2],a[3],…,a
,从中选出一个子序列(a[i],a[i+1],…a[j]),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的。
例如:4,-1,5,-2,-1,2,6,-2。-1,5,-2,-1,序列和为1,是最小的。
Input
第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数
Output
输出最小正子段和。
Input示例
8 4 -1 5 -2 -1 2 6 -2
Output示例
1 代码如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long LL; struct node { LL m; int pos; } A[50005]; bool cmp(const node x,const node y) { return x.m<y.m; } int main() { int N; while(scanf("%d",&N)!=EOF) { A[0].m=0; A[0].pos=0; for(int i=1; i<=N; i++) { LL x; scanf("%lld",&x); A[i].m=A[i-1].m+x; A[i].pos=i; } sort(A,A+N+1,cmp); LL t=999999999999; for(int i=1; i<=N; i++) { for(int j=i-1; j>=0; j--) { LL tmp=A[i].m-A[i-1].m; if(A[i].pos<A[i-1].pos) tmp=0-tmp; if(tmp>0) { t=min(t,tmp); break; } } } cout<<t<<endl; } return 0; }
相关文章推荐
- 51Node 1035----最长的循环节
- 脚本化文档(1)
- nodejs一文件系统
- Node入门
- node模块系统常用命令
- ZooKeeper的Znode剖析
- Swap Two Nodes in Linked List
- 基于Node的PetShop,RESTful API以及认证
- 《深入浅出Node.js》迷你书笔记
- 237. Delete Node in a Linked List
- [Node.js] Creating JWTs (JSON Web Tokens) in Node
- javafx中为一个Node加入多个Effect
- 基于Node的PetShop,oauth2认证RESTful API
- 基于Node的PetShop,oauth2认证RESTful API
- 索引节点和inode的指针结构浅析
- Node.js 4.4.x 更新日志的重要内容回顾
- Ubuntu 16.04 64位 搭建 node.js NodeJS 环境
- HTMLParser使用详解(2)- Node内容
- nssm在windows服务器上部署nodejs
- Leetcode-populating-next-right-pointers-in-each-node-ii ***