首页 > 其他分享 >G. The Great Equalizer

G. The Great Equalizer

时间:2023-08-25 15:36:21浏览次数:37  
标签:Equalizer Great le 10 12 device array rightarrow

G. The Great Equalizer

Tema bought an old device with a small screen and a worn-out inscription "The Great Equalizer" on the side.

The seller said that the device needs to be given an array $a$ of integers as input, after which "The Great Equalizer" will work as follows:

  1. Sort the current array in non-decreasing order and remove duplicate elements leaving only one occurrence of each element.
  2. If the current length of the array is equal to $1$, the device stops working and outputs the single number in the array — output value of the device.
  3. Add an arithmetic progression {$n,\ n - 1,\ n - 2,\ \ldots,\ 1$} to the current array, where $n$ is the length of the current array. In other words, $n - i$ is added to the $i$-th element of the array, when indexed from zero.
  4. Go to the first step.

To test the operation of the device, Tema came up with a certain array of integers $a$, and then wanted to perform $q$ operations on the array $a$ of the following type:

  1. Assign the value $x$ ($1 \le x \le 10^9$) to the element $a_i$ ($1 \le i \le n$).
  2. Give the array $a$ as input to the device and find out the result of the device's operation, while the array $a$ remains unchanged during the operation of the device.

Help Tema find out the output values of the device after each operation.

Input

The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

Then follows the description of each test case.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the size of the array $a$ that Tema initially came up with.

The second line of each test case contains $n$ integers $a_1, a_2, a_3, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$.

The third line of a set contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of operations.

Each of the next $q$ lines of a test case contains two integers $i$ ($1 \le i \le n$) and $x$ ($1 \le x \le 10^9$) - the descriptions of the operations.

It is guaranteed that the sum of the values of $n$ and the sum of the values of $q$ for all test cases do not exceed $2 \cdot 10^5$.

Output

For each test case, output $q$ integers — the output values of the device after each operation.

Example

input

4
3
2 4 8
3
1 6
2 10
3 1
5
1 2 2 2 2
1
5 3
2
5 6
7
1 2
1 7
1 7
2 5
1 2
2 7
2 2
5
2 5 1 10 6
10
1 7
4 8
2 5
1 4
2 8
3 4
1 9
3 7
3 4
3 1

output

10 12 15 
4 
10 8 8 9 8 12 2 
14 12 12 11 11 10 11 10 11 14 

Note

Let's consider the first example of the input.

Initially, the array of numbers given as input to the device will be $[6, 4, 8]$. It will change as follows: $$[6, 4, 8] \rightarrow [4, 6, 8] \rightarrow [7, 8, 9] \rightarrow [10, 10, 10] \rightarrow [10]$$

Then, the array of numbers given as input to the device will be $[6, 10, 8]$. It will change as follows: $$[6, 10, 8] \rightarrow [6, 8, 10] \rightarrow [9, 10, 11] \rightarrow [12, 12, 12] \rightarrow [12]$$

The last array of numbers given as input to the device will be $[6, 10, 1]$. It will change as follows: $$[6, 10, 1] \rightarrow [1, 6, 10] \rightarrow [4, 8, 11] \rightarrow [7, 10, 12] \rightarrow [10, 12, 13] \rightarrow [13, 14, 14] \rightarrow [13, 14] \rightarrow [15, 15] \rightarrow [15]$$

 

