Ultraviolet A 507 Jill Rides Again (DP)
Ultraviolet A 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 processing ing 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 using different bus routes, each with several stops at which Jill cocould leave or enter the bus, she feels that a computer program cocould 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
BRepresenting 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
S-1 lines, each line
I() Is an integer
N
IRepresenting Jill's assessment of the niceness of the road between the two stops
IAnd
I+ 1. Output For each route
RIn the input file, your program shocould 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 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
RIn the input file, print a line in the form:
The nicest part of routeRIs between stopsIAndJ
However, if the maximal sum is not positive, your program shocould print:
RouteRHas no nice parts
Sample Input
33 -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 has no nice parts
Example of each group includes two parts: 1) number of points n
2) value between n-1 points and points
Returns the maximum continuous sum.
Solution: add values between the first vertex and the vertex. If the sum is positive, it indicates that there is still "potential". If the sum is negative, if there is no "potential", set the sum to 0, modify the left boundary, and continue to add it later.
Potential: it is not possible to obtain a larger value if you add it later.
# Include
# Include
# Include
# Includeusing 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 )) {// when the values are equal, the left boundary must be equal before sum = temp; L = l + 1; R = I + 1 ;}} return sum ;}int main () is established () {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 ;}