UVa 507:jill Rides Again

Source: Internet
Author: User
Tags integer min printf requires

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=448

Type: max continuous and

Original title:

Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses the exc Ellent public bus system for part of her journey. She has a folding bicycle which she carries with her as she uses the bus for the ' the ' the ' the ' I her trip. When the bus reaches some pleasant part of the city, Jill gets off and rides her. She follows the bus route until she reaches her destination or she comes to a part of the "City she does". In the latter event she'll board the bus to finish her trips.

Through years of experience, Jill has rated each road to an integer scale of ' niceness. ' Positive niceness values indicate roads Jill likes; Negative values are used for roads she does don't like. There are not zero values. Jill plans where to leave the bus and start bicycling, as as a where to stop bicycling and re-join the Sum of niceness values of the roads she bicycles on is maximized. This is means that she'll sometimes cycle along a road she does not like and provided that it joins the up two other parts of her Journey involving roads she likes enough to compensate. It may being that no, the "route is" suitable for cycling, and Jill takes the bus for its entire route. Conversely, it may being that the whole route are so nice Jill would not have the bus at all.

Since There are 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 her identify the "best part to" cycle for each bus route.

Input

The input file contains information on several bus routes. The ' the ' of the ' 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 and on a line by itself. The number of stops is followed by s-1 lines, each line I () was an integer ni representing Jill ' s assessment o f The niceness of the road between the two stops I and i+1.

Output

For each route r in the input file, your program should identify the "Beginning bus stop I" and "Ending bus stop J" I Dentify the segment of the route which yields the maximal sum of niceness, m= ni+ni+1+...+nj-1. If more than one segment are maximally nice, choose the one with the longest cycle ride (largest j-i). To-break ties in longest maximal segments, choose the segment of that begins and the earliest stop (lowest i). For each route r in the input file, print a line in the form:

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The nicest part of Route R is between stops I and J

However, if the maximal sum is isn't positive, your program should print:

Route R has no nice parts

Sample Input

3
3
  -1
   6
   4
  -5 4-3 4 4
  -4
   4
  -5
4
  -2
  -3
  -4

Sample Output

The nicest part of Route 1 was between stops 2 and 3 the nicest part of
Route 2 is between stops 3 and 9
Route 3 ha s no nice parts

The main effect of the topic:

The title of the hype, simply speaking is a section of the bus road, each station for the 1,2,3...S, each station between the value of this section of the road is different, such as Station 1 to the station 2 goodwill is 5, station 3 to the station 4 goodwill is-3. To find a continuous station of the degree of goodwill and the maximum is how much.

Analysis and Summary:

Maximum continuity and problems, there are several algorithms, the fastest one of which is O (n), only need to scan once.

Suppose you use an array to arr the degree of goodwill, then set the sum[i]=arr[0]+arr[1]+. +arr[i]. namely Sum[i] is from Station 1 to station I of goodwill degree of the sum. In this case, if you want a section of the station's degree of goodwill, assuming the station 3 to station 5, only need O (1) Time: sum[5]-sum[2] is the answer.

To make sum[i]-sum[j] bigger, it's going to make sum[j smaller.

When you need to pay attention to the problem, the answer requires the highest degree of continuous and interval length the longer the better, the better the starting point as soon as possible.

Code:

* * * Uva:507-jill rides Again * time:0.120s * author:d_double/#include <iostream> #include <cstd  
io> #include <cstring> #include <algorithm> #define MAXN 20005 using namespace std;  
int b,s;  
      
int ARR[MAXN];  
    int main () {scanf ("%d", &b);  
        for (int t=1; t<=b; ++t) {scanf ("%d", &s);  
        arr[0]=0;  
            for (int i=1; i<=s-1; ++i) {scanf ("%d", &arr[i]);  
        Arr[i] + = arr[i-1];  
      
        int min=0, maxsum=-2147483646, left=0, right=0;  
            for (int i=1; i<=s-1; ++i) {int t=arr[i]-arr[min];  
            if (t>maxsum) {maxsum=t; right=i; left=min;} else if (t==maxsum) {//This particular attention should be paid to!!!   
                Because the problem requires the longer the interval the better if (Min==left) right=i;  
            else if (I-min > Right-left) right=i, left=min; } if (Arr[i]<arr[min]) {min=i}  
        } if (maxsum>0) {printf ("the nicest part of route%d is between stops%d and%  
        D\n ", T, Left+1, right+1);  
        else{printf ("Route%d has no nice parts\n", t);  
} return 0; }

Attached: Upgrade version of this algorithm: UVa 108-maximum Sum

Author: csdn Blog shuangde800

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.