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 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, . Each route description begins with the number of stops on the route:
an integer s, on a line by itself. The number of stops is followed by
s - 1 lines, each line i ( ) is an integer
ni 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

题目大意:每组样例包括两个部分:1)点的个数n

2)n-1 个点与点间的值

求最长最大连续和。

解题思路:从第一个点与点间的值开始往后加,若和为正,则说明还有“潜力”,继续往后加;若和为负,则没有“潜力”,将和置为0,修改左边界,继续往后加。

潜力:往后加还有没有可能获取更大的值。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int num[20005], n, L, R, l, r;
int cal() {
	int sum = 0, temp = 0;
	l = r = 0;
	for (int i = 1; i < n; i++) {
		temp += num[i];
		if (temp < 0) {
			temp = 0;
			l = i;
		}
		else if (temp > sum || (temp == sum && l == L - 1)) { //相等时必须要左边界相等,才成立
			sum = temp;
			L = l + 1;
			R = i + 1;
		}
	}
	return sum;
}
int main() {
	int T, Case = 1;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		memset(num, 0, sizeof(num));
		L = R = l = r = 0;
		for (int i = 1; i < n; i++) {
			scanf("%d", &num[i]);
		}
		int ans = cal();
		if (!ans) {
			printf("Route %d has no nice parts\n", Case++);
		}
		else {
			printf("The nicest part of route %d is between stops %d and %d\n", Case++, L, R);
		}
	}
	return 0;
}
时间: 2024-10-13 11:43:29

uva 507 Jill Rides Again (DP)的相关文章

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 (最大连续子序列)

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 10635 Prince and Princess(DP)

uva 10635 Prince and Princess(DP) In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, make p jumps and finally reach square n*n. He enters a

UVA 10564-Paths through the Hourglass(DP)

题目大意:给出一个沙漏,包含一个倒三角和一个三角,每个方格有一个0到9的数字,从顶上某格走到底下某格,求得到一个特殊值的路径有多少条,并输出字典序最小的.路径用一个起点和一系列'L'和'R'的字符表示. 用a[i][j]表示第i行第j列的数字.由于要求字典序最小的,所以从下往上递推.用d[i][j][u]表示在(i,j)且和为u的路径有多少条,在上半部分是倒三角,可以由d[i+1][j][u-a[i][j]]和d[i+1][j+1][u-a[i][j]]相加递推而来,在下半部分是正三角,可以由d

uva 10313 Pay the Price (DP)

uva 10313 Pay the Price 题目大意:现在有300种面额的硬币(1~300),给出一个金额数,问这300种面额的硬币组成该金额数的方式有多少种.注意:该题的输入有三种模式,1个数n:n为金额数:2个数n, a:n为金额数,a为硬币个数上限:3个数n, a,b:n为金额数,a b为硬币个数的下限和上限. 解题思路:dp[i][j]表示面额i的金额,在硬币数不超过j的情况下,有几种组成方式.这个题目涉及到一个结论,用不超过i个硬币凑出面值j的方案种数,是和用面值不超过i的硬币凑出

uva 10401 Injured Queen Problem(DP)

uva 10401 Injured Queen Problem 题目大意:这是一个变形的N皇后问题,皇后不再是占据一行一列以及斜线,她占据的只是她周围的一圈以及她所在的一列.题目给出一个含有问号,数字和字母的字符串.第i个字符是问号代表皇后在第i列的任意一行,若第i个字符是数字或字母X(1-F)代表皇后在第i列的X行.求满足该字符串的摆放方式的方法一共有几种. 解题思路:从第一列开始往后递推.dp[i][j]表示的是结合j - 1列的摆放方式,将皇后放在(i, j)的位置会有几种情况. #inc

Uva 437-The Tower of Babylon(DP)

题目链接:点击打开链接 DAG上最长路. 题意:给出n种长方体,(每种无限),要求能摞的最大高度.连边是大的连小的. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio&g

UVA 10891 Game of Sum(DP)

This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at

uva 10739 String to Palindrome (dp)

uva 10739 String to Palindrome In this problem you are asked to convert a string into a palindrome with minimum number of operations. The operations are described below: Here you'd have the ultimate freedom. You are allowed to: Add any character at a