UVa 507 Jill Rides Again (贪心&最大连续子串和)

本文深入探讨了信息技术领域的各个方面,包括前端开发、后端开发、移动开发、游戏开发等细分领域。从HTML、CSS、JavaScript等基础技术,到PHP、Python、Java等高级语言的应用,再到大数据开发、开发工具、嵌入式硬件等复杂场景,全面展示了信息技术的广阔前景。此外,文章还涵盖了音视频基础、音视频直播流媒体、图像处理AR特效、AI音视频处理等前沿技术,以及测试、基础运维、DevOps等实践层面的内容。通过丰富的实例和深入的分析,读者可以全面了解信息技术领域的最新趋势和发展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

507 - Jill Rides Again

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=448

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 when she uses the bus for the first part of her trip. When the bus reaches some pleasant part of the city, Jill gets off and rides her bicycle. She follows the bus route until she reaches her destination or she comes to a part of the city she does not like. In the latter event she will board the bus to finish her trip.


Through years of experience, Jill has rated each road on an integer scale of ``niceness.'' Positive niceness values indicate roads Jill likes; negative values are used for roads she does not like. There are not zero values. Jill plans where to leave the bus and start bicycling, as well as where to stop bicycling and re-join the bus, so that the sum of niceness values of the roads she bicycles on is maximized. This means that she will sometimes cycle along a road she does not like, provided that it joins up two other parts of her journey involving roads she likes enough to compensate. It may be that no part of the route is suitable for cycling so that Jill takes the bus for its entire route. Conversely, it may be that the whole route is so nice Jill will not use the bus at all.


Since there are many different bus routes, each with several stops at which Jill could leave or enter the bus, she feels that a computer program could help her identify the best part to cycle for each bus route.

Input 

The input file contains information on several bus routes. The first line of the file is a single integer  b representing the number of route descriptions in the file. The identifier for each route ( r ) is the sequence number within the data file,  $1 \le r \le b$ . Each route description begins with the number of stops on the route: an integer  s $2 \le s \le 20,000$  on a line by itself. The number of stops is followed by  s  - 1 lines, each line  i  (  $1 \le i < s$ ) is an integer  n i  representing Jill's assessment of the niceness of the road between the two stops  i  and  i +1.

Output 

For each route  r  in the input file, your program should identify the beginning bus stop  i  and the ending bus stop  j  that identify the segment of the route which yields the maximal sum of niceness, m= n i +n i+1 +...+n j-1 . If more than one segment is maximally nice, choose the one with the longest cycle ride (largest  j - i ). To break ties in longest maximal segments, choose the segment that begins with the earliest stop (lowest  i ). For each route  r  in the input file, print a line in the form:


The nicest part of route r is between stops i and j


However, if the maximal sum is not positive, your program should print:


Route r has no nice parts

Sample Input 

3
3
  -1
   6
10
   4
  -5
   4
  -3
   4
   4
  -4
   4
  -5
4
  -2
  -3
  -4

Sample Output 

The nicest part of route 1 is between stops 2 and 3
The nicest part of route 2 is between stops 3 and 9
Route 3 has no nice parts

这题的测试数据很弱,就算你不满足“If more than one segment is maximally nice, choose the one with the longest cycle ride (largest j-i)”也能AC。


但还是放上正确的代码:

/*0.116s*/

#include<cstdio>

int main(void)
{
	int T, cas = 0, n, i, val;
	int maxsum, sum, beg, tbeg, end, len;
	scanf("%d", &T);
	while (T--)
	{
		sum = maxsum = len = 0;
		tbeg = 1;
		scanf("%d", &n);
		for (i = 1; i < n; i++)
		{
			scanf("%d", &val);
			sum += val;
			if (sum < 0)
			{
				tbeg = i + 1;
				sum = 0;
			}
			else if (sum > maxsum || (sum == maxsum && i - tbeg > len))
			{
				len = i - tbeg;
				beg = tbeg;
				end = i;
				maxsum = sum;
			}
		}
		if (maxsum) printf("The nicest part of route %d is between stops %d and %d\n", ++cas, beg, end + 1);
		else printf("Route %d has no nice parts\n", ++cas);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值