Psychos in a Line CodeForces - 319B (单调栈的应用)
Psychos in a Line
There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step.
You're given the initial arrangement of the psychos in the line. Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment. Look notes to understand the statement more precise.
Input
The first line of input contains integer n denoting the number of psychos, (1 ≤ n ≤ 105). In the second line there will be a list of n space separated distinct integers each in range 1 to n, inclusive — ids of the psychos in the line from left to right.
Output
Print the number of steps, so that the line remains the same afterward.
Examples
Input
1010 9 7 8 6 5 3 4 2 1
Output
2
Input
61 2 3 4 5 6
Output
0
Note
In the first sample line of the psychos transforms as follows: [10 9 7 8 6 5 3 4 2 1] → [10 8 4] → [10]. So, there are two steps.
题意:
给定一个n个数的全排列,每一次操作,一个数字会消失,当且仅当它左边有数字,并且比它大。 问几次操作后数组将稳定不变。
思路:
用单调栈解决本问题,
我们栈中维护两个信息,一个是数字的大小,另一个维护这个数字会被第几次消灭,记为cnt。
以数字的大小维护严格递减的单调栈。
那么如果栈是空的,这个数设置成第0次被消灭,因为他永远不会被消灭,一直存在,所以设为0.
否则 如果栈顶元素小于当前元素,将其弹出,同时维护当前元素弹出的所有元素中,被第几次被消灭的最大值m。
那么加入这个元素时,cnt就为 m+1
如果没有弹出元素,就设置为1.
那么答案就是加入到栈中的所有cnt中的最大值。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
stack<pii> st;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin>>n;
repd(i,1,n)
{
cin>>a[i];
}
int ans=0;
repd(i,1,n)
{
if(st.empty())
{
st.push(mp(a[i],0));
}else
{
int m=0;
while(st.size()&&st.top().fi<=a[i])
{
m=max(m,st.top().se);
st.pop();
}
if(st.empty())
m=-1;
st.push(mp(a[i],m+1));
ans=max(ans,m+1);
}
}
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Psychos in a Line CodeForces - 319B (单调栈的应用)的更多相关文章
- CodeForces 548D 单调栈
Mike and Feet Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Subm ...
- Discrete Centrifugal Jumps CodeForces - 1407D 单调栈+dp
题意: 给你n个数hi,你刚开始在第1个数的位置,你需要跳到第n个数的位置. 1.对于i.j(i<j) 如果满足 max(hi+1,-,hj−1)<min(hi,hj) max(hi,hj ...
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...
- Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp
D. Psychos in a Line time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces 319B Psychos in a Line(模拟)
There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At eac ...
- Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈
B. Mike and Feet Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- Imbalanced Array CodeForces - 817D (思维+单调栈)
You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
随机推荐
- 【转载】android权限大全
1.android.permission.WRITE_USER_DICTIONARY 允许应用程序向用户词典中写入新词 2.android.permission.WRITE_SYNC_SETTINGS ...
- CentOS7+Python3.6利用web.py库进行微信公众平台服务器简易配置,token验证
1.安装配置Python CentOS7 自带 Python2.7, 我用的是Python3.6.5,需要CentOS7安装配置Python3并和Python2共存的,请看以下教程: CentOS7安 ...
- day38 各种队列、Event事件、协程、猴子补丁
1.各种队列 我们已经学习了队列这种存取值的方法,我们以前使用的队列是可以进行进程间通信的(IPC),但是今天学习的这两种队列是不能进行进程间通信的,只能进行线程间的通信 这两种队列分别是先进后出式队 ...
- golang结构体数组
转自: https://www.liaotaoo.cn/200.html package main import "fmt" type student struct{ id int ...
- Android性能专项分类
性能专项分类:1.资源消耗2.启动耗时3.主要页面加载时间4.内存泄漏.抖动5.卡顿.页面渲染 一.资源消耗:CPU.内存.流量.功耗-----1.查看CPU占用率:adb shell dumpsys ...
- 【Python】【demo实验9】【练习实例】【三数排序】
原题: 输入三个整数x,y,z,请把这三个数由小到大输出. 我的解法: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 输入三 ...
- COleVariant功能
COLeVariant是数据库常用到的数据类型.它可以是字符串,整型值,日期等.知道怎样将它转换为CString. COLeVariant类是对VARIANT结构的封装.它的构造函数具有极为强大的功能 ...
- Python【无引号、单引号、双引号、三引号】
无引号#数字和数学运算是标准化.有固定格式的>>> print(520) 520>>> print(1+1)2 单引号#文字却能够千变万化>>> ...
- gRPC 和 C#
前些天gRPC 发布1.0 版本,代表着gRPC 已经正式进入稳定阶段. 今天我们就来学习gRPC C# .而且目前也已经支持.NET Core 可以实现完美跨平台. 传统的.NET 可以通过Mono ...
- 移动站Web开发图片自适应两种常见情况解决方案
本文主要说的是Web中图片根据手机屏幕大小自适应居中显示,图片自适应两种常见情况解决方案.开始吧 在做配合手机客户端的Web wap页面时,发现文章对图片显示的需求有两种特别重要的情况,一是对于图集, ...