Codeforces Round 894 (Div. 3)


A. Gift Carpet

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Recently, Tema and Vika celebrated Family Day. Their friend Arina gave them a carpet, which can be represented as an n ⋅ m n \cdot m nm table of lowercase Latin letters.

Vika hasn’t seen the gift yet, but Tema knows what kind of carpets she likes. Vika will like the carpet if she can read her name on. She reads column by column from left to right and chooses one or zero letters from current column.

Formally, the girl will like the carpet if it is possible to select four distinct columns in order from left to right such that the first column contains “v”, the second one contains “i”, the third one contains “k”, and the fourth one contains “a”.

Help Tema understand in advance whether Vika will like Arina’s gift.

Input

Each test consists of multiple test cases. The first line of input contains a single integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers n n n, m m m ( 1 ≤ n , m ≤ 20 1 \le n, m \le 20 1n,m20) — the sizes of the carpet.

The next n n n lines contain m m m lowercase Latin letters each, describing the given carpet.

Output

For each set of input data, output “YES” if Vika will like the carpet, otherwise output “NO”.

You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be accepted as a positive answer.

Example
input

5
1 4
vika
3 3
bad
car
pet
4 4
vvvv
iiii
kkkk
aaaa
4 4
vkak
iiai
avvk
viaa
4 7
vbickda
vbickda
vbickda
vbickda

output

YES
NO
YES
NO
YES

Note

In the first sample, Vika can read her name from left to right.

In the second sample, Vika cannot read the character “v”, so she will not like the carpet.

Tutorial
依次遍历每一列,按顺序查看vika 是否在这些列中,如果全部找到即为YES

Solution

import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n, m = map(int, input().split())
    a = [input().strip() for __ in range(n)]
    judge = ['v', 'i', 'k', 'a']
    k = 0
    flag = False
    for j in range(m):
        s = ""
        for i in range(n):
            s += a[i][j]
        if judge[k] in s:
            k += 1
        if k == 4:
            flag = True
            break
    print("YES" if flag else "NO")

B. Sequence Game

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Tema and Vika are playing the following game.

First, Vika comes up with a sequence of positive integers a a a of length m m m and writes it down on a piece of paper. Then she takes a new piece of paper and writes down the sequence b b b according to the following rule:

  • First, she writes down a 1 a_1 a1.
  • Then, she writes down only those a i a_i ai ( 2 ≤ i ≤ m 2 \le i \le m 2im) such that a i − 1 ≤ a i a_{i - 1} \le a_i ai1ai. Let the length of this sequence be denoted as n n n.

For example, from the sequence a = [ 4 , 3 , 2 , 6 , 3 , 3 ] a=[4, 3, 2, 6, 3, 3] a=[4,3,2,6,3,3], Vika will obtain the sequence b = [ 4 , 6 , 3 ] b=[4, 6, 3] b=[4,6,3].

She then gives the piece of paper with the sequence b b b to Tema. He, in turn, tries to guess the sequence a a a.

Tema considers winning in such a game highly unlikely, but still wants to find at least one sequence a a a that could have been originally chosen by Vika. Help him and output any such sequence.

Note that the length of the sequence you output should not exceed the input sequence length by more than two times.

Input

Each test consists of multiple test cases. The first line of input data contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the length of the sequence b b b.

The second line of each test case contains n n n integers b 1 , b 2 , b 3 , … , b n b_1, b_2, b_3, \dots, b_n b1,b2,b3,,bn ( 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1bi109) — the elements of the sequence.

The sum of the values of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output two lines. In the first line, output a single integer m m m — the length of the sequence ( n ≤ m ≤ 2 ⋅ n n \le m \le 2 \cdot n nm2n). In the second line, output m m m integers a 1 , a 2 , a 3 , … , a m a_1, a_2, a_3, \dots, a_m a1,a2,a3,,am ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) — the assumed sequence that Vika could have written on the first piece of paper.

If there are multiple suitable sequences, you can output any of them.

Example
input

6
3
4 6 3
3
1 2 3
5
1 7 9 5 7
1
144
2
1 1
5
1 2 2 1 1

output

