码迷,mamicode.com
首页 > 其他好文 > 详细

uva507 - Jill Rides Again(最长连续和)

时间:2014-07-17 20:07:53      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   2014   art   io   

题目:uva507 - Jill Rides Again(最长连续和)


题目大意:给每两个站之间的满意度,满意的路线必须加起来的和不小于0.帮Jill找出她满意的路线,要求是最长的,并且一样长的话取站相对靠前的。


代码:

#include <stdio.h>
#include <string.h>

const int N = 20005;
int s, b, e;
int stop[N];

int solve () {
	
	int mm = stop[1];
	int sum = stop[1];
	int tems = 1;
	b = e = 1;

	if (sum < 0) {

		tems = 2;
		sum = 0;
	}

	for (int i = 2; i < s; i++) {

		sum += stop[i];
		if (sum < 0) {

			tems = i + 1;
			sum = 0;
		}
		else if (sum >= mm) {
				
			if ( (sum == mm && i - tems > e - b) || sum > mm) {
				
				b = tems;
				e = i;
				mm = sum;
			}	
		}
	}
	return mm;	
}

int main () {

	int t;
	scanf ("%d", &t);
	for (int i = 1; i <= t; i++) {

		scanf("%d", &s);
		for (int j = 1; j < s; j++)
			scanf ("%d", &stop[j]);
		
		int mm = solve();
		if (mm < 0)
			printf ("Route %d has no nice parts\n", i);
		else
			printf ("The nicest part of route %d is between stops %d and %d\n", i, b, e + 1);
	}
	return 0;
}


uva507 - Jill Rides Again(最长连续和),布布扣,bubuko.com

uva507 - Jill Rides Again(最长连续和)

标签:des   blog   http   2014   art   io   

原文地址:http://blog.csdn.net/u012997373/article/details/37907013

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!