What a Beautiful Lake

来源:互联网 发布:西安行知中学初中部 编辑:程序博客网 时间:2024/06/07 23:04

Problem D. What a Beautiful Lake

Description

Weiming Lake, also named "Un-named Lake", is the most famous scenicspot in Peking University. It is located in the north of the campus and issurrounded by walking paths, small gardens, and old red buildings with typicalChinese curly roofs. The lake was once the royal garden in Qing Dynasty. Boyatower stands on a little hill beside the lake. The lake and the tower form adistinctive landscape and a symbol of Peking University.

Weiming Lake is a very good place for studying, reading, skating in thewinter, and of course, jogging. More and more students and teachers run or walkaround Weiming Lake every day and show how many paces they have covered in themobile app WeChat Sports to get "Zans" (applauses).

ACMer X also enjoys jogging around Weiming Lake. His watch can measureand record an altitude value every meter. After a round of running, X collectedthe altitude data around the lake. Now he wants to find out the longest slopearound the lake.

Input

There are no more than 20 test cases.

Each case has two lines.

The first line is an integer N (2 <= N <= 100) meaning that thelength of the road around the lake is N meters.

The second line contains N integers a1,a2...aN,(0<= a1,a2...aN <= 100) indicating Naltitude sample values around the lake. The samples are given in clockwiseorder, and the distance between two adjacent samples is one meter. Of coursethe distance between a1 and aN is 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. Aslope is a part of the road around the lake, and it must keep going up or goingdown. If there are 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

SampleOutput

0

5

4

4

题意:在一个环中,求最长的斜坡长度。比较简单,线扫一遍即可。

Code:

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int a[10000];int main(){int n;scanf("%d",&n);while (n!=0)        {memset(a,0,sizeof(a));for (int i=1;i<=n;i++)scanf("%d",&a[i]);for (int i=1;i<=n-1;i++) a[i+n]=a[i];int m=2*n-1;int ans=0,mx=0;for (int i=2;i<=m;i++)                {if (a[i]>a[i-1]) ans++;else ans=0;if (ans>mx) mx=ans;}ans=0;for (int i=2;i<=m;i++)                {if (a[i]<a[i-1]) ans++;else ans=0;if (ans>mx) mx=ans;}printf("%d\n",mx);scanf("%d",&n);}return 0;}

原创粉丝点击