6
4 3 2 6 3 3
3
1 2 3
6
1 7 9 3 5 7
1
144
2
1 1
6
1 2 2 1 1 1

Note

The first sample is explained in the problem statement.

In the second sample, Vika could have chosen the original sequence.

Tutorial
根据题意给出的 b i b_i bi 数组,若 b i b_i bi b i − 1 b_{i - 1} bi1 小,说明两者之间一定存在一个 x x x 使得 x ≤ b i x \le b_i xbi ,此时直接在中间添加一个 b i b_i bi 即可,反之直接添加在数组后方即可

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), ans;
    for (int &ai : a) {
        cin >> ai;
    }
    ans.emplace_back(a[0]);
    for (int i = 1; i < n; ++i) {
        if (a[i] >= ans.back()) {
            ans.emplace_back(a[i]);
        } else {
            ans.emplace_back(a[i]);
            ans.emplace_back(a[i]);
        }
    }
    int l = ans.size();
    cout << l << endl;
    for (int i = 0; i < l; ++i) {
        cout << ans[i] << " \n"[i == l - 1];
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

C. Flower City Fence

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Anya lives in the Flower City. By order of the city mayor, she has to build a fence for herself.

The fence consists of n n n planks, each with a height of a i a_i ai meters. According to the order, the heights of the planks must not increase. In other words, it is true that a i ≥ a j a_i \ge a_j aiaj for all i < j i < j i<j.

Anya became curious whether her fence is symmetrical with respect to the diagonal. In other words, will she get the same fence if she lays all the planks horizontally in the same order.

For example, for n = 5 n = 5 n=5, a = [ 5 , 4 , 3 , 2 , 1 ] a = [5, 4, 3, 2, 1] a=[5,4,3,2,1], the fence is symmetrical. Because if all the planks are laid horizontally, the fence will be [ 5 , 4 , 3 , 2 , 1 ] [5, 4, 3, 2, 1] [5,4,3,2,1], as shown in the diagram.

On the left is the fence [ 5 , 4 , 3 , 2 , 1 ] [5, 4, 3, 2, 1] [5,4,3,2,1], on the right is the same fence laid horizontally

But for n = 3 n = 3 n=3, a = [ 4 , 2 , 1 ] a = [4, 2, 1] a=[4,2,1], the fence is not symmetrical. Because if all the planks are laid horizontally, the fence will be [ 3 , 2 , 1 , 1 ] [3, 2, 1, 1] [3,2,1,1], as shown in the diagram.

On the left is the fence [ 4 , 2 , 1 ] [4, 2, 1] [4,2,1], on the right is the same fence laid horizontally

Help Anya and determine whether her fence is symmetrical.

Input

The first line of the input contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The description of the test cases follows.

The first line of a test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the length of the fence.

The second line of a test case contains n n n integers a 1 ≥ a 2 ≥ a 3 ≥ ⋯ ≥ a n a_1 \ge a_2 \ge a_3 \ge \dots \ge a_n a1a2a3an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) — the heights of the planks.

The sum of the values of n n n for all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output “YES” if the fence is symmetrical, otherwise output “NO”.

You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”, “Yes” and “YES” will be accepted as a positive answer.

Example
input

7
5
5 4 3 2 1
3
3 1 1
3
4 2 1
1
2
5
5 3 3 1 1
5
5 5 5 3 3
2
6 1

output

YES
YES
NO
NO
YES
YES
NO

Note

In the first and second test cases of the example, the fence is symmetrical.

In the third test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be [ 3 , 2 , 1 , 1 ] [3, 2, 1, 1] [3,2,1,1].

In the fourth test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be [ 1 , 1 ] [1, 1] [1,1].

In the fifth and sixth test cases of the example, the fence is symmetrical.

In the seventh test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be [ 2 , 1 , 1 , 1 , 1 , 1 ] [2, 1, 1, 1, 1, 1] [2,1,1,1,1,1].

Tutorial

画图可知,若想要条件成立,需满足:$ a_{a_i} \ge i $

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    bool flag = a[1] == n;
    for (int i = 2; i <= n and flag; ++i) {
        flag = a[a[i]] >= i;
    }
    cout << (flag ? "YES" : "NO") << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

