首页 > 代码库 > uva507 - Jill Rides Again(最长连续和)
uva507 - Jill Rides Again(最长连续和)
题目: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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。