初学数据结构、、书上的在线算法 感觉有点像dp、、当然简单求lis的话dp能过 不过没试过这个用dp怎么样、、 有机会补上
#include<iostream>
#include<stdio.h>
using namespace std;
#pragma warning(disable:4996)
const int N = 1e5 + 10;
int a[N];
int k;
int main() {
scanf("%d", &k);
for (int i = 0; i < k; i++)scanf("%d", &a[i]);
int sum = 0;
int cnt = 0;
int maxsum = -1;//test static -1 -2 0
//如果不置成负数面对上述数据时有误
int st = a[0], ed = a[k - 1];
for (int i = 0; i < k; i++) {
sum += a[i];
if (sum > maxsum) {
maxsum = sum;
ed = a[i];
st = a[cnt];
}
else if (sum < 0) {
sum = 0;
cnt = i + 1;
}
}
if(maxsum>0)cout << maxsum << " " << st << " " << ed;
else {
if (maxsum == 0)cout << maxsum << " " << st << " " << ed;
//得讨论如-1 -2 0这种类型的测试数据的输出
else cout << 0 << " " << a[0] << " " << a[k - 1];
//如果全是负值
}
return 0;
}