杭电多校第三场 Ascending Rating 单调队列

问题 A: Ascending Rating

时间限制: 3 Sec  内存限制: 128 MB
提交: 23  解决: 10
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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 ai.
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=0 and count=0. Everytime he meets a contestant k with strictly higher rating than maxrating, he will change maxrating to ak 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.

输入

The first line of the input contains an integer T(1≤T≤2000), denoting the number of test cases.
In each test case, there are 7 integers n,m,k,p,q,r,MOD(1≤m,k≤n≤107,5≤p,q,r,MOD≤109) 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 a1,a2,...,ak(0≤ai≤109), 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 ai(k<i≤n) are then produced as follows :

It is guaranteed that ∑n≤7×107 and ∑k≤2×106.

输出

Since the output file may be very large, let's denote maxratingi and counti 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 :

Note that ``⊕'' denotes binary XOR operation.

样例输入

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

样例输出

46 11

[提交][状态]

从前到后在每m个区间内更新最大值和最大值变化次数,可以反向思考,从后向前扫,相当于下台阶

用单调队列来维护,时间复杂度O(n)

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=1e7+5;
const int INF=1e9;
int a[maxx];
int s[maxx];
int main()
{
    int t,n,m,k;
    int p,q,r,mod;
    scanf("%d",&t);
    while(t--){
        //memset(s,0,sizeof(s));
        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] = ( 1ll * p * a[i-1] + 1ll *  q * i + 1ll * r ) % mod;
        int head = 0,tail = -1;
        ll ma = 0,cnt = 0;
        for(int i=n; i>0; i--){
            while(head <= tail && a[i] >= a[ s[tail] ])  tail--;
            s[++tail]=i;
            while(s[head] > i+m-1)  head++;
            if(i <= n-m+1){
                ma += a[s[head]] ^ i;
                cnt += (tail-head+1) ^ i;
            }
        }
        cout<<ma<<" "<<cnt<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/renzijing/article/details/81454868