当前位置:网站首页>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;
}
边栏推荐
- Redis data persistence
- Cache read / write -- write
- Sword finger offer II 022 Entry node of a link in a linked list
- Among the four common technologies for UAV obstacle avoidance, why does Dajiang prefer binocular vision
- MCU: EEPROM multi byte read / write operation sequence
- Unity shader learning 004 shader debugging platform difference third-party debugging tools
- Interpretation and implementation of proxy mode
- 建模雜談系列143 數據處理、分析與决策系統開發的梳理
- Mongodb compass connects to the Alibaba cloud remote server database or reports an error occurred while loading instance info: command hostinfo req
- 10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects
猜你喜欢
120. 三角形最小路径和-动态规划
[test development] fundamentals of software testing
Redis data persistence
Reread the classic: end to end object detection with transformers
Single chip microcomputer: a/d differential input signal
How to use debounce in lodash to realize anti shake
dumi 搭建文檔型博客
[kubernetes series] pod chapter actual operation
VGA display based on de2-115 platform
Introduction and use of ES6
随机推荐
R: Employee turnover forecast practice
Introduction and use of ES6
Mongodb compass connects to the Alibaba cloud remote server database or reports an error occurred while loading instance info: command hostinfo req
[zeloengine] localization process /imgui Chinese culture
Goframe day 4
ROS中的msg消息
缓存读写--写
【ZeloEngine】本地化流程/ImGui中文化
Simple static web page + animation (small case)
Interpretation and implementation of proxy mode
Discussion sur la modélisation de la série 143
PAT 1054 The Dominant Color
Promise combined with await
EIA map making - data processing + map making
On the value of line height
MCU: NEC protocol infrared remote controller
[test development] advanced part - Classification of various test technologies
Lambda termination operation find and match nonematch
Difference between OKR and KPI
Lambda end operation find and match findany