解题思路

  需要观察出性质,一个数组在经过如若干次操作后得到的最终结果是什么。可以发现每次给数组加上$\{ n, n - 1, \ldots, 1 \}$后,元素间的相对大小关系没有发生变化,且相邻两个元素的差值会减小$1$。假设初始时数组排序后最大的差值是$x$,那么经过$x$次操作后数组就会剩下一个元素,又由于相对大小不变,因此数组中最大的元素(即最后一个位置的元素)每次操作都会加$1$,那么留下来的元素就会是初始数组排序后最大的元素加上$x$。

  因此对于每一个询问我们要动态维护数组在排序后相邻元素最大的差值,以及最大的元素是哪个,我们可以开两个$\text{std::multiset}$ $st_1$和$st_2$来分别维护。一开始直接把数组所有元素压入$st_1$,然后再把数组排序后所有相邻元素的差值压入$st_2$。

  对于每个询问,假设是把$a_x$改成$y$,那么可以理解为先删除$a_x$,再插入$y$。先在$st_1$中找到$a_x$的前驱$l$和后继$r$(如果存在的话),然后在$st_2$中删除$l$和$a_x$这两个相邻元素的差值,删除$a_x$和$r$这两个相邻元素的差值。由于$a_x$被删除,$l$和$r$变为相邻元素,因此在$st_2$中插入$l$和$r$的差值。接着由于要插入$y$,在$st_1$中找到$y$的前驱$l$和后继$r$(如果存在的话),然后在$st_2$中删除$l$和$r$这两个相邻元素的差值,再往$st_2$中插入$l$和$y$的差值,以及$y$和$r$的差值。这样就可以把信息维护好了,那么这一次询问的结果就是 *st1.rbegin() + *st2.rbegin() 。

  AC代码如下,时间复杂度为$O(q \cdot \log{n})$:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 int a[N];
 9 
10 void solve() {
11     int n, m;
12     scanf("%d", &n);
13     multiset<int> st1, st2;
14     for (int i = 1; i <= n; i++) {
15         scanf("%d", a + i);
16         st1.insert(a[i]);
17     }
18     for (auto it = next(st1.begin()); it != st1.end(); it++) {
19         st2.insert(*it - *prev(it));    // 插入相邻元素的差值 
20     }
21     scanf("%d", &m);
22     while (m--) {
23         int x, y;
24         scanf("%d %d", &x, &y);
25         auto it = st1.find(a[x]);    // 找到a[x]的迭代器 
26         if (it != st1.begin()) st2.erase(st2.find(*it - *prev(it)));    // 存在前驱,则删除a[x]与前驱的差值 
27         if (next(it) != st1.end()) st2.erase(st2.find(*next(it) - *it));    // 存在后继,则删除a[x]与后继的差值 
28         if (it != st1.begin() && next(it) != st1.end()) st2.insert(*next(it) - *prev(it));    // 同时存在前驱和后继,则前驱后继变成相邻元素,插入他们的差值 
29         st1.erase(it);    // 删除a[x] 
30         a[x] = y;
31         st1.insert(y);    // 插入y 
32         it = st1.find(y);    // 找到y的迭代器 
33         if (it != st1.begin() && next(it) != st1.end()) st2.erase(st2.find(*next(it) - *prev(it)));    // 同时存在前驱后继,前驱后继不再是相邻元素,因此删除他们的差值 
34         if (it != st1.begin()) st2.insert(*it - *prev(it));    // 存在前驱,则与前驱构成相邻元素,插入差值 
35         if (next(it) != st1.end()) st2.insert(*next(it) - *it);    // 存在后继,则与后继构成相邻元素,插入差值 
36         if (st2.empty()) printf("%d ", *st1.rbegin());    // 不存在差值,说明数组中只有一个元素 
37         else printf("%d ", *st1.rbegin() + *st2.rbegin());
38     }
39     printf("\n");
40 }
41 
42 int main() {
43     int t;
44     scanf("%d", &t);
45     while (t--) {
46         solve();
47     }
48     
49     return 0;
50 }

 

参考资料

  Codeforces Round #894 (Div.3) Editorial:https://codeforces.com/blog/entry/119715

标签:Equalizer,Great,le,10,12,device,array,rightarrow
From: https://www.cnblogs.com/onlyblues/p/17657055.html

