Ascending Rating (单调队列)

Problem Description

Before the start of contest, there are $n$ ICPC contestants waiting in a long queue. They are labeled by $1$ to $n$ from left to right. It can be easily found that the $i$-th contestant's QodeForces rating is $a_i$.
Little Q, the coach of Quailty Normal University, is bored to just watch them waiting in the queue. He starts to compare the rating of the contestants. He will pick a continous interval with length $m$, say $[l,l+m-1]$, and then inspect each contestant from left to right. Initially, he will write down two numbers $maxrating=-1$ and $count=0$. Everytime he meets a contestant $k$ with strictly higher rating than $maxrating$, he will change $maxrating$ to $a_k$ and $count$ to $count+1$.
Little T is also a coach waiting for the contest. He knows Little Q is not good at counting, so he is wondering what are the correct final value of $maxrating$ and $count$. Please write a program to figure out the answer.

Input

The first line of the input contains an integer $T(1\leq T\leq2000)$, denoting the number of test cases. In each test case, there are $7$ integers $n,m,k,p,q,r,MOD(1\leq m,k\leq n\leq 10^7,5\leq p,q,r,MOD\leq 10^9)$ in the first line, denoting the number of contestants, the length of interval, and the parameters $k,p,q,r,MOD$. In the next line, there are $k$ integers $a_1,a_2,...,a_k(0\leq a_i\leq 10^9)$, denoting the rating of the first $k$ contestants. To reduce the large input, we will use the following generator. The numbers $p,q,r$ and $MOD$ are given initially. The values $a_i(k<i\leq n)$ are then produced as follows : \begin{eqnarray*} a_i&=&(p\times a_{i-1}+q\times i+r)\bmod MOD \end{eqnarray*} It is guaranteed that $\sum n\leq 7\times 10^7$ and $\sum k\leq 2\times 10^6$.

Output

Since the output file may be very large, let's denote $maxrating_i$ and $count_i$ as the result of interval $[i,i+m-1]$. For each test case, you need to print a single line containing two integers $A$ and $B$, where : \begin{eqnarray*} A&=&\sum_{i=1}^{n-m+1} (maxrating_i\oplus i)\\ B&=&\sum_{i=1}^{n-m+1} (count_i\oplus i) \end{eqnarray*} Note that ``$\oplus$'' denotes binary XOR operation.

Sample Input

 

1 10 6 10 5 5 5 5 3 2 2 1 5 7 6 8 2 9

Sample Output

 

46 11

题目大意:给定一个长度为n的序列,现在问你每个区间长度为m的子序列最大值异或上i和最大值变化次数异或上i的求和。

思路方法:这道题用到一个新的知识点是单调队列,所谓的单调队列就是利用栈和队列来维护这个序列为有序的序列,从而确定子序列的最大或最小值。注意count的结果要将序列反过来求解,这样队列中元素个数就是count个数(倒的是递减正的就是递增所以每两个相邻都会让count加一,加上最开始0正好是队列个数),队头就是最大值。

单调队列的模板:

#include <iostream>
#include<cstdio>
using namespace std;
const int Max = 1000004;
int n,m,a[Max];//定义输入的三个量
int v[Max],h,t;
//数组v代表单调队列,存放数组a的下标,h代表队首,t代表队尾
void getmin()//单调递增
{
    h = 1;t = 0;//代表队列的头和尾

    for(int i = 1; i <= n; i++)
    {
        while(h <= t && a[v[t]] >= a[i]) t--;
        //如果要插入的数比队尾的数小,将对位的取代
        v[++t] = i;
        if(i>=m) //前m个数一定是在第一个m区间的
        {
            while(v[h] < i-m+1) h++;//此时的最小值不再m区间内,队首后移
            printf("%d ",a[v[h]]);//输出队首元素,即最小值
        }
    }
    cout << endl;
}
void getmax()//单调递减
{
    h = 1;t = 0;
    for(int i = 1; i < m; i++)
    {
        while(h <= t && a[v[t]] <= a[i]) t--;
        v[++t] = i;
    }
    for(int i = m; i <= n; i++)
    {
        while(h <= t && a[v[t]] <= a[i]) t--;
        v[++t] = i;
        if(i>=m) //前m个数一定是在第一个m区间的
        {
             while(v[h] < i-m+1) h++;
             printf("%d ",a[v[h]]);
        }
    }
    cout << endl;
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    getmin();
    getmax();
}

这道题代码:

#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;
}

猜你喜欢

转载自blog.csdn.net/pdsu_congshuang/article/details/81487970
今日推荐