单调队列
详细介绍添加链接描述
其实就是维护一个严格单调的队列(或栈)
保证这个队列是单调的且队列中的元素的顺序和和之前的前后顺序保持一致
维护的时候:
如果新的元素比队尾的元素小(假如是单调增队列)那么一直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;
}