UVA 507 Jill Rides Again
Jill likes to ride hers bicycle, but since the pretty city of Greenhills where she lives have grown, Jill often uses the exc Ellent public bus system for part of her journey. She has a folding bicycle which she carries and she when she uses the bus for the first part of hers 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 isn't like. In the latter event she'll board the bus to finish hers trip.
Through years of experience, Jill have rated each road on a integer scale of ' niceness. ' Positive niceness values indicate roads Jill likes; Negative values is used for roads she does don't like. There is 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 Sum of niceness values of the roads she bicycles on is maximized. This means that she'll sometimes cycle along a road she does not like, provided that it joins up the other parts of her Journey involving roads she likes enough to compensate. It May, is, and no part of the suitable, the route is, cycling so, Jill takes the bus for its entire route. Conversely, it may be, the the whole route was so nice Jill won't use the bus at all.
Since There is many different bus routes, each with several stops at which Jill could leave or enter the bus, she feels t Hat a computer program could help she identify the best part-to-cycle for each bus route.
InputThe input file contains information on several bus routes. The first line of the file was a single integer
bRepresenting the number of the 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
N
IRepresenting Jill's assessment of the niceness of the road between the and the other stops
Iand
I+1.OutputFor each route
RIn the input file, your program should identify the beginning bus stop
IAnd the ending bus stop
JThat identify the segment of the route which yields the maximal sum of niceness, m= ni+n i+1+...+n j-1. If more than one segment are maximally nice, choose the one with the longest cycle ride (largest
J-
I). To broke ties in longest maximal segments, choose the segment that begins with the earliest stop (lowest
I). For each route
RIn the input file, print a line in the form:
The nicest part of the Route R is between stops I and J
However, if the maximal sum is not positive, your program should print:
Route R Have no nice parts
Sample Input
1 610 4 -5 4 -3 4 4 -4 4 -54 -2- 3 - 4
Sample Output
The nicest part of Route 1 is between stops 2 and 3The nicest part of Route 2 is between stops 3 and 9Route 3 have no nice Parts
Main topic: Each group of samples including two parts: 1) The number of points n
2) The value between n-1 points and points
The longest maximum continuous and.
problem-solving ideas: From the first point to the point between the value of the next plus, if and is positive, then there is "potential", continue to add, if and for negative, there is no "potential", and set to 0, modify the left edge, continue to add.
Potential: There is no possibility of getting a larger value in the future.
#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)) {//equality must be equal to the left boundary before the sum = temp is established; 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;}
UVA 507 Jill Rides Again (DP)