相关文章

  • 活动 | 塑造软件新生态 赋能发展新变革——GreatSQL 受邀2023国际软博会
    塑造软件新生态,赋能发展新变革。8月31日-9月2日,第二十五届中国国际软件博览会将于天津梅江会展中心召开。本届软博会由中国电子信息行业联合会主办,聚焦全球软件前沿技术与产业发展方向,充分展示软件赋能数字经济、信息技术应用创新、工业互联网平台、智能制造及元宇宙等多领域发展......
  • 活动 | 塑造软件新生态 赋能发展新变革——GreatSQL 受邀2023国际软博会
    塑造软件新生态,赋能发展新变革。8月31日-9月2日,第二十五届中国国际软件博览会将于天津梅江会展中心召开。本届软博会由中国电子信息行业联合会主办,聚焦全球软件前沿技术与产业发展方向,充分展示软件赋能数字经济、信息技术应用创新、工业互联网平台、智能制造及元宇宙等多领域发展......
  • 探索GreatADM:图形化部署MGR的全新体验
    摘要:在DBA的日常工作中,快速部署数据库高可用架构,且标准化地入网部署数据库是一项重要的基础任务。本文将介绍常见的部署MGR的方式,并重点介绍万里数据库的GreatADM数据库管理平台进行图形化、可视化、标准化的部署过程,以提高交付效率和质量,给DBA提供一种全新的体验。(本文阅读大约......
  • Great Cow Gathering G
    GreatCowGatheringG思路换根dp,TreeDistancesI强化版,同样的先思考单个的,那么对于子树\(u\)对于每一个儿子\(v\)都有:\(f_u=f_v+sum_v*w_{u,v}\)其中\(sum\)是子树大小,而\(w\)则是边的长度,用这种方式可以求出以1为根的答案,然后考虑换根公式,首先要转移到的节点......
  • 图文结合丨带你轻松玩转MySQL Shell for GreatSQL
    一、引言1.1什么是MySQLShell?MySQLShell是MySQL的一个高级客户端和代码编辑器,是第二代MySQL客户端。第一代MySQL客户端即我们常用的MySQL。除了提供类似于MySQL的SQL功能外,MySQLShell还提供JavaScript和Python脚本功能,并包括与MySQL一起使用的API。......
  • GreatSQL从单机到MGR扩展纪实
    一、前言原有的业务系统跑在MySQL主从架构中,高可用通过脚本完成,但存在切换数据丢失和切换不及时风险,调研了高可用更稳定的MGR后,准备入手一试。本篇文章主要记录GreatSQL从单机扩展到MGR的详细过程,遇到的问题及解决方法。二、基础环境服务器角色如下IP端口主机名作用......
  • 数据质量管理工具预研——Griffin VS Deequ VS Great expectations VS Qualitis
    开源数据质量管理工具预研——GriffinVSDeequVSGreatexpectationsVSQualitis。概述 数据质量监控(DQC)是最近很火的一个话题,也是数据治理中最重要的一环。有一句话说得好。数据质量未必是数据治理中最重要的一部分,但是数据质量可能是让数据治理工作全部崩盘的第一步。所以......
  • GreatSQL通过错误日志信息判断数据库实例是如何关闭的
    背景概述在一次客户的数据库实例连接不上了,需要我们排查一下原因,通过查看数据库实例进程已经不存在了,在错误日志中没有发现其他报错信息,发现有shutdown的字样出现,怀疑是某个用户手动关闭了实例。我们通过以下测试,发现是由于用户关闭了主机所导致的。问题复现本次测试基于GreatS......
  • is greater than this module's compileSdkVersion (android-32). Dependency: an
    实现"isgreaterthanthismodule'scompileSdkVersion(android-32)"的步骤为了解决这个问题,我们需要按照以下步骤进行操作:步骤操作1确认项目的compileSdkVersion2更新项目的compileSdkVersion3更新相关依赖库的版本下面是每一步具体需要做的操作:步骤1......
  • python3使用pip安装wordcloud报错error: Microsoft Visual C++ 14.0 or greater is re
    背景:使用的是Anaconda集成环境,python版本是:3.10,安装wordcloud包,使用的命令是:pipinstallwordcloud,出现报错:error:MicrosoftVisualC++14.0orgreaterisrequired.Getitwith"MicrosoftC++BuildTools":https://visualstudio.microsoft.com/visual-cpp-build-tools/......