D. Ice Cream Balls

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Tema decided to improve his ice cream making skills. He has already learned how to make ice cream in a cone using exactly two balls.

Before his ice cream obsession, Tema was interested in mathematics. Therefore, he is curious about the minimum number of balls he needs to have in order to make exactly n n n different types of ice cream.

There are plenty possible ice cream flavours: 1 , 2 , 3 , … 1, 2, 3, \dots 1,2,3,. Tema can make two-balls ice cream with any flavours (probably the same).

Two ice creams are considered different if their sets of ball flavours are different. For example, { 1 , 2 } = { 2 , 1 } \{1, 2\} = \{2, 1\} {1,2}={2,1}, but { 1 , 1 } ≠ { 1 , 2 } \{1, 1\} \neq \{1, 2\} {1,1}={1,2}.

For example, having the following ice cream balls: { 1 , 1 , 2 } \{1, 1, 2\} {1,1,2}, Tema can make only two types of ice cream: { 1 , 1 } \{1, 1\} {1,1} and { 1 , 2 } \{1, 2\} {1,2}.

Note, that Tema do not need to make all the ice cream cones at the same time. This means that he making ice cream cones independently. Also in order to make a following cone { x , x } \{x, x\} {x,x} for some x x x, Tema needs at least 2 2 2 balls of type x x x.

Help Tema answer this question. It can be shown that answer always exist.

Input

Each test consists of multiple test cases. The first line of input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 18 1 \le n \le 10^{18} 1n1018) — the number of ice cream types that Tema wants to make.

Output

For each test case, output a single integer — the minimum number of balls Tema needs to buy.

Example
input

5
1
3
6
179
1000000000000000000

output

2
3
4
27
2648956421

Note

In the first sample, it is enough to have following balls types: { 1 , 1 } \{1, 1\} {1,1}. Note, that set { 1 } \{1\} {1} if not enough since we need at least 2 2 2 balls of a type 1 1 1 in order to make such cone { 1 , 1 } \{1, 1\} {1,1}.

In the second sample, it is not possible to make it with 2 2 2 balls, but it can be done with these balls: { 1 , 2 , 3 } \{1, 2, 3\} {1,2,3}.

In the third sample, { 1 , 2 , 3 , 4 } \{1, 2, 3, 4\} {1,2,3,4} is optimal answer, so we can get following ice cream cones: { 1 , 2 } \{1, 2\} {1,2}, { 1 , 3 } \{1, 3\} {1,3}, { 1 , 4 } \{1, 4\} {1,4}, { 2 , 3 } \{2, 3\} {2,3}, { 2 , 4 } \{2, 4\} {2,4}, { 3 , 4 } \{3, 4\} {3,4}.

Tutorial

由题意可得,每新加入一个数就会有两种贡献度:

  • 这个口味没有出现过:贡献度就是加入这个数之前出现过的口味的个数

  • 这个口味已经出现过:因为原先已经有了这个口味,所以这个口味已经和其他出现过的口味组合过了,就只能和曾经出现的自己组成一个新的口味,此时贡献度为 1 1 1,并由此可知同一种口味最多出现两次,两次以上将没有贡献度

此时我们可以先用二分找出一个最大的口味数量,其中每个口味互不相同,总贡献度不超过 n n n,如果此时还差一些贡献度到达 n n n,只需要将原本的口味再次添加进去即可,那么会使某个口味出现三次吗?那答案当然是不会的,因为如果某个口味出现了三次,那么现存的口味一定都至少出现了两次,此时为何不直接添加一个新口味进去呢?

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n;
    cin >> n;
    int left = 1, right = 2e9;
    while (left + 1 < right) {
        int mid = (left + right) >> 1;
        if (mid * (mid - 1) / 2 <= n) {
            left = mid;
        } else {
            right = mid;
        }
    }
    cout << left + (n - left * (left - 1) / 2) << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

E. Kolya and Movie Theatre

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Recently, Kolya found out that a new movie theatre is going to be opened in his city soon, which will show a new movie every day for n n n days. So, on the day with the number 1 ≤ i ≤ n 1 \le i \le n 1in, the movie theatre will show the premiere of the i i i-th movie. Also, Kolya found out the schedule of the movies and assigned the entertainment value to each movie, denoted by a i a_i ai.

