HDU 6319 Problem A. Ascending Rating (单调队列)

【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=6319

题目意思

给定一个长度为n的序列,给出前k个值,后面根据公式自己算。现在问你每个区间长度为m的子序列最大值异或上i和最大值变化次数异或上i的求和。

解题思路

用单调队列来维护,只要把序列倒过来维护递减序列。这样队列中元素个数就是count个数(倒的是递减正的就是递增所以每两个相邻都会让count加一,加上最开始0正好是队列个数),队头就是最大值。
注意:求a[i]和最终值可能过大。

代码部分


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
#include <math.h>
#include <set>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
//const int mod = 1e9+7;
const int N = 1e7+7;
int n,m,k,q,p,r,mod;
int a[N];  
int qu[N];   ///单调栈 
LL ans,sum;
void  geti()
{
    int head,rear;  
    head = rear =0;
    for (int i = n; i >= 1; i--)
    {
        while (rear > head && a[qu[rear-1]] <= a[i])  ///存储单调递减数列   
            rear--; 
        qu[rear++] = i;
        if (i > n-m+1) continue;
        while (qu[head] > i+m-1) head++;  ///移到区间内 
        //printf("%d %d %d %d\n",rear,head,rear-head,a[qu[head]]);
        ans += (rear-head)^i;
        sum += a[qu[head]]^i;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        sum = ans =0;
        scanf("%d %d %d %d %d %d %d",&n,&m,&k,&p,&q,&r,&mod);
        for (int i = 1; i <= k; i++)
            scanf("%d",&a[i]);
        for (int i = k+1; i <= n; i++)
            a[i] = ((LL)p*a[i-1]%mod+(LL)q*i%mod+r%mod)%mod;
        geti();
        printf("%lld %lld\n",sum,ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值