题意:题目很长一大段,其实就是要求最大连续和,不过是要输出连续和最大且长度最长的情况,如果结果是小于等于0,输出no...
题解:计算sum,如果sum大于max,更新max和左右边界,如果sum<0,重新让sum为0,左边界从当前位置开始,注意如果max == sum且左边界不变,要更新右边界。
#include <stdio.h>
#include <string.h>
const int N = 20005;
int main() {
int t, cases = 1, s[N], n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int sum = 0, maxx = -1e9, x, y;
for (int i = 0, j = 0; i < n - 1; i++) {
scanf("%d", &s[i]);
sum += s[i];
if (sum > maxx) {
maxx = sum;
x = j;
y = i;
}
if (sum == maxx && x == j && y < i)
y = i;
if (sum < 0) {
sum = 0;
j = i + 1;
}
}
if (maxx > 0)
printf("The nicest part of route %d is between stops %d and %d\n", cases++, x + 1, y + 2);
else
printf("Route %d has no nice parts\n", cases++);
}
}