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;
}

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

时间: 2024-12-29 12:28:39

uva507 - Jill Rides Again(最长连续和)的相关文章

UVa 507 Jill Rides Again (最大连续子序列)

Jill Rides Again Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Jill Rides Again  Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses

uva 507 Jill Rides Again (分治)

uva 507 Jill Rides Again Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses the excellent public bus system for part of her journey. She has a folding bicycle which she carries with her

uva 507 Jill Rides Again (DP)

uva 507 Jill Rides Again Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses the excellent public bus system for part of her journey. She has a folding bicycle which she carries with her

ACdream ????计算最长连续相同字符的个数

计算最长连续相同字符的个数 测试数据 输入: aaaassdfg adasafag ssddffffgt 输出: 4 1 4 <span style="font-size:18px;">#include<stdio.h> #include<string.h> int main() { int i,n,t; int b[105]; char a[105]; scanf("%d",&t); while(t--) { n=0;

任意区间的最长连续递增子序列,最大连续子序列和

hdu3308 给n个数,有m个操作 U a b 表示将第a个数改成b Q a b 表示询问区间[a,b]的最长连续递增子序列. 区间询问问题且带修改,一般是用线段树来解决 那么要维护 Llen[rt], Lval[rt][2] 表示rt所对应的区间[l,r] 以l开头的最长连续递增子序列的长度, Lval[rt][0]表示子序列的最左边的值,Lval[rt][1]表示子序列最右边的值 Rlen[rt],Rval[rt][2]  表示rt所对应的区间[l,r]以r结尾的最长连续递增子序列的长度,

HDU 3308 LCIS(最长连续上升子序列)(线段树区间合并)

题意:给你n个整数,有两种操作,U A B把第A个数变成B,Q A B查询区间[A,B]的最长连续上升序列. 思路:还是查询和更新操作,而且也是询问区间中满足条件的连续最长区间 ,所以是线段树区间合并类型的题,通法是开三棵线段树,一个记录此区间内的LCIS的最长长度,一个记录从左边第一个数开始的LCIS长度,另一个记录从右边最后一个数结尾的LCIS长度.然后试图找到父亲与儿子关系维护的递推关系式就好 本题中的递推关系是: 1. 左儿子最右边的值 < 右儿子最左边的值 lmx = (左儿子的lmx

[LeetCode] Longest Consecutive Sequence 求最长连续序列

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

ALBB 找公共最长连续字母序列的长度

问题描写叙述 给定一个 query 和一个 text .均由小写字母组成.要求在 text 中找出以相同的顺序连续出如今 query 中的最长连续字母序列的长度. 比如, query为"acbac".text为"acaccbabb",那么text中的"cba"为最长的连续出如今query中的字母序列,因此,返回结果应该为其长度3.请注意程序效率. 代码思想 1.遍历两字符串的每个元素,遇见同样元素则计算该次同样次数同样元素数目.并与之前最大值比較

[LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p