However, the longer Kolya stays without visiting a movie theatre, the larger the decrease in entertainment value of the next movie. That decrease is equivalent to d ⋅ c n t d \cdot cnt dcnt, where d d d is a predetermined value and c n t cnt cnt is the number of days since the last visit to the movie theatre. It is also known that Kolya managed to visit another movie theatre a day before the new one opened — the day with the number 0 0 0. So if we visit the movie theatre the first time on the day with the number i i i, then c n t cnt cnt — the number of days since the last visit to the movie theatre will be equal to i i i.

For example, if d = 2 d = 2 d=2 and a = [ 3 , 2 , 5 , 4 , 6 ] a = [3, 2, 5, 4, 6] a=[3,2,5,4,6], then by visiting movies with indices 1 1 1 and 3 3 3, c n t cnt cnt value for the day 1 1 1 will be equal to 1 − 0 = 1 1 - 0 = 1 10=1 and c n t cnt cnt value for the day 3 3 3 will be 3 − 1 = 2 3 - 1 = 2 31=2, so the total entertainment value of the movies will be a 1 − d ⋅ 1 + a 3 − d ⋅ 2 = 3 − 2 ⋅ 1 + 5 − 2 ⋅ 2 = 2 a_1 - d \cdot 1 + a_3 - d \cdot 2 = 3 - 2 \cdot 1 + 5 - 2 \cdot 2 = 2 a1d1+a3d2=321+522=2.

Unfortunately, Kolya only has time to visit at most m m m movies. Help him create a plan to visit the cinema in such a way that the total entertainment value of all the movies he visits is maximized.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers n n n, m m m, and d d d ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105, 1 ≤ m ≤ n 1 \le m \le n 1mn, 1 ≤ d ≤ 1 0 9 1 \le d \le 10^9 1d109).

The second line of each set of input data contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 109ai109) — the entertainment values of the movies.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the maximum total entertainment value that Kolya can get.

Example
input

6
5 2 2
3 2 5 4 6
4 3 2
1 1 1 1
6 6 6
-82 45 1 -77 39 11
5 2 2
3 2 5 4 8
2 1 1
-1 2
6 3 2
-8 8 -2 -1 9 0

output

2
0
60
3
0
7

Note

The first test case is explained in the problem statement.

In the second test case, it is optimal not to visit any movies.

In the third test case, it is optimal to visit movies with numbers 2 2 2, 3 3 3, 5 5 5, 6 6 6, so the total entertainment value of the visited movies will be 45 − 6 ⋅ 2 + 1 − 6 ⋅ 1 + 39 − 6 ⋅ 2 + 11 − 6 ⋅ 1 = 60 45 - 6 \cdot 2 + 1 - 6 \cdot 1 + 39 - 6 \cdot 2 + 11 - 6 \cdot 1 = 60 4562+161+3962+1161=60.

Tutorial

由题意可知,不去电影院的时间更长,下一步电影的娱乐价值就会下降 d ⋅ n d · n dn,其实我们可以仔细分析题意可知,看完 m m m 个电影后,下降的娱乐价值总和就是 d ⋅ i d x d · idx didx,其中 i d x idx idx 为最后一场电影所在的天数

