Ultraviolet A 507 Jill Rides Again (the largest and most problematic continuous subsequences)

Source: Internet
Author: User

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 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 shocould 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 = ni + 1 +... + nj-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 shocould 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, A positive value indicates that he prefers to ride a bicycle, and a negative value indicates that he does not like to take a bus. Now ask the longest road for this bike ride.
Thought: in fact, it is the greatest sum of consecutive subsequences... However, this question must output the start and end points ..


Then .. Note: if there are the largest and the same, the maximum length of the output sequence is required. If the sequence length is equal .. The minimum output starting point is required .. Fxck has wa several times because of this pitfall.


Code:


 

#include <stdio.h>#include <string.h>#include <limits.h>int t, n, sum, value[20005], Max, start, go, end, d;int main() {    scanf("%d", &t);    int No = 1;    while (t --) {scanf("%d", &n);Max = -INT_MAX; sum = 0; start = 1, d = 0;for (int i = 1; i < n; i ++) {    scanf("%d", &value[i]);    if (sum >= 0)sum += value[i];    else {start = i;sum = value[i];    }    if (Max <= sum) {if ((Max == sum && i + 1 - start > d) || Max < sum) {    d = i + 1 - start;    go = start;    end = i + 1;}Max = sum;    }}if (Max > 0)     printf("The nicest part of route %d is between stops %d and %d\n", No ++, go, end);else    printf("Route %d has no nice parts\n", No ++);    }    return 0;}

 

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.