【模板】单调队列

单调队列
详细介绍添加链接描述
其实就是维护一个严格单调的队列(或栈)
保证这个队列是单调的且队列中的元素的顺序和和之前的前后顺序保持一致
维护的时候:
如果新的元素比队尾的元素小(假如是单调增队列)那么一直pop,直到新的元素比队尾元素大(直接加入队尾)或者和队尾元素相等(不用操作,因为该元素已经在队列中了)。即让该队列不存在比新元素大的元素了。

题目链接https://vjudge.net/contest/364510#status/BBL/B/0/
HDU 4252

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int n;
int num[100010] = {0};
int head = 1;
int tail = 1;

int read()
{
	int x = 0, w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') w = -1;
		ch = getchar();
	}
	while(ch <= '9' && ch >= '0')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * w;
}

int main()
{
	int cnt = 0;
	while(scanf("%d", &n) != EOF)
	{
		int ans = 0;
		head = 1;
		tail = 1;
		while(n--)
		{
			int x = read();
			while(head < tail)
			{
				if(x < num[tail  -1])
				{
					tail --;
				}
				else break;
			}
			if(x != num[tail - 1])
			{
				num[tail] = x;
				tail ++;
				if(x != 0)
				{
					ans ++;
				}
			}
		}
		printf("Case %d: ", ++cnt);
		printf("%d\n", ans);
	}
	
	
	
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值