记录编号 |
447732 |
评测结果 |
AAAAAAAAAA |
题目名称 |
单子序列最大和 |
最终得分 |
100 |
用户昵称 |
HeHe |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.020 s |
提交时间 |
2017-09-11 12:38:47 |
内存使用 |
12.01 MiB |
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
inline char getc(void) {
static char buf[1 << 18], *fs, *ft;
return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft) ? EOF : *fs++;
}
inline int read(void) {
register char tmp = getc(), f = 1;
register int res = 0;
while(!isgraph(tmp)) tmp = getc();
if(tmp == '-') f = -1, tmp = getc();
while(isdigit(tmp))
res = ((res + (res << 2)) << 1) + (tmp ^ 0x30),
tmp = getc();
return res * f;
}
#define MAXN (1000010)
#define INF (0x7fffffff)
typedef long long int64;
int N, s[MAXN];
int maxx = -INF;
int64 sum[MAXN];
int l, r;
int main() {
#ifndef LOCAL
freopen("subq.in", "r", stdin);
freopen("subq.out", "w", stdout);
#endif
N = read();
bool flag = true;
for(int i = 1; i <= N; ++i) {
s[i] = read();
if(s[i] > 0) flag = false;
else {
if(maxx < s[i]) {
l = r = i;
maxx = s[i];
}
}
}
if(flag) {
printf("%d\n%d\n%d\n", l, r, maxx);
return 0;
}
int k = 1, nCurSum = 0;
maxx = -INF;
for(int i = 1; i <= N; ++i) {
nCurSum += s[i];
if(nCurSum < 0) {
nCurSum = 0;
k = i + 1;
}
if(nCurSum > maxx) {
maxx = nCurSum;
l = k, r = i;
}
}
printf("%d\n%d\n%d\n", l, r, maxx);
return 0;
}