HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 单调队列优化
Posted ymzjj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 单调队列优化相关的知识,希望对你有一定的参考价值。
任意门: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
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.
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.
For each test case, you need to print a single line containing two integers A and B, where :
Note that ``⊕‘‘ denotes binary XOR operation.
提议概括:
给出 N 个数的序列,求第 i 个长度为 M 的子串里的最大值与 i 的异或值 之和, 第 i 个长度为 M 的子串求的最大值的比较次数 与 i 的异或值之和;
为了简化输入样例,只给出前 K 个数,K~N个数可根据公式 ai=(p×ai?1+q×i+r)modMOD 求出;
解题思路:
用一个单调队列从后面往前面扫,队列的大小就是 需要比较交换的次数,队尾元素就是最大值。
AC code:
1 #include <deque> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 using namespace std; 9 10 const int MAXN = 1e7+10; 11 struct data 12 { 13 LL value; 14 int no; 15 }; 16 deque<struct data>que; 17 18 LL num[MAXN]; 19 LL N, M, K; 20 LL p, q, r, MOD; 21 22 int main() 23 { 24 int T_case; 25 scanf("%d", &T_case); 26 while(T_case--){ 27 que.clear(); 28 scanf("%lld %lld %lld %lld %lld %lld %lld", &N, &M, &K, &p, &q, &r, &MOD); 29 for(LL i = 1; i <= K; i++){ 30 scanf("%lld", &num[i]); 31 } 32 33 if(K < N){ 34 for(int i = K+1; i <= N; i++){ 35 num[i] = (p*num[i-1]+q*i+r)%MOD; 36 } 37 } 38 data it; 39 for(LL i = (N-M+1); i <= N; i++){ 40 if(que.empty() || que.back().value < num[i]){ 41 it.value = num[i]; 42 it.no = i; 43 que.push_back(it); 44 } 45 } 46 /* 47 while(!que.empty()){ 48 printf("%lld ", que.front()); 49 que.pop_front(); 50 } 51 */ 52 LL id = (N-M)+1; 53 //printf("id:%lld ", id); 54 LL ans_A = (que.back().value^id); 55 LL ans_B = (que.size()^id); 56 for(LL i = (N-M); i >= 1; i--){ 57 if(que.back().no >= (i+M)) que.pop_back(); 58 while(que.front().value <= num[i] && !que.empty()) que.pop_front(); 59 it.value = num[i]; 60 it.no = i; 61 que.push_front(it); 62 id--; 63 ans_A += (que.back().value^id); 64 ans_B += (que.size()^id); 65 } 66 67 printf("%lld %lld ", ans_A, ans_B); 68 } 69 70 return 0; 71 }
以上是关于HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 单调队列优化的主要内容,如果未能解决你的问题,请参考以下文章
hdu 6299 Balanced Sequence( 2018 Multi-University Training Contest 1 )
HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 单调队列优化
hdu6365 2018 Multi-University Training Contest 6 1004 Shoot Game
HDU 6396 Swordsman --------2018 Multi-University Training Contest 7 (模拟+读入挂)
hdu 6319 Problem A. Ascending Rating (2018 Multi-University Training Contest 3)
HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)