1. 程式人生 > >HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【單調隊列優化】

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【單調隊列優化】

names scrip element lar meet per ont them ger

任意門: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(1T2000), denoting the number of test cases.
In each test case, there are 7 integers n,m,k,p,q,r,MOD(1m,kn107,5p,q,r,MOD109) 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(0ai109), 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<in) are then produced as follows :
ai=(p×ai?1+q×i+r)modMOD
It is guaranteed that n7×107 and k2×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(maxratingii) i=1n?m+1(countii)
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 2018 Multi-University Training Contest 3

提議概括:

給出 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 }

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【單調隊列優化】