当前位置:网站首页>(codeforce319) b.psychos in a line (monotone stack)
(codeforce319) b.psychos in a line (monotone stack)
2022-07-18 08:53:00 【AC__ dream】
Topic link :Problem - 319B - Codeforces

The sample input :
10
10 9 7 8 6 5 3 4 2 1
Sample output :
2
Simplify the question : Yes n Individual numbers are 1~n, There is a gun in every hand , Aim at the person next to you on the right , If your number is greater than the number of the adjacent person on the right , Just shoot the person next to you on your right , All people shoot at the same time , One shot counts as one round , If and only if everyone can't shoot the person next to him on his right , Ask how many rounds the game ends at most .
analysis : Let's think about a question first , That is the first. i When does a person die , The first thing to be sure of is If the first i If a person dies, he must have been killed by someone on his left with a larger number , When the first i When a person has a larger number than the person on his left who is close to him, he must die later than the person on his left , because Only his left numbered people smaller than him are dead , Only on his left can he become a person with a larger number , When all the people smaller than him on his left died , Then it must be the turn of i Personal , And he died in the next round after the death of someone younger than him . So we can use f[i] Record No. i The cycle of personal death , If and only if i Only when a person doesn't die f[i]=0, Then use monotone stack to simulate this process , All current elements pop All elements of die before the current element , When the elements smaller than their own number die, it's their turn , So we can pop Record the maximum value of a death round during the process , And those numbers corresponding to largest round of the death are separated by i Number and kill number i The last group of consecutive numbers among individuals decreases . Then the current element will die after the maximum , Finally, we only need to record the maximum value in the process of maintaining the monotone stack , Don't forget to put every element on the stack .
Here is the code :
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int N=1e5+10;
int a[N],f[N];
//a[i] Record No. i Personal number
//f[i] Record the game rounds of your death , Not dead marked as 0
int st[N],tt;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int ans=0;
for(int i=1;i<=n;i++)
{
int t=0;
while(tt&&a[st[tt]]<a[i])
t=max(t,f[st[tt--]]);
if(!tt) st[++tt]=i,f[i]=0;
else st[++tt]=i,f[i]=t+1;
ans=max(ans,f[i]);
}
cout<<ans;
return 0;
}
边栏推荐
- Top level planning scheme of "smart forest and grass" in Heilongjiang
- Real time user session tracking using eventlog analyzer
- MySQL reports an error mysqld:sort aborted:server shutdown in progress reason
- SQL杂谈
- Da Vinci pro's FPGA learning notes 6.2 -- VCs and Verdi development hummingbird e203
- 【集训DAY4】 Card game【贪心】
- QT (II) UI control introduction and optional tree control demonstration
- 九联科技开发板正式合入OpenHarmony主干
- 90% of people have never used Microsoft!
- DNS attack protection principle
猜你喜欢

(2021牛客多校五)B-Boxes(概率期望)

【集训DAY1】Spy dispatch【最小生成树】

Online sql to yaml tool

Modifying background photos and using skills of idea

HMS core graphics and image technology shows the latest functions and application scenarios, and accelerates the construction of digital intelligence life

关于MySQL的基础学习

【Luogu_P5431】【模板】乘法逆元 2【数论】

How to open slow query log in docker MySQL container

这个国产编辑器,即将开源!

(2021 Niuke multi school V) B-boxes (probability expectation)
随机推荐
About March 2022, apt-c-41 disguised as winrar Exe attack terminal side emergency response troubleshooting point
2022.7.11~8.1纪中游记
(2021牛客多校五)B-Boxes(概率期望)
uniapp自定义头部组件
Use honeypots to counter the blue team
Real time user session tracking using eventlog analyzer
Huawei's general card identification function enables multiple card bindings with one key
[in depth study of 4g/5g/6g topic -36]: urllc-7 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -1- business scenarios, evolution routes and performa
同花顺开户安全吗,同属顺是证券公司吗?
(codeforce1699) a & B (construction)
【集训DAY2】Sculpture【状压DP】
开发命令行工具
【集训DAY2】Torch bearer【暴力】【DFS】
电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
国内外知名的待办事项app
Develop command line tools
Honghu Wanlian Zhiyuan development board is officially integrated into the openharmony backbone
(2021 Niuke multi school III) j-counting triangles (thinking)
[2023 school recruitment questions] column planning (non final presentation status, for bloggers' personal reference)
无需训练代码,推理性能提升1.4~7.1倍,业界首个自动模型压缩工具开源!