本文主要是介绍UVA - 507 Jill Rides Again,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:求最大的连续子序列和,当最大相等的时候,取最长的长度#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;int arr[20001],n,Max,begin,start,end;int main()
{int t;scanf("%d",&t);for (int i = 1; i <= t; i++){scanf("%d",&n);int flag = 1;for (int j = 1; j < n; j++){scanf("%d",&arr[j]);if (arr[j] > 0)flag = 0;}if (flag)printf("Route %d has no nice parts\n",i);else {int sum = 0,Max = 0;start = begin = end = 1;for (int j = 1; j < n; j++){sum += arr[j];if (sum < 0){sum = 0;start = j + 1;}if (sum > Max || (sum == Max) && (j - start > end - begin)){Max = sum;begin = start;end = j;}}printf("The nicest part of route %d is between stops %d and %d\n",i,begin,end+1);}}return 0;
}
这篇关于UVA - 507 Jill Rides Again的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!