Ultraviolet A 507-Jill Rides Again

Source: Internet
Author: User


A person wants to ride a bicycle for a tour, but now there is an s-1 route, each of which has a value, indicating that he likes it and then he will ride a bicycle, A negative number indicates that he does not like bus. Now ask the longest road for this bike ride

Solution:
1. Train of Thought: DP, deformation of the maximum sub-segment and
2. Question output requirements: 1. If max is less than 0, output "Route % d has no nice parts \ n"; otherwise, output other results; 2. If multiple identical max values exist, the longest output sequence, if the longest, also has the smallest output start point.
3. Problem Solving Process Analysis: Calculate max based on the maximum sub-segment and idea. When updating max, pay attention to updating the start and end positions.

Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace std;
# Define maxn20010
 
Int t, s;
Int value [MAXN];
Long dp [MAXN];
Int start, end, len;
 
Void solve (int k ){
Long max;
Int tmpStart, tmpEnd, tmpLen;
Memset (dp, 0, sizeof (dp ));
Start = 1; end = 2; len = 1;
TmpStart = 1; tmpEnd = 2; tmpLen = 1;
Dp [1] = value [1]; max = dp [1];
For (int I = 2; I <s; I ++) {/* enumerate each vertex */
If (dp [I-1]> = 0) {/* if dp [I-1]> = 0 */
Dp [I] = dp [I-1] + value [I];
TmpEnd = I + 1; tmpLen ++;/* update tmpEnd and tmpLen */
If (max <dp [I]) {/* if max is smaller than dp [I], update max, start, end, And len */
Max = dp [I];
Start = tmpStart; end = tmpEnd;
Len = tmpLen;
}
When else if (max = dp [I]) {/* is equal, determine whether the current length of tmpLen is greater than len */
If (tmpLen> len ){
Start = tmpStart; end = tmpEnd;
Len = tmpLen;
}
}
}
Else {/* If dp [I-1] <0 */
Dp [I] = value [I]; tmpStart = I;
TmpEnd = I + 1; tmpLen = 1;/* update tmpEnd and tmpLen */
If (max <dp [I]) {/* if max <dp [I], update max, start, end, And len */
Max = dp [I]; len = tmpLen;
Start = tmpStart; end = tmpEnd;
}
}
}
Printf ("The nicest part of route % d is between stops % d and % d \ n", k, start, end );
}
 
Int main (){
// Freopen ("input.txt", "r", stdin );
Int I, k, flag;
Scanf ("% d % * c", & t );
For (k = 1; k <= t; k ++ ){
Scanf ("% d * c", & s); flag = 0;/* flag indicates whether a positive number exists */
For (I = 1; I <s; I ++ ){
Scanf ("% d * c", & value [I]);
If (value [I]> 0) flag = 1;
}
If (flag) solve (k );
Else printf ("Route % d has no nice parts \ n", k );
}
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.