首页 > 代码库 > Problem D. What a Beautiful Lake dp
Problem D. What a Beautiful Lake dp
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 is surrounded by walking paths, small gardens, and old red buildings with typical 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. More and more students and teachers run or walk around Weiming Lake every day and show how many paces they have 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 are no more than 20 test cases.
Each case has two lines.
The first line is an integer N (2 <= N <= 100) 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 <= 100) indicating N altitude sample values around the lake. The samples are given in clockwise order, and the distance between two adjacent samples is one meter. Of course the 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. A slope is a part of the road around the lake, and it must keep going up or going down. 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
Sample Output
0
5
4
4
用dp[i][0 or 1]表示是否比上一个小,或者大的时候,以a[i]为结尾的最大长度。
因为可以循环,所以就直接把数组复制一次在后面,形成2 * n的数组
如果相等就直接跳过好了。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> int n; const int maxn = 1e3 + 20; int dp[maxn][2]; int a[maxn]; void work() { for (int i = 1; i <= n; ++i) { cin >> a[i]; } int t = n; for (int i = 1; i <= n; ++i) { a[++t] = a[i]; } memset(dp, 0, sizeof dp); for (int i = 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; } else dp[i][0] = dp[i - 1][0] + 1; } int ans = 0; for (int i = 1; i <= t; ++i) { ans = max(ans, dp[i][0]); ans = max(ans, dp[i][1]); } cout << ans << endl; } int main() { #ifdef local freopen("data.txt","r",stdin); #endif IOS; while (cin >> n && n) work(); return 0; }
Problem D. What a Beautiful Lake dp