Problem D. What a Beautiful Lake dp

Source: Internet
Author: User

Problem D. What a Beautiful Lake

Description

Weiming Lake, also named "Un-named Lake", is the most famous scenic spot in Peking University. It is located in the north of the campus and are surrounded by walking paths, small gardens, and old red buildings with Typ ical Chinese curly roofs. The lake was once the royal garden in Qing dynasty. Boya Tower stands on a little hill beside the lake. The lake and the tower form a distinctive landscape and a symbol of Peking University.

Weiming Lake is a very good place for studying, reading, skating in the winter, and of course, jogging. Students and teachers run or walk around weiming Lake every day and show how many paces they has covered in The mobile app WeChat Sports to get "Zans" (applauses).

Acmer X also enjoys jogging around weiming Lake. His watch can measure and record an altitude value every meter. After a round of running, X collected the altitude data around the lake. Now he wants to find out the longest slope around the lake.

Input

There is no more than test cases.

Each case has a lines.

The first line is a integer n (2 <= n <=) meaning that the length of the road around the lake is N meters.

The second line contains n integers a1,a2...an, (0<= a1,a2...an <=) indicating N altitude sample values around T He lake. The samples is given in clockwise order, and the distance between both adjacent samples is one meter. Of course the distance between A1 and an are also one meter.

The input ends by a line of 0.

Output

For each test case, print the length of the longest slope in meters. A slope is a part of the road around the lake, and it must keep going up or going down. If there is no slope, print 0.

Sample Input

4

1 1 1 1

8

5 1 2 3 4 5 6 2

6

5 4 3 2 1 2

10

1 0 2 3 2 2 3 4 3 2

0

Sample Output

0

5

4

4

Use Dp[i][0 or 1] to indicate whether it is smaller than the last, or larger, with A[i] as the maximum length of the end.

Because it can be looped, copy the array directly at the back, forming an array of 2 * n

If it's equal, skip right.

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>intN;Const intMAXN = 1e3 + -;intdp[maxn][2];intA[MAXN];voidWork () { for(inti =1; I <= N; ++i) {cin>>A[i]; }    intt =N;  for(inti =1; I <= N; ++i) {a[++T] =A[i]; } memset (DP,0,sizeofDP);  for(inti =2; I <= t; ++i) {if(A[i] = = A[i-1])Continue; if(A[i] > A[i-1]) {dp[i][1] = Dp[i-1][1] +1; } Elsedp[i][0] = Dp[i-1][0] +1; }    intAns =0;  for(inti =1; I <= t; ++i) {ans= Max (ans, dp[i][0]); Ans= Max (ans, dp[i][1]); } cout<< ans <<Endl;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endifIOS;  while(Cin >> N &&N) work (); return 0;}
View Code

Problem D. What a Beautiful Lake dp

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.