此时我们只需要维护一个优先队列即可解决这个问题,先将娱乐价值大于 0 的电影添加到队列中,如果此时队列的长度超过了 m m m,此时就将娱乐价值最小的电影剔除,保证自己所获得的娱乐价值最大化,令 s u m sum sum 为队列里留下的电影娱乐价值总和, i i i 为看的最后一部电影所在的天数,所以最后可以获得的最大娱乐价值就是 s u m − d ⋅ i sum - d · i sumdi

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n, m, d, ans = 0, cnt = 0;
    cin >> n >> m >> d;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    priority_queue<int, vector<int>, greater<int>> q;
    for (int i = 1; i <= n; ++i) {
        if (a[i] > 0) {
            q.emplace(a[i]);
            cnt += a[i];
            while (q.size() > m) {
                cnt -= q.top();
                q.pop();
            }
            ans = max(ans, cnt - d * i);
        }
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

F. Magic Will Save the World

time limit per test: 4 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

A portal of dark forces has opened at the border of worlds, and now the whole world is under a terrible threat. To close the portal and save the world, you need to defeat n n n monsters that emerge from the portal one after another.

Only the sorceress Vika can handle this. She possesses two magical powers — water magic and fire magic. In one second, Vika can generate w w w units of water mana and f f f units of fire mana. She will need mana to cast spells. Initially Vika have 0 0 0 units of water mana and 0 0 0 units of fire mana.

Each of the n n n monsters that emerge from the portal has its own strength, expressed as a positive integer. To defeat the i i i-th monster with strength s i s_i si, Vika needs to cast a water spell or a fire spell of at least the same strength. In other words, Vika can spend at least s i s_i si units of water mana on a water spell, or at least s i s_i si units of fire mana on a fire spell.

Vika can create and cast spells instantly. Vika can cast an unlimited number of spells every second as long she has enough mana for that.

The sorceress wants to save the world as quickly as possible, so tell her how much time she will need.

Input

Each test consists of several test cases. The first line of each test contains a single integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains two integers w w w, f f f ( 1 ≤ w , f ≤ 1 0 9 1 \le w, f \le 10^9 1w,f109) — the amount of water and fire mana that Vika can generate per second.

The second line of each test case contains a single integer n n n ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100) — the number of monsters.

The third line of each test case contains n n n integers s 1 , s 2 , s 3 , … , s n s_1, s_2, s_3, \dots, s_n s1,s2,s3,,sn ( 1 ≤ s i ≤ 1 0 4 1 \le s_i \le 10^4 1si104) — the strengths of the monsters.

It is guaranteed that the sum of n n n over all test cases does not exceed 100 100 100.

Output

For each test case, output a single integer — the minimum time in seconds that Vika will need to defeat all the monsters.

Example
input

4
2 3
3
2 6 7
37 58
1
93
190 90
2
23 97
13 4
4
10 10 2 45

output

3
2
1
5

Note

In the first sample, after the first second, Vika can spend 2 2 2 units of fire mana to kill the first monster. Then she has 2 2 2 units of water mana and 1 1 1 unit of fire mana. After the third second, she will have 6 6 6 units of water mana and 7 7 7 units of fire mana at her disposal. This will be enough to immediately kill the second and third monsters.

Tutorial

本题其实就是一个背包DP问题

可以先假设女巫先攒够所有魔法值再去一次解决问题

我们可以假设用水魔法解决血量总和为 A A A 的怪物,用火魔法解决血量总和为 B B B 的怪物,此时可以使用DP数组维护所有可能的 A A A,再用总和算出 B B B,与此同时计算最小值

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int w, f, n, ans = 2e9, sum = 0;
    cin >> w >> f >> n;
    vector<int> a(n);
    for (int &ai : a) {
        cin >> ai;
        sum += ai;
    }
    vector<bool> dp(sum + 1, false);
    dp[0] = true;
    for (int i = 0; i < n; ++i) {
        for (int j = sum; j - a[i] >= 0; --j) {
            dp[j] = dp[j] or dp[j - a[i]];
        }
    }
    for (int i = 0; i <= sum; ++i) {
        if (dp[i]) {
            ans = min(ans, max((i + w - 1) / w, (sum - i + f - 1) / f));
        }
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

G. The Great Equalizer

time limit per test: 4 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

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 a 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 1 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 ,   … ,   1 n,\ n - 1,\ n - 2,\ \ldots,\ 1 n, n1, n2, , 1} to the current array, where n n n is the length of the current array. In other words, n − i n - i ni is added to the i i 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 a a, and then wanted to perform q q q operations on the array a a a of the following type:

  1. Assign the value x x x ( 1 ≤ x ≤ 1 0 9 1 \le x \le 10^9 1x109) to the element a i a_i ai ( 1 ≤ i ≤ n 1 \le i \le n 1in).
  2. Give the array a a a as input to the device and find out the result of the device’s operation, while the array a a 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 t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — 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 n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the size of the array a a a that Tema initially came up with.

The second line of each test case contains n n n integers a 1 , a 2 , a 3 , … , a n a_1, a_2, a_3, \ldots, a_n a1,a2,a3,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) — the elements of the array a a a.

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

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

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

