HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

时间:2023-01-15 12:19:01

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319

Problem A. Ascending Rating

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

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
 

提议概括:

给出 N 个数的序列,求第 i 个长度为 M 的子串里的最大值与 i 的异或值 之和, 第 i 个长度为 M 的子串求的最大值的比较次数 与 i 的异或值之和;

为了简化输入样例,只给出前 K 个数,K~N个数可根据公式 ai=(p×ai−1+q×i+r)modMOD 求出;

解题思路:

用一个单调队列从后面往前面扫,队列的大小就是 需要比较交换的次数,队尾元素就是最大值。

AC code:

 #include <deque>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 1e7+;
struct data
{
LL value;
int no;
};
deque<struct data>que; LL num[MAXN];
LL N, M, K;
LL p, q, r, MOD; int main()
{
int T_case;
scanf("%d", &T_case);
while(T_case--){
que.clear();
scanf("%lld %lld %lld %lld %lld %lld %lld", &N, &M, &K, &p, &q, &r, &MOD);
for(LL i = ; i <= K; i++){
scanf("%lld", &num[i]);
} if(K < N){
for(int i = K+; i <= N; i++){
num[i] = (p*num[i-]+q*i+r)%MOD;
}
}
data it;
for(LL i = (N-M+); i <= N; i++){
if(que.empty() || que.back().value < num[i]){
it.value = num[i];
it.no = i;
que.push_back(it);
}
}
/*
while(!que.empty()){
printf("%lld ", que.front());
que.pop_front();
}
*/
LL id = (N-M)+;
//printf("id:%lld\n", id);
LL ans_A = (que.back().value^id);
LL ans_B = (que.size()^id);
for(LL i = (N-M); i >= ; i--){
if(que.back().no >= (i+M)) que.pop_back();
while(que.front().value <= num[i] && !que.empty()) que.pop_front();
it.value = num[i];
it.no = i;
que.push_front(it);
id--;
ans_A += (que.back().value^id);
ans_B += (que.size()^id);
} printf("%lld %lld\n", ans_A, ans_B);
} return ;
}

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】的更多相关文章

  1. 2018&period;10&period;29 bzoj1023&colon; &lbrack;SHOI2008&rsqb;cactus仙人掌图(仙人掌&plus;单调队列优化dp)

    传送门 求仙人掌的直径. 感觉不是很难. 分点在环上面和不在环上分类讨论. 不在环上直接树形dpdpdp. 然后如果在环上讨论一波. 首先对环的祖先有贡献的只有环上dfsdfsdfs序最小的点. 对答 ...

  2. 【单调队列优化dp】HDU 3401 Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...

  3. hdu 6319 Problem A&period; Ascending Rating &lpar;2018 Multi-University Training Contest 3 A&rpar;

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6319 思路: 单调队列倒着维护,队列里面剩下的值的数量就是这一段区间的count值,如样例第一个区间:3 ...

  4. 2018 Multi-University Training Contest 4 Problem J&period; Let Sudoku Rotate 【DFS&plus;剪枝&plus;矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

  5. 2018 Multi-University Training Contest 4 Problem K&period; Expression in Memories 【模拟】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...

  6. 2018 Multi-University Training Contest 4 Problem E&period; Matrix from Arrays 【打表&plus;二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  7. 2018 Multi-University Training Contest 4 Problem L&period; Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...

  8. 2018 Multi-University Training Contest 4 Problem B&period; Harvest of Apples 【莫队&plus;排列组合&plus;逆元预处理技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...

  9. 2018 Multi-University Training Contest 3 Problem F&period; Grab The Tree 【YY&plus;BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

随机推荐

  1. linux菜鸟日记&lpar;3&rpar;

    Centos7利用shell编辑一串 一键完成一些基础配置的代码: 在这串shell代码中我实现了  IP地址的配置.光盘的挂载.本地yum源的搭建.一些服务的安装例如 httpd. php. ntp ...

  2. ok6410按键中断编程&comma;linux按键裸机

    6410按键中断编程 一.流程分析 外部中断控制寄存器(s3c6410x  359页) 1.EINTxCONy: 外部中断组x的第y个控制器.这个就是设置中断的触发方式.有5种触发方式. 2.EINT ...

  3. js查看浏览器类型和版本

    var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; var scan; (s = ua.match(/msie ([\d. ...

  4. &lbrack;Redux&rsqb; Reducer Composition with combineReducers&lpar;&rpar;

    Previous, we do composition with objects: const todoApp = (state = {}, action) => { return { todo ...

  5. Android 动画的分类

    分为三类: View Animation (补间动画 Tween动画) Drawable Animation(帧动画 Frame动画) Property Animation(android 3.0引入 ...

  6. C&plus;&plus; 头文件系列&lpar;forward&lowbar;list&rpar;

    简介 forwrad_list字面意思为前向列表,但实际上它是一种单向列表,只能从单一方向遍历. 单向链表实现 forward_list内部是用单向列表实现的,并且设计该库的时候就是以近乎手写的单向链 ...

  7. iOS initWithFrame、initWithCoder、awakeFromNib的区别解析

    当我们需要自定义一个View控件时,会有 initWithFrame.initWithCoder.awakeFromNib 这三个系统方法,关于这三个方法何时调用,如何调用,有时候可能很多人会弄混淆. ...

  8. 总结切面编程AOP的注解式开发和XML式开发

    有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...

  9. js 属性增改删操作

    js 属性增改删操作,可参看菜鸟教程,这里记录一个小问题:disabled属性 使用setAttribute操作无法 禁用disabled属性,需使用removeAttribute操作,原因是只要有d ...

  10. Python开发——函数【Python内建函数】