Lower
Problem Statement
There are N squares arranged in a row from left to right.
The height of the i-th square from the left is Hi.
You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.
Find the maximum number of times you can move.
Constraints
- All values in input are integers.
- 1≤N≤105
- 1≤Hi≤109
Input
Input is given from Standard Input in the following format:
N
H1 H2 … HN
Output
Print the maximum number of times you can move.
Sample Input 1
5
10 4 8 7 3
Sample Output 1
2
By landing on the third square from the left, you can move to the right twice.
Sample Input 2
7
4 4 5 6 6 5 5
Sample Output 2
3
By landing on the fourth square from the left, you can move to the right three times.
Sample Input 3
4
1 2 3 4
Sample Output 3
0
给一串数,当前面的数的值比后面的数大的时候,就更新为后面的那个,然后继续往后面比较,找出出现前面数比后面数大的次数最多的子段。
最开始想复杂了,非要记下标,还忘记break了,一直WA,结束后还不知道哪里错了,然后华超级大佬帮忙给找出来错误了,但是超时了,这种还记下标,简直弱爆了(我好sa)。
超时的代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int main()
{
int n;
int a[maxn];
while(cin >> n){
// cin >> n;
for(int i = 0;i < n;i ++){
cin >> a[i];
}
int cnt ,maxx=0;
for(int i = 0;i < n-1;i ++){
cnt = 0;
if(a[i] >= a[i+1] && a[i] > a[i-1]){
// cout << "dijige" << i << a[i] << endl;
int t = i;
// cout << t;
for(int j = t;j < n-1;j ++){
if(a[t] >= a[j+1] && (cnt+t) == j){
a[t] = a[j+1];
cnt ++;
}
else break;
}
maxx = max(cnt,maxx);
}else continue;
}
cout << maxx << endl;
}
return 0;
}
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int x,xx ;
cin >> xx;
int cnt = 0;
int maxx = -1;
for(int i = 1; i < n; i ++){
cin >> x;
if(x <= xx){
cnt ++;
xx = x;
}else{
maxx = max(maxx, cnt);
cnt = 0;
}
xx = x;
}
maxx = max(maxx,cnt);
cout << maxx << endl;
}