UVA 507 Jill Rides Again (divided)

Source: Internet
Author: User

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: Divide and conquer, at the same time to obtain the maximum value of the record boundary (three cases: the longest-maximum continuous and can be left, right or beginning at the left end of the interval in the right interval). Note that a longer sequence is required when and when equal.



#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm>using namespace Std;int num[20005], n;int cal (int x, int y, int &l, int &r) {int Max = 0;if (y-x = = 1) {L = x, R = Y;return num[x ];} int mid = x + (y-x)/2; Rounded down int L1, L2, r1, r2;int a = Cal (X, Mid, L1, r1), B = cal (Mid, Y, L2, R2); if (a > B) {Max = A; L = L1, R = r1;} else if (a < b) {Max = b; L = L2, R = R2;} else {//If the same size takes longer max = A;if (r1-l1 >= r2-l2) {L = L1, R = r1;} else {L = L2, R = r2;}} int v = 0, L3 = mid-1, VL = num[mid-1];for (int i = mid-1; i >= x; i--) {//Check starting point in left interval, end of sequence in right interval v + = num[i];if (V & gt;= VL) {VL = V;l3 = i;}} int r3 = Mid, VR = Num[mid];v = 0;for (int i = mid; I < Y; i++) {v + = num[i];if (v >= vR) {VR = V;R3 = i;}} R3++;if (Max < VL + VR) {L = L3, R = R3;return VL + VR;} else if (max > VL + vR) {return max;} else {//equality when finding the longest sequence if (R3-l3 > r-l) {L = L3, R = R3;} else if (R3-l3 = = r-l && L3 < L) {L = L3, r = r3;} return Max;}} int main () {int T, case = 1;scanf ("%d", &t), while (t--) {scanf ("%d", &n), memset (num, 0, sizeof (NUM)), and for (int i = 1; I < n; i++) {scanf ("%d", &num[i]);} int F, e;int ans = cal (1, N, F, e); if (ans < 0) {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++, F, e);}} return 0;}


UVA 507 Jill Rides Again (divided)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.