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.

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

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 }

原文地址:https://www.cnblogs.com/ymzjj/p/10291226.html

时间: 2024-10-10 07:55:31

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

2018 Multi-University Training Contest 3 1001 / hdu6319 Problem A. Ascending Rating 单调队列,思维

Problem A. Ascending Rating 题意: 给定一个序列a[1..n],对于所有长度为m的连续子区间,求出区间的最大值以及从左往右扫描该区间时a的最大值的变化次数. 1≤m≤n≤107. Shortest judge solution: 534 bytes 题解: 官方题解:按照r从m到n的顺序很难解决这个问题.考虑按照r从n到m的顺序倒着求出每个区间的答案.按照滑窗最大值的经典方法维护a的单调队列,那么队列中的元素个数就是最大值的变化次数.时间复杂度O(n). 最大值好算,

hdu 4123 Bob’s Race (树的直径相关+rmq+单调队列思想)

Bob's Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2115    Accepted Submission(s): 658 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble

2014 Super Training #2 C Robotruck --单调队列优化DP

原题: UVA 1169  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3610 大白书上的原题. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algor

HDU 4122 Alice&#39;s mooncake shop 单调队列优化dp

Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4122 Description The Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest festival celebrated by Ch

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

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1536    Accepted Submission(s): 830 Problem Description Ther

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

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2150    Accepted Submission(s): 772Special Judge Problem De

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

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1363    Accepted Submission(s): 717 Problem Description Sudoku i

hdu 3401 单调队列优化+dp

http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5188    Accepted Submission(s): 1776 Problem Description Recently, lxhgww is addicted to stoc

HDU 4374 One hundred layer DP的单调队列优化

One hundred layer Problem Description Now there is a game called the new man down 100th floor. The rules of this game is: 1.  At first you are at the 1st floor. And the floor moves up. Of course you can choose which part you will stay in the first ti