HDU 6319 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 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=−1 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.

 

 

Input

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 :

ai=(p×ai−1+q×i+r)modMOD

It is guaranteed that ∑n≤7×10^7 and ∑k≤2×10^6 .
 

Output

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 :

A==∑i=1n−m+1(maxratingi⊕i)

B==∑i=1n−m+1(counti⊕i)

Note that ``⊕ '' 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

题目大意:给你一个序列,一个长度k,要求找出队列从左至右每k个的最大值,和最大值更新的次数。为了避免输入数据过大,给出一个公式推导后面的数组元素,要求输出每个区间最大值与下标的异或值累加输出。(详情见公式)(因复制过来的题目公式略混乱,题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6319)java限时1s其他限时5s。

思路:

比赛的时候不会做,这道题,涉及到一个类似于模板的算法——滑窗最大问题。(算法详解:https://blog.csdn.net/gatieme/article/details/51915826

https://blog.csdn.net/gatieme/article/details/51915826)

可以考虑用队列,堆,数组来做。算法都是一样的。

标程代码:

 

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<stack>
#include<string.h>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<ext/rope>
#include<stdlib.h>
#include<vector>
#include<list>
using namespace std;
using namespace __gnu_cxx;
const int N=10000010;
int T,n,m,k,P,Q,R,MOD,i,a[N],q[N],h,t;long long A,B;
int main()
{
    scanf("%d",&T);
    while(T--)
	{
        scanf("%d%d%d%d%d%d%d",&n,&m,&k,&P,&Q,&R,&MOD);
        for(i=1;i<=k;i++) scanf("%d",&a[i]);
        for(i=k+1;i<=n;i++) a[i]=(1LL*P*a[i-1]+1LL*Q*i+R)%MOD;//由题目中的公式将数组补全 
        
		for(h=1,t=A=B=0,i=n;i;i--)
		{
            while(h<=t&&a[q[t]]<=a[i]) t--;//逆序比较 
            q[++t]=i;//存每个窗最大值得下标 
            if(i+m-1<=n)
		    {
                while(q[h]>=i+m) h++;
                A+=i^a[q[h]]; //printf("%d*",i);//题目中的公式二进制取异或累加 
                B+=i^(t-h+1);// 
                //printf("%d*",B);
            }
        }
        //for(i=1;i<n-m;i++) printf("%d*",q[i]);
        printf("%lld %lld\n",A,B);
    }
    return 0;
}

模板代码;

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<stack>
#include<string.h>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<ext/rope>
#include<stdlib.h>
#include<vector>
#include<list>
using namespace std;
using namespace __gnu_cxx;
const int maxn=100000100;
typedef long long ll;
int arr[maxn],p1[maxn],que[maxn],num[maxn];
int n,m,k,p,q,r,mod;
void getMax(int n,int k)
{
    int a=0,b=0;
    for(int i=1;i<=n;i++)
    {
        while(b>a&&arr[que[b-1]]<=arr[i])  b--;
        que[b++]=i;
        if(i<k) continue;
        while(que[a]<i-k+1)  a++;
        p1[i]=arr[que[a]];
        //printf("%d&",que[a]); 
    }
}//窗口最大 
void getNum(int n,int k)
{
    int a=0,b=0;
    for(int i=n;i>=1;i--)
    {
        while(b>a&&arr[que[b-1]]<=arr[i])  b--;
        que[b++]=i;
        if(i>n-k+1) continue;
        while(que[a]>i+k-1)  a++;
        num[i]=b-a;
    }
}//下标 
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d%d%d",&n,&m,&k,&p,&q,&r,&mod);
        for(int i=1;i<=k;i++) scanf("%d",&arr[i]);
        for(int i=k+1;i<=n;i++) arr[i]=((ll)p%mod*arr[i-1]%mod+(ll)q%mod*i%mod+r%mod)%mod;
        getMax(n,m);//获得最大 
        getNum(n,m);//获得下标 
        long long b=0,a=0;
        for(int i=1;i<=n-m+1;i++)
        {
            b=b+(num[i]^i);//printf("%d*",num[i]);
            a=a+(p1[i+m-1]^i);
        }
        printf("%lld %lld\n",a,b);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值