Problem A. Ascending Rating

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2884    Accepted Submission(s): 905

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=−1and 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×107 and ∑k≤2×106.

 
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 :

AB==∑i=1n−m+1(maxratingi⊕i)∑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
 
Source
 
  如果是正向推过去的话发现很难维护这个变化次数,倒着来的话,由于单调队列的特性,大的值进来会把之前小于它的值全部pop出去,所以单调队列内的元素个数就是变化次数了,逆着跑一边deque就好了。
  

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
#define pb push_back
#define pii pair<int,int>
deque<int>q;
int a[];
int main(){
LL n,i,j,k,m,P,Q,R,t,MOD;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld%lld%lld%lld%lld",&n,&m,&k,&P,&Q,&R,&MOD);
q.clear();
for(i=;i<=k;++i) scanf("%d",a+i);
for(i=k+;i<=n;++i)a[i]=(P*a[i-]+Q*i+R)%MOD;
LL ans1=,ans2=;
for(i=n;i>=;--i){
while(!q.empty()&&a[i]>=a[q.back()])q.pop_back();
while(!q.empty()&&q.front()>i+m-) q.pop_front();
q.push_back(i);
if(i>n-m+) continue;
ans1+=(a[q.front()]^i);
ans2+=(q.size()^i);
}
cout<<ans1<<' '<<ans2<<endl;
}
return ;
}
 

hdu-6319-单调队列的更多相关文章

  1. HDU 3507 单调队列 斜率优化

    斜率优化的模板题 给出n个数以及M,你可以将这些数划分成几个区间,每个区间的值是里面数的和的平方+M,问所有区间值总和最小是多少. 如果不考虑平方,那么我们显然可以使用队列维护单调性,优化DP的线性方 ...

  2. hdu 3530 单调队列最值

    /** HDU 3530 单调队列的应用 题意: 给定一段序列,求出最长的一段子序列使得该子序列中最大最小只差x满足m<=x<=k. 解题思路: 建立两个单调队列分别递增和递减维护(头尾删 ...

  3. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  4. hdu 3415(单调队列) Max Sum of Max-K-sub-sequence

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3415 大意是给出一个有n个数字的环状序列,让你求一个和最大的连续子序列.这个连续子序列的长度小于等于k. ...

  5. hdu 3401 单调队列优化+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  6. hdu 3415 单调队列

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU 2191 - 单调队列优化多重背包

    题目: 传送门呀传送门~ Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种 ...

  8. HDU 3530 单调队列

    题目大意:给你n个数, 让你问你最长的满足要求的区间有多长,区间要求:MAX - MIN >= m && MAX - MIN <= k 思路:单调队列维护递增和递减,在加入 ...

  9. HDU 4122 单调队列

    转载自:http://blog.csdn.net/lvshubao1314/article/details/46910271 DES :给出n个订单和m是商店的开放时间.然后n行给出n个订单的信息.然 ...

  10. HDU 3530Subsequence(单调队列)

    题意 题目链接 给出$n$个数,找出最长的区间,使得区间中最大数$-$最小数 $>= m$ 且$<= k$ Sol 考虑维护两个单调队列. 一个维护$1 - i$的最大值,一个维护$1 - ...

随机推荐

  1. C/C++之static函数与普通函数

    全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式.这两者在存储方式上并无不同.这两者的区别虽在于非静态全局变量 ...

  2. Linux 搭建FTP

    Linux 搭建FTP 步骤一:安装 vsftpd 1,运行以下命令安装 vsftpd. yum install -y vsftpd 出现下图表示安装成功. 2,打开etc/vsftpd cd /et ...

  3. CF#338D. GCD Table

    传送门 简单的中国剩余定理练习. 首先行数一定是$lcm$,然后只要确定最小的列数就能判定解合不合法了. 我们可以得到线性模方程组: $y \equiv 0 \pmod{a_1}$ $y+1 \equ ...

  4. noip 2012 提高组 day2 部分题解

    这道题有多种解法,我用的是扩展欧几里得算法求到的答案 #include<iostream> #include<fstream> #include<cstdio> u ...

  5. vijos 1360 八数码问题 - 启发式搜索

    背景 Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们. 描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0 ...

  6. SQLException: com.mchange.v2.c3p0.ComboPooledDataSource [ java.beans.IntrospectionException: java.lang.reflect.InvocationTargetException [numThreadsAwaitingCheckoutDefaultUser] ] has been closed()

    问题:Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mchange.v2.c3p0.Com ...

  7. sql 锁

    数据库中不使用锁时可能出现的问题: 1.更新丢失(Lost update) 两个事务都同时更新一行数据,但是第二个事务却中途失败退出,导致对数据的两个修改都失效了.这是因为系统没有执行任何的锁操作,因 ...

  8. poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解

    题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...

  9. HDU 1548 A strange lift (Dijkstra)

    https://vjudge.net/problem/HDU-1548 题意: 电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点 ...

  10. MVC ---- EF批处理

    #region 批处理 ///<summary> ///两增一删一改 ///</summary> public void Save(){ //新增参一 Parameter pa ...