CF2108C Neo's Escape 题解

https://codeforces.com/problemset/problem/2108/C
思路很简单,其实就是要找数据中的山峰就可以了。原来的代码是之所以会错就是因为你在盆地到n的情况会被误判为山峰。所以最好的方法就是先去掉连续相同的部分。p[0]和p[n+1]分别设置成0,就可以了。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int T;
int n;
int a[N];
int p[N];
int find(int x)
{
	if(p[x]==x) return x;
	return p[x]=find(p[x]);
}
set<int> s;

int main()
{
	cin>>T;
	while(T--)
	{
		cin>>n;
		s.clear();
		int cnt=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			if(a[i]==a[i-1])
			{
				continue;
			}
			else
			{
				p[++cnt]=a[i];
			}
		}
		long long ans=0;
		p[0]=0;
		p[cnt+1]=0;
		for(int i=1;i<=cnt;i++)
		{
			if(p[i]>p[i-1]&&p[i]>p[i+1])
			{
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	
	
	
	
	
	return 0;	
} 
posted @ 2026-01-16 11:16  曾翎一  阅读(0)  评论(0)    收藏  举报