当前位置:网站首页>Consolidated figures

Consolidated figures

2022-06-13 04:17:00 csx_ zzh

The garlic gentleman got n Number , He wants to do the following with these numbers , Select the absolute value of the leftmost adjacent difference as 1 Two numbers of , Keep only small numbers , Delete the larger number , Until the absolute value of no two adjacent differences is 1 Number of numbers , Ask how many times you can do this at most ?

Input format
Enter the first line as an integer n(1≤n≤10^5), Represents the total number of numbers

Second behavior n It's an integer x1,x2,…,xn(0≤xi≤10^9), Represent these numbers .

Output format
Output one line , It's an integer , Indicates the maximum number of such operations that you can perform .

The sample input

4
1 2 0 1

Sample output

3
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
const int maxn=1e6+10;
stack<int> q; 
int main()
{
    
	int n;
	scanf("%d",&n);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
    
		int x;
		scanf("%d",&x);
		while(!q.empty()&&q.top()-x==1)// The front is bigger than the back , Delete the previous one , Compare it with the previous one  
		{
    
			q.pop();
			ans++;
		}
		if(!q.empty()&&x-q.top()==1)// The back is bigger than the front ,ans++ Select next comparison  
		{
    
			ans++;
		}
		else
		{
    
			q.push(x);
		}
	}
	cout<<ans<<endl; 
}
原网站

版权声明
本文为[csx_ zzh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280524207088.html