Output

For each test case, output q q 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 ] [6, 4, 8] [6,4,8]. It will change as follows:

[ 6 , 4 , 8 ] → [ 4 , 6 , 8 ] → [ 7 , 8 , 9 ] → [ 10 , 10 , 10 ] → [ 10 ] [6, 4, 8] \rightarrow [4, 6, 8] \rightarrow [7, 8, 9] \rightarrow [10, 10, 10] \rightarrow [10] [6,4,8][4,6,8][7,8,9][10,10,10][10]

Then, the array of numbers given as input to the device will be [ 6 , 10 , 8 ] [6, 10, 8] [6,10,8]. It will change as follows:

[ 6 , 10 , 8 ] → [ 6 , 8 , 10 ] → [ 9 , 10 , 11 ] → [ 12 , 12 , 12 ] → [ 12 ] [6, 10, 8] \rightarrow [6, 8, 10] \rightarrow [9, 10, 11] \rightarrow [12, 12, 12] \rightarrow [12] [6,10,8][6,8,10][9,10,11][12,12,12][12]

The last array of numbers given as input to the device will be [ 6 , 10 , 1 ] [6, 10, 1] [6,10,1]. It will change as follows:

[ 6 , 10 , 1 ] → [ 1 , 6 , 10 ] → [ 4 , 8 , 11 ] → [ 7 , 10 , 12 ] → [ 10 , 12 , 13 ] → [ 13 , 14 , 14 ] → [ 13 , 14 ] → [ 15 , 15 ] → [ 15 ] [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] [6,10,1][1,6,10][4,8,11][7,10,12][10,12,13][13,14,14][13,14][15,15][15]
Tutorial

根据题意,“The Great Equalizer” 的每次工作后,按排序后的相邻元素之间的差值就会减 1 1 1,所以每次操作后的最后的答案等于 最大的数字 + 按排序顺序相邻数字之间的最大差值,我们也就将问题转换为了维护 最大数字 和

按顺序排序后的相邻元素差值,最大数字我们可以直接用 s e t set set 存储,按排序顺序相邻数字之间的最大差值 我们可以用 m u l t i s e t multiset multiset 存储,对于每次操作,我们可以直接查找位置,直接删除增加即可

Solution

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;

#define ff first
#define ss second
#define endl '\n'
#define int long long

void solve() {
    int n, q;
    cin >> n;
    vector<int> a(n);
    for (int &ai : a) {
        cin >> ai;
    }
    vector<int> b(a);
    ranges::sort(b);
    
    set<PII> vals;
    multiset<int> diff;
    diff.insert(0);
    for (int i = 0; i < n; ++i) {
        vals.insert({a[i], i});
        if (i) {
            diff.insert(b[i] - b[i - 1]);
        }
    }

    cin >> q;
    while (q--) {
        int i, x;
        cin >> i >> x;
        i--;

        // 先将原来的删除
        set<PII>::iterator it = vals.find({a[i], i});
        set<PII>::iterator it2 = it, it3 = it;
        ++it2, --it3;
        
        if (it2 != vals.end()) {
            diff.erase(diff.find(it2->ff - it->ff));
        }
        if (it != vals.begin()) {
            diff.erase(diff.find(it->ff - it3->ff));
        }
        if (it2 != vals.end() and it != vals.begin()) {
            diff.insert(it2->ff - it3->ff);
        }

        vals.erase(it);
        a[i] = x;
        vals.insert({a[i], i});
        
        // 将更改后的添加进去
        it = vals.find({a[i], i});
        it2 = it3 = it;
        ++it2, --it3;

        if (it2 != vals.end()) {
            diff.insert(it2->ff - it->ff);
        }
        if (it != vals.begin()) {
            diff.insert(it->ff - it3->ff);
        }
        if (it2 != vals.end() and it != vals.begin()) {
            diff.erase(diff.find(it2->ff - it3->ff));
        }
        
        // 返回最大的数 + 工作次数,其中 工作次数 是相邻元素的最大差值
        cout << vals.rbegin()->ff + *diff.rbegin() << " \n"[!q];
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值