单调队列板子

蒟蒻自己写的垃圾板子,希望大牛指出板子的不足之处

#include <iostream>
#include <utility>
#define maxn 1000005

typedef long long ll;

using namespace std;

ll a[maxn];
pair<ll,ll> q[maxn];
ll n,m,head,tail;
void pushmax(pair<ll,ll> x)
{
    if(x.second >= q[head].second)
    {
        head = x.first;
        tail = x.first;
        q[head] = x;
        return ;
    }
    else
    {
        while(x.second >= q[tail].second)
        {
            tail--;
        }
        tail++;
        q[tail] = x;
    }
}

void pushmin(pair<ll,ll> x)
{
    if(x.second <= q[head].second)
    {
        head = x.first;
        tail = x.first;
        q[head] = x;
        return ;
    }
    else
    {
        while(x.second <= q[tail].second)
        {
            tail--;
        }
        tail++;
        q[tail] = x;
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    pair<ll,ll> x;
    ll n,m;
    cin >> n >> m;

    head = 1,tail = 1;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
    }

    q[head].first = 1;
    q[head].second = a[1];
    for(int i = 1; i <= n; i ++)
    {
        x.first = i;
        x.second = a[i];
        pushmin(x);
        if(i > q[head].first + m - 1)head++;
        if(i >= m)cout << q[head].second << " ";
    }
    cout << endl;

    head = 1,tail = 1;
    q[head].first = 1;
    q[head].second = a[1];
    for(int i = 1; i <= n; i ++)
    {
        x.first = i;
        x.second = a[i];
        pushmax(x);
        if(i > q[head].first + m - 1)head++;
        if(i >= m)cout << q[head].second << " ";
    }
    cout << endl;

    return 0;
}

#include <iostream>
#include <cstdio>
#include <queue>
#include <utility>
#define maxn 1000005

typedef long long ll;

using namespace std;

ll a[maxn],n,m;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    //cin >> n >> m;
    scanf("%lld%lld",&n,&m);

    for(int i = 1; i <= n; i ++)
    {
        scanf("%lld",&a[i]);
    }

    deque<pair<ll,ll> > q1,q2;
    q1.push_front(make_pair(1,a[1]));
    for(int i = 1; i <= n; i ++)
    {
        if(a[i] <= q1.front().second)
        {
            while(!q1.empty())q1.pop_front();
            q1.push_front(make_pair(i,a[i]));
        }
        else
        {
            while(a[i] <= q1.back().second)
            {
                q1.pop_back();
            }
            q1.push_back(make_pair(i,a[i]));
        }
        if(i > q1.front().first + m - 1)q1.pop_front();
        if(i >= m)printf("%lld ", q1.front().second);
    }
    printf("\n");

    q2.push_front(make_pair(1,a[1]));
    for(int i = 1; i <= n; i ++)
    {
        if(a[i] >= q2.front().second)
        {
        	while(!q2.empty())q2.pop_front();
            q2.push_front(make_pair(i,a[i]));
        }
        else
        {
            while(a[i] >= q2.back().second)
            {
                q2.pop_back();
            }
            q2.push_back(make_pair(i,a[i]));
        }
        if(i > q2.front().first + m - 1)q2.pop_front();
        if(i >= m)printf("%lld ", q2.front().second);
    }
    printf("\n");

    return 0;
}
#include <iostream>
#include <utility> 
#include <queue>
#include <deque> 

using namespace std;
 
typedef long long ll;

typedef long double ld;
 
const ll maxn = 1e6 + 5;

const ll mod = 1e9 + 7;

deque<pair<ll,ll> > q1,q2;
ll a[maxn];
int main() 
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	
	ll n,m;
	cin >> n >> m;
	
	for(int i = 1; i <= n; i ++)
	{
		cin >> a[i];
	} 
	
	for(int i = 1; i <= n; i ++)
	{
		while(!q1.empty() && q1.back().second >= a[i])
		{
			q1.pop_back();
		}
		q1.push_back(make_pair(i,a[i]));
		if(i > q1.front().first + m - 1)q1.pop_front();
		if(i >= m)cout << q1.front().second << ' ';
	}
	cout << '\n';
	
	for(int i = 1; i <= n; i ++)
	{
		while(!q2.empty() && q2.back().second <= a[i])
		{
			q2.pop_back();
		}
		q2.push_back(make_pair(i,a[i]));
		if(i > q2.front().first + m - 1)q2.pop_front();
		if(i >= m)cout << q2.front().second << ' ';
	}
	cout << '\n';
	
	
	return 0;
}
	

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值