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
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.
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 :
It is guaranteed that ∑n≤7×107 and ∑k≤2×106.
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 :
Note that ``⊕'' denotes binary XOR operation.
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:
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\n", 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\n", ans_A, ans_B); 68 } 69 70 return 0; 71 }