UVa 507 Jill Rides Again (maximum continuous subsequence)

Source: Internet
Author: User

Jill Rides Again
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

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 Excellent 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.

Input

The input file contains information on several bus routes. The first line of the file was a single integer b representing 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 () are an integer ni repre Senting Jill's assessment of the niceness of the road between the, the 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 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 of the route R in the input file, print A is 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

Roughly test instructions: a route, every two sites between the scenery is not the same, then the corresponding charm value of the landscape is not the same, if the charm value is negative, it is the protagonist does not like the scenery, only more than 0 of the charm value is the protagonist like the scenery (excluding the case of 0), in this n hanging sites between the maximum charm value of And the output is between two sites (which can contain multiple sites).

Idea: The problem is to find the maximum continuous sub-sequence, output the largest site interval coordinates (in which two sites of the landscape charm value is the largest).
1#include <stdio.h>2#include <string.h>3 inta[20005];4 intMain ()5 {6     intT,n,i,max,sum,flag,left,right,count=1;7scanf"%d",&T);8      while(t--)9     {Tenscanf"%d",&n); One          for(i=1; i<n;i++) Ascanf"%d",&a[i]); -max=sum=a[1]; -flag=left=right=1; the          for(i=2; i<n;i++) -         { -sum+=A[i]; -             if(sum<0) +             { -sum=0; +flag=i; A             } at  -             if(Sum>max | | Sum==max && i-flag>right-Left ) -             { -max=sum; -left=Flag; -right=i; in             } -         } to         if(max<=0) +printf"Route%d have no nice parts\n", count++); -         Else theprintf"The nicest part of Route%d is between stops%d and%d\n", count++,left,right+1); *     } $     return 0;Panax Notoginseng}

UVa 507 Jill Rides Again (maximum continuous subsequence)

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.