【CodeForces】[66B]Petya and Countryside

Problem Description

Loading...

Problem Analysis

The problem statement is a bit hard to understand
It is essentially finding the largest “mountain” shape
That is, a shape that is first non-decreasing and then non-increasing

Once you understand the problem,
the code is easy to write

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int a[1020];
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		scanf("%d",&a[0]);
		for(int i=1; i<n; i++)
			scanf("%d",&a[i]);
		int res=0;
		for(int i=0; i<n; i++) {
			int j;
			for(j=i+1; j<n&&a[j]>=a[j-1]; j++);
			while(j<n&&a[j]<=a[j-1])
				j++;
			res=res<j-i?j-i:res;
		}
		printf("%d\n",res);
	}
	return 0;
}

Problem link:【CodeForces】[66B]Petya and Countryside

Meaning:
Find the longest subarray in the given array that satisfies
first non-decreasing and then non-increasing
i.e., a shape like “1223221”

Solution process
Since we need a subarray that is first non-decreasing and then non-increasing
we can simulate this process
starting from each number and scanning forward
record the maximum length that can be reached
and thus update the maximum value

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...