【hdu 6319】Ascending Rating

本文介绍了一种使用单调队列解决滑动窗口中最大值及更新次数问题的算法。通过从后往前滑动窗口,并维护以窗口内值为元素的单调队列,可以高效地找出每个窗口的最大值及其更新次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【链接】 我是链接,点我呀:)
【题意】


给你一个长为n的数组a
让你对于每个长度为m的窗口。
算出其中的最大值以及从左往右遍历的时候这个最大值更新的次数。

【题解】


单调队列。
从后往前滑动窗口。
会发现我们维护以这个窗口里面的值为元素的单调队列的时候。
这个单调队列的长度就是最大值更新的次数。
因为我们把a[i]加入队列的时候。
队列尾巴上,小于等于这个a[i]的元素都会被删掉.
每个都这么做的话。
队列里面的元素就和更新时候的元素对应。
最大值就是队列的头部。
少做一些取余操作。不然会超时。

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
using namespace std;

namespace IO {
    const int MX = 4e7;
    char buf[MX]; int c, sz;
    void begin() {
        c = 0;
        sz = fread(buf, 1, MX, stdin);
    }
    inline bool read(int &t) {
        while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
        if(c >= sz) return false;
        bool flag = 0; if(buf[c] == '-') flag = 1, c++;
        for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
        if(flag) t = -t;
        return true;
    }
}

const int N = 1e7;

int n,m,k,p,q,r,MOD,a[N+10];
int max_queue[N+10],h,t;

int main(){
    //freopen("rush_in.txt", "r", stdin);
    int T;
    IO::begin();
    IO::read(T);
    while(T--){
        IO::read(n);IO::read(m);IO::read(k);IO::read(p);IO::read(q);IO::read(r);IO::read(MOD);
        rep1(i,1,k) IO::read(a[i]);
        rep1(i,k+1,n) a[i] = (1LL*p*a[i-1] + 1LL*q*i+r)%MOD;
        h = 1,t = 0;
        long long A=0,B=0;
        h = 1,t = 0;
        rep2(i,n,1)
        {
            if (i < n-m+1) while ( h<=t && max_queue[h]>i+m-1) h++;
            while (h<= t && a[max_queue[t]]<=a[i]) t--;
            t++;
            max_queue[t] = i;
            if (i <= n-m+1)
                {
                A = A + (a[max_queue[h]]^i);
                B = B + ((t-h+1)^i);
            }
        }
        printf("%lld %lld\n",A,B);
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/9394341.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值