Codeforces Round 888 (Div. 3)


A. Escalator Conversations

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

One day, Vlad became curious about who he can have a conversation with on the escalator in the subway. There are a total of n n n passengers. The escalator has a total of m m m steps, all steps indexed from 1 1 1 to m m m and i i i-th step has height i ⋅ k i \cdot k ik.

Vlad’s height is H H H centimeters. Two people with heights a a a and b b b can have a conversation on the escalator if they are standing on different steps and the height difference between them is equal to the height difference between the steps.

For example, if two people have heights 170 170 170 and 180 180 180 centimeters, and m = 10 , k = 5 m = 10, k = 5 m=10,k=5, then they can stand on steps numbered 7 7 7 and 5 5 5, where the height difference between the steps is equal to the height difference between the two people: k ⋅ 2 = 5 ⋅ 2 = 10 = 180 − 170 k \cdot 2 = 5 \cdot 2 = 10 = 180 - 170 k2=52=10=180170. There are other possible ways.

Given an array h h h of size n n n, where h i h_i hi represents the height of the i i i-th person. Vlad is interested in how many people he can have a conversation with on the escalator individually.

For example, if n = 5 , m = 3 , k = 3 , H = 11 n = 5, m = 3, k = 3, H = 11 n=5,m=3,k=3,H=11, and h = [ 5 , 4 , 14 , 18 , 2 ] h = [5, 4, 14, 18, 2] h=[5,4,14,18,2], Vlad can have a conversation with the person with height 5 5 5 (Vlad will stand on step 1 1 1, and the other person will stand on step 3 3 3) and with the person with height 14 14 14 (for example, Vlad can stand on step 3 3 3, and the other person will stand on step 2 2 2). Vlad cannot have a conversation with the person with height 2 2 2 because even if they stand on the extreme steps of the escalator, the height difference between them will be 6 6 6, while their height difference is 9 9 9. Vlad cannot have a conversation with the rest of the people on the escalator, so the answer for this example is 2 2 2.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases.

Then the descriptions of the test cases follow.

The first line of each test case contains integers: n , m , k , H n, m, k, H n,m,k,H ( 1 ≤ n , m ≤ 50 1 \le n,m \le 50 1n,m50, 1 ≤ k , H ≤ 1 0 6 1 \le k,H \le 10^6 1k,H106). Here, n n n is the number of people, m m m is the number of steps, k k k is the height difference between neighboring steps, and H H H is Vlad’s height.

The second line contains n n n integers: h 1 , h 2 , … , h n h_1, h_2, \ldots, h_n h1,h2,,hn ( 1 ≤ h i ≤ 1 0 6 1 \le h_i \le 10^6 1hi106). Here, h i h_i hi represents the height of the i i i-th person.

Output

For each test case, output a single integer — the number of people Vlad can have a conversation with on the escalator individually.

Example
input

7
5 3 3 11
5 4 14 18 2
2 9 5 6
11 9
10 50 3 11
43 44 74 98 62 60 99 4 11 73
4 8 8 49
68 58 82 73
7 1 4 66
18 66 39 83 48 99 79
9 1 1 13
26 23 84 6 60 87 40 41 25
6 13 3 28
30 70 85 13 1 55

output

2
1
4
1
0
0
3

Note

The first example is explained in the problem statement.

In the second example, Vlad can have a conversation with the person with height 11 11 11.

In the third example, Vlad can have a conversation with people with heights: 44 , 74 , 98 , 62 44, 74, 98, 62 44,74,98,62. Therefore, the answer is 4 4 4.

In the fourth example, Vlad can have a conversation with the person with height 73 73 73.

Tutorial
两个人站的位置不能在同一级,同时他们之间的差值应该能被 k 整除,同时他们差的楼梯数不能超过 m

Solution

for _ in range(int(input())):
    cnt = 0
    n, m, k, H = map(int, input().split())
    a = list(map(int, input().split()))
    for i, h in enumerate(a):
        if abs(H - h) % k == 0 and abs(H - h) / k < m and abs(H - h) / k > 0:
            cnt += 1
    print(cnt)

B. Parity Sort

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

You have an array of integers a a a of length n n n. You can apply the following operation to the given array:

  • Swap two elements a i a_i ai and a j a_j aj such that i ≠ j i \neq j i=j, a i a_i ai and a j a_j aj are either both even or both odd.

Determine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero).

For example, let a a a = [ 7 , 10 , 1 , 3 , 2 7, 10, 1, 3, 2 7,10,1,3,2]. Then we can perform 3 3 3 operations to sort the array:

  1. Swap a 3 = 1 a_3 = 1 a3=1 and a 1 = 7 a_1 = 7 a1=7, since 1 1 1 and 7 7 7 are odd. We get a a a = [ 1 , 10 , 7 , 3 , 2 1, 10, 7, 3, 2 1,10,7,3,2];
  2. Swap a 2 = 10 a_2 = 10 a2=10 and a 5 = 2 a_5 = 2 a5=2, since 10 10 10 and 2 2 2 are even. We get a a a = [ 1 , 2 , 7 , 3 , 10 1, 2, 7, 3, 10 1,2,7,3,10];
  3. Swap a 4 = 3 a_4 = 3 a4=3 and a 3 = 7 a_3 = 7 a3=7, since 3 3 3 and 7 7 7 are odd. We get a a a = [ 1 , 2 , 3 , 7 , 10 1, 2, 3, 7, 10 1,2,3,7,10].

Input

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.

The description of the test cases follows.

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

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

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 on a separate line:

  • YES if the array can be sorted by applying the operation to it some number of times;
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).

Example
input

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

output

YES
YES
NO
NO
YES
NO

Note

The first test case is explained in the problem statement.

Tutorial
将原有的数组复制到另一个数组,然后对其进行排序,最后看相应位置上的数字奇偶性是否统一,如果不统一就是 NO,否则为 YES

Solution

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = sorted(a)
    flag = True
    for i, x in enumerate(b):
        if x & 1 != a[i] & 1:
            flag = False
            break
    if flag:
        print("YES")
    else:
        print("NO")

C. Tiles Comeback

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

Vlad remembered that he had a series of n n n tiles and a number k k k. The tiles were numbered from left to right, and the i i i-th tile had colour c i c_i ci.

If you stand on the first tile and start jumping any number of tiles right, you can get a path of length p p p. The length of the path is the number of tiles you stood on.

Vlad wants to see if it is possible to get a path of length p p p such that:

  • it ends at tile with index n n n;
  • p p p is divisible by k k k
  • the path is divided into blocks of length exactly k k k each;
  • tiles in each block have the same colour, the colors in adjacent blocks are not necessarily different.

For example, let n = 14 n = 14 n=14, k = 3 k = 3 k=3.

The colours of the tiles are contained in the array c c c = [ 1 , 2 , 1 , 1 , 7 , 5 , 3 , 3 , 1 , 3 , 4 , 4 , 2 , 4 \color{red}{1}, \color{violet}{2}, \color{red}{1}, \color{red}{1}, \color{gray}{7}, \color{orange}{5}, \color{green}{3}, \color{green}{3}, \color{red}{1}, \color{green}{3}, \color{blue}{4}, \color{blue}{4}, \color{violet}{2}, \color{blue}{4} 1,2,1,1,7,5,3,3,1,3,4,4,2,4]. Then we can construct a path of length 6 6 6 consisting of 2 2 2 blocks:

c 1 → c 3 → c 4 → c 11 → c 12 → c 14 \color{red}{c_1} \rightarrow \color{red}{c_3} \rightarrow \color{red}{c_4} \rightarrow \color{blue}{c_{11}} \rightarrow \color{blue}{c_{12}} \rightarrow \color{blue}{c_{14}} c1c3c4c11c12c14

All tiles from the 1 1 1-st block will have colour 1 \color{red}{\textbf{1}} 1, from the 2 2 2-nd block will have colour 4 \color{blue}{\textbf{4}} 4.

It is also possible to construct a path of length 9 9 9 in this example, in which all tiles from the 1 1 1-st block will have colour 1 \color{red}{\textbf{1}} 1, from the 2 2 2-nd block will have colour 3 \color{green}{\textbf{3}} 3, and from the 3 3 3-rd block will have colour 4 \color{blue}{\textbf{4}} 4.

Input

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.

The description of the test cases follows.

The first line of each test case contains two integers n n n and k k k ( 1 ≤ k ≤ n ≤ 2 ⋅ 1 0 5 1 \le k \le n \le 2 \cdot 10^5 1kn2105)—the number of tiles in the series and the length of the block.

The second line of each test case contains n n n integers c 1 , c 2 , c 3 , … , c n c_1, c_2, c_3, \dots, c_n c1,c2,c3,,cn ( 1 ≤ c i ≤ n 1 \le c_i \le n 1cin) — the colours of the tiles.

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 on a separate line:

  • YES if you can get a path that satisfies these conditions;
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).

Example
input

10
4 2
1 1 1 1
14 3
1 2 1 1 7 5 3 3 1 3 4 4 2 4
3 3
3 1 3
10 4
1 2 1 2 1 2 1 2 1 2
6 2
1 3 4 1 6 6
2 2
1 1
4 2
2 1 1 1
2 1
1 2
3 2
2 2 2
4 1
1 1 2 2

output

YES
YES
NO
NO
YES
YES
NO
YES
YES
YES

Note

In the first test case, you can jump from the first tile to the last tile;

The second test case is explained in the problem statement.

Tutorial
路径必须是以第一个图块开始,最后一个图块结束,所以只需要考虑如下两种情况:

  • 如果 c 1 = c n c_1 = c_n c1=cn,则此时只需要考虑能否构成 1 1 1 个长度为 k k k 的连通块,即只需要判断第一个图块和最后一个图块之间是否有 k − 2 k - 2 k2 个颜色为 c 0 c_0 c0 的图块
  • 如果 c 1 ≠ c n c_1 \neq c_n c1=cn,可以使用双指针,从 c 1 c_1 c1 移动到 c n c_n cn,看两者能否在不晚于 k k k 个块相遇,如果没有相遇则答案为 Y E S YES YES, 否则为 N O NO NO

Solution

for _ in range(int(input())):
    n, k = map(int, input().split())
    c = list(map(int, input().split()))
    a, b, x, y, cnt = c[0], c[-1], -1, -1, 0
    for i, ci in enumerate(c):
        cnt += ci == a
        if cnt == k:
            x = i
            break
    cnt = 0
    for i, ci in enumerate(c[::-1]):
        cnt += ci == b
        if cnt == k:
            y = n - i - 1
            break
    if a == b and x != -1:
        print("YES")
    elif x == -1 or y == -1 or x > y:
        print("NO")
    else:
        print("YES")

D. Prefix Permutation Sums

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

Your friends have an array of n n n elements, calculated its array of prefix sums and passed it to you, accidentally losing one element during the transfer. Your task is to find out if the given array can matches permutation.

A permutation of n n n elements is an array of n n n numbers from 1 1 1 to n n n such that each number occurs exactly one times in it.

The array of prefix sums of the array a a a — is such an array b b b that b i = ∑ j = 1 i a j , 1 ≤ i ≤ n b_i = \sum_{j=1}^i a_j, 1 \le i \le n bi=j=1iaj,1in.

For example, the original permutation was [ 1 , 5 , 2 , 4 , 3 ] [1, 5, 2, 4, 3] [1,5,2,4,3]. Its array of prefix sums — [ 1 , 6 , 8 , 12 , 15 ] [1, 6, 8, 12, 15] [1,6,8,12,15]. Having lost one element, you can get, for example, arrays [ 6 , 8 , 12 , 15 ] [6, 8, 12, 15] [6,8,12,15] or [ 1 , 6 , 8 , 15 ] [1, 6, 8, 15] [1,6,8,15].

It can also be shown that the array [ 1 , 2 , 100 ] [1, 2, 100] [1,2,100] does not correspond to any permutation.

Input

The first line contains a positive number 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 the description of each test case contains a positive number n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105) — the size of the initial array.

The second line of the description of each test case contains n − 1 n - 1 n1 positive number a i a_i ai ( 1 ≤ a i ≤ 1 0 18 1 \le a_i \le 10^{18} 1ai1018), a i − 1 < a i a_{i-1} < a_i ai1<ai — elements of the array of prefix sums.

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 “YES” if such a permutation exists, and “NO” otherwise.

You can output “YES” and “NO” in any case (for example, the strings “yEs”, “yes” and “Yes” will be recognized as a positive response).

Example
input

12
5
6 8 12 15
5
1 6 8 15
4
1 2 100
4
1 3 6
2
2
3
1 2
4
3 7 10
5
5 44 46 50
4
1 9 10
5
13 21 36 42
5
1 2 3 1000000000000000000
9
9 11 12 20 25 28 30 33

output

YES
YES
NO
YES
YES
NO
YES
NO
NO
NO
NO
NO

Note

In the fourth example, for example, the permutation [ 1 , 2 , 3 , 4 ] [1, 2, 3, 4] [1,2,3,4] is suitable. In the fifth example, for example, the permutation [ 2 , 1 ] [2, 1] [2,1] is suitable. In the seventh example, for example, the permutation [ 1 , 2 , 4 , 3 ] [1, 2, 4, 3] [1,2,4,3] is suitable.

Tutorial
先用题目给出的数组判断有哪些数缺失,如果只缺失了一个元素,那么直接可以判断为 YES,如果缺失了三个元素或者更多,则可以直接判断为 NO,如果恰好缺失了两个元素,再判断他是否缺少了两边的元素

Solution

from collections import Counter

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    mp = Counter()
    judge = []
    for i, x in enumerate(a):
        if i:
            mp[x - a[i - 1]] += 1
        else:
            mp[x] += 1
    for i in range(1, n + 1):
        if mp[i] == 0:
            judge.append(i)
    if len(judge) == 1:
        print("YES")
    elif len(judge) > 2:
        print("NO")
    else:
        print("YES" if mp[judge[0] + judge[1]] > (sum(judge) <= n) else "NO")

E. Nastya and Potions

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

Alchemist Nastya loves mixing potions. There are a total of n n n types of potions, and one potion of type i i i can be bought for c i c_i ci coins.

Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes.

As an experienced alchemist, Nastya has an unlimited supply of k k k types of potions p 1 , p 2 , … , p k p_1, p_2, \dots, p_k p1,p2,,pk, but she doesn’t know which one she wants to obtain next. To decide, she asks you to find, for each 1 ≤ i ≤ n 1 \le i \le n 1in, the minimum number of coins she needs to spend to obtain a potion of type i i i next.

Input

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

Each test case is described as follows:

The first line contains two integers n n n and k k k ( 1 ≤ k < n ≤ 2 ⋅ 1 0 5 1 \le k < n \le 2 \cdot 10^5 1k<n2105) — the total number of potion types and the number of potion types Nastya already has.

The second line contains n n n integers c 1 , c 2 , … , c n c_1, c_2, \dots, c_n c1,c2,,cn ( 1 ≤ c i ≤ 1 0 9 1 \le c_i \le 10^9 1ci109) — the costs of buying the potions.

The third line contains k k k distinct integers p 1 , p 2 , … , p k p_1, p_2, \dots, p_k p1,p2,,pk ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin) — the indices of potions Nastya already has an unlimited supply of.

This is followed by n n n lines describing ways to obtain potions by mixing.

Each line starts with the integer m i m_i mi ( 0 ≤ m i < n 0 \le m_i < n 0mi<n) — the number of potions required to mix a potion of the type i i i ( 1 ≤ i ≤ n 1 \le i \le n 1in).

Then line contains m i m_i mi distinct integers e 1 , e 2 , … , e m i e_1, e_2, \dots, e_{m_i} e1,e2,,emi ( 1 ≤ e j ≤ n 1 \le e_j \le n 1ejn, e j ≠ i e_j \ne i ej=i) — the indices of potions needed to mix a potion of the type i i i. If this list is empty, then a potion of the type i i i can only be bought.

It is guaranteed that no potion can be obtained from itself through one or more mixing processes.

It is guaranteed that the sum of all n n n values across all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105. Similarly, it is guaranteed that the sum of all m i m_i mi values across all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output n n n integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.

Example
input1

4
5 1
30 8 3 5 10
3
3 2 4 5
0 
0 
2 3 5
0 
3 2
5 143 3
1 3
1 2
0 
2 1 2
5 1
5 4 1 3 4
2
2 4 5
3 3 5 4
2 1 4
1 5
0 
4 2
1 1 5 4
2 4
3 2 4 3
0 
2 2 4
1 2

output1

23 8 0 5 10 
0 143 0 
5 0 1 3 4 
0 0 0 0 

input2

3
6 3
5 5 4 5 2 2
3 4 5
2 2 5
1 5
3 4 1 6
4 2 6 1 5
0 
0 
6 2
1 4 4 1 5 2
3 6
4 6 3 4 5
4 6 5 3 4
0 
1 5
1 6
0 
2 1
4 3
1
0 
1 1

output2

0 0 0 0 0 2 
0 0 0 0 0 0 
0 0 

Note

In the first test case of the first sample, it is optimal:

  • Get a potion of the first type by buying and mixing 2 2 2, 4 4 4 and 5 5 5;
  • a potion of the second type can only be obtained by purchasing it;
  • Nastya already has an unlimited number of potions of the third type;
  • a potion of the fourth type is more profitable to buy than to buy and mix other potions;
  • a potion of the fifth type can only be obtained by purchasing it.

Tutorial
先将免费的药水进行标记,再对每一种药水采用 DFS 判断合成每种药水的价格

Solution

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

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

int n, m, x, y;

void solve() {
    cin >> n >> m;
    vector<int> a(n + 1), ans(n + 1);
    vector<bool> memo(n + 1);
    vector<vector<int>> all;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    while (m--) {
        cin >> x;
        a[x] = 0;
    }
    all.push_back({});
    for (int i = 1; i <= n; ++i) {
        cin >> x;
        vector<int> mid(x);
        for (int j = 0; j < x; ++j) {
            cin >> mid[j];
        }
        all.emplace_back(mid);
    }
    for (int i = 1; i <= n; ++i) {
        if (a[i] == 0) {
            ans[i] = 0;
            memo[i] = true;
        }
    }

    function<void(int)> dfs = [&](int x) {
        for (int y : all[x]) {
            if (!memo[y]) {
                dfs(y);
            }
            ans[x] += ans[y];
        }
        if (all[x].empty()) {
            ans[x] = a[x];
        }
        ans[x] = min(ans[x], a[x]);
        memo[x] = true;
    };

    for (int i = 1; i <= n; ++i) {
        if (!memo[i]) {
            dfs(i);
        }
        cout << ans[i] << " \n"[i == n];
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}

F. Lisa and the Martians

time limit per test: 3 second
memory limit per test: 512 megabytes
input: standard input
outputL: standard output

Lisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let’s call integer martian if it is a non-negative integer and strictly less than 2 k 2^k 2k, for example, when k = 12 k = 12 k=12, the numbers 51 51 51, 1960 1960 1960, 0 0 0 are martian, and the numbers π \pi π, − 1 -1 1, 21 8 \frac{21}{8} 821, 4096 4096 4096 are not.

The aliens will give Lisa n n n martian numbers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an. Then they will ask her to name any martian number x x x. After that, Lisa will select a pair of numbers a i , a j a_i, a_j ai,aj ( i ≠ j i \neq j i=j) in the given sequence and count ( a i ⊕ x ) & ( a j ⊕ x ) (a_i \oplus x) \& (a_j \oplus x) (aix)&(ajx). The operation ⊕ \oplus means Bitwise exclusive OR, the operation & \& & means Bitwise And. For example, ( 5 ⊕ 17 ) & ( 23 ⊕ 17 ) = ( 0010 1 2 ⊕ 1000 1 2 ) & ( 1011 1 2 ⊕ 1000 1 2 ) = 1010 0 2 & 0011 0 2 = 0010 0 2 = 4 (5 \oplus 17) \& (23 \oplus 17) = (00101_2 \oplus 10001_2) \& (10111_2 \oplus 10001_2) = 10100_2 \& 00110_2 = 00100_2 = 4 (517)&(2317)=(001012100012)&(101112100012)=101002&001102=001002=4.

Lisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such i , j , x i, j, x i,j,x that maximize the calculated value.

Input

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

Each testcase is described by two lines.

The first line contains integers n , k n, k n,k ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105, 1 ≤ k ≤ 30 1 \le k \le 30 1k30) — the length of the sequence of martian numbers and the value of k k k.

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 0 ≤ a i < 2 k 0 \le a_i < 2^k 0ai<2k) — a sequence of martian numbers.

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

Output

For each testcase, print three integers i , j , x i, j, x i,j,x ( 1 ≤ i , j ≤ n 1 \le i, j \le n 1i,jn, i ≠ j i \neq j i=j, 0 ≤ x < 2 k 0 \le x < 2^k 0x<2k). The value of ( a i ⊕ x ) & ( a j ⊕ x ) (a_i \oplus x) \& (a_j \oplus x) (aix)&(ajx) should be the maximum possible.

If there are several solutions, you can print any one.

Example
input

10
5 4
3 9 1 4 13
3 1
1 0 1
6 12
144 1580 1024 100 9 13
4 3
7 3 0 4
3 2
0 0 1
2 4
12 2
9 4
6 14 9 4 4 4 5 10 2
2 1
1 0
2 4
11 4
9 4
2 11 10 1 6 9 11 0 5

output

1 3 14
1 3 0
5 6 4082
2 3 7
1 2 3
1 2 15
4 5 11
1 2 0
1 2 0
2 7 4

Note

First testcase: ( 3 ⊕ 14 ) & ( 1 ⊕ 14 ) = ( 001 1 2 ⊕ 111 0 2 ) & ( 000 1 2 ⊕ 111 0 2 ) = 110 1 2 = 110 1 2 & 111 1 2 = 110 1 2 = 13 (3 \oplus 14) \& (1 \oplus 14) = (0011_2 \oplus 1110_2) \& (0001_2 \oplus 1110_2) = 1101_2 = 1101_2 \& 1111_2 = 1101_2 = 13 (314)&(114)=(0011211102)&(0001211102)=11012=11012&11112=11012=13.

Second testcase: ( 1 ⊕ 0 ) & ( 1 ⊕ 0 ) = 1 (1 \oplus 0) \& (1 \oplus 0) = 1 (10)&(10)=1.

Third testcase: ( 9 ⊕ 4082 ) & ( 13 ⊕ 4082 ) = 4091 (9 \oplus 4082) \& (13 \oplus 4082) = 4091 (94082)&(134082)=4091.

Fourth testcase: ( 3 ⊕ 7 ) & ( 0 ⊕ 7 ) = 4 (3 \oplus 7) \& (0 \oplus 7) = 4 (37)&(07)=4.

Tutorial
排序找到相邻数异或最小的位置,因为相邻位置的数异或在二进制表示上是最小的

官方题解:
按非降序对 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an 进行排序。我们将证明答案是一对相邻的数字。设答案为数字 a i , a j a_i, a_j ai,aj ( j − i > 1 j - i > 1 ji>1)。如果 a i = a j a_i = a_j ai=aj ,则 a i = a i + 1 a_i = a_{i + 1} ai=ai+1。否则,它们具有共同的位前缀,后面有不同的位。也就是说,在某个位置 t t t a i a_i ai 0 0 0 a j a_j aj 1 1 1。由于 j − i > 1 j - i > 1 ji>1 a i + 1 a_{i + 1} ai+1在该位置可以有 0 0 0 1 1 1,但第一种情况选择 a i , a i + 1 a_i, a_{i + 1} ai,ai+1 更有利,第二种情况选择 a i + 1 , a j a_{i + 1}, a_j ai+1,aj更有利。选择 a i + 1 , a j a_{i + 1}, a_j ai+1,aj 作为答案。该解的复杂度为 O ( n log ⁡ n ) O(n \log n) O(nlogn)

Solution

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

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

void solve() {
    int n, k, x, ans, mn, mid;
    cin >> n >> k;
    vector<PII> all;
    ans = 0;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        all.emplace_back(x, i);
    }
    ranges::sort(all);
    mn = all[0].fi ^ all[1].fi;
    for (int i = 2; i < n; ++i) {
        mid = all[i].fi ^ all[i - 1].fi;
        if (mid < mn) {
            mn = mid;
            ans = i - 1;
        }
    }
    cout << all[ans].se + 1 << " " << all[ans + 1].se + 1 << " " << (1 << k) - 1 - all[ans].fi << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}

G. Vlad and the Mountains

time limit per test: 5 second
memory limit per test: 512 megabytes
input: standard input
outputL: standard output

Vlad decided to go on a trip to the mountains. He plans to move between n n n mountains, some of which are connected by roads. The i i i-th mountain has a height of h i h_i hi.

If there is a road between mountains i i i and j j j, Vlad can move from mountain i i i to mountain j j j by spending h j − h i h_j - h_i hjhi units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain i i i to mountain j j j. Note that h j − h i h_j - h_i hjhi can be negative and then the energy will be restored.

Vlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain a a a and ending at mountain b b b, given that he initially has e e e units of energy?

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 descriptions of the test cases follow.

The first line of each test case contains two numbers n n n and m m m ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105, 1 ≤ m ≤ min ⁡ ( n ⋅ ( n − 1 ) 2 , 2 ⋅ 1 0 5 ) 1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5) 1mmin(2n(n1),2105)) — the number of mountains and the number of roads between them, respectively.

The second line contains n n n integers h 1 , h 2 , h 3 , … , h n h_1, h_2, h_3, \dots, h_n h1,h2,h3,,hn ( 1 ≤ h i ≤ 1 0 9 1 \le h_i \le 10^9 1hi109) — the heights of the mountains.

The next m m m lines contain two integers u u u and v v v ( 1 ≤ u , v ≤ n 1 \le u, v \le n 1u,vn, u ≠ v u \ne v u=v) — the numbers of the mountains connected by a road. It is guaranteed that no road appears twice.

The next line contains an integer q q q ( 1 ≤ q ≤ 2 ⋅ 1 0 5 1 \le q \le 2 \cdot 10^5 1q2105) — the number of queries.

The following q q q lines contain three numbers a a a, b b b, and e e e ( 1 ≤ a , b ≤ n 1 \le a, b \le n 1a,bn, 0 ≤ e ≤ 1 0 9 0 \le e \le 10^9 0e109) — the initial and final mountains of the route, and the amount of energy, respectively.

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. The same guarantee applies to m m m and q q q.

Output

For each query, output “YES” if Vlad can construct a route from mountain a a a to mountain b b b, and “NO” otherwise.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as a positive answer).

In the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.

Example
input1

2
7 7
1 5 3 4 2 4 1
1 4
4 3
3 6
3 2
2 5
5 6
5 7
5
1 1 3
6 2 0
4 7 0
1 7 4
1 7 2
6 5
4 7 6 2 5 1
1 3
5 3
1 5
2 4
6 2
5
1 5 1
1 3 1
1 2 1000
6 2 6
6 2 5

output1

YES
NO
YES
YES
NO

YES
NO
NO
YES
NO

input2

2
3 2
1 3 9
1 2
2 3
5
1 1 1
3 2 2
1 1 2
3 3 0
1 2 1
3 3
1 4 1
1 2
2 3
1 3
5
3 3 9
1 3 6
1 1 2
3 3 6
3 3 4

output2

YES
NO
YES
YES
NO

YES
NO
NO
YES
NO

input3

2
3 2
1 3 9
1 2
2 3
5
1 1 1
3 2 2
1 1 2
3 3 0
1 2 1
3 3
1 4 1
1 2
2 3
1 3
5
3 3 9
1 3 6
1 1 2
3 3 6
3 3 4

output3

YES
YES
YES
YES
NO

YES
YES
YES
YES
YES

Tutorial
由题意得,从顶点 i i i 出发,有可能到达任何不经过高度大于 h a + e h_a + e ha+e 的顶点的路径,因此需要对每个查询,从 a a a 构建一个集合,其中包含所有高度不大于 h a + e h_a + e ha+e 的顶点,查看 b b b 是否在其中,此时需要维护一个并查集
在每个查询前,添加所有没有添加的边,并对与这个特定查询,它们的 max ⁡ ( h u , h v ) \max(h_u, h_v) max(hu,hv) 不大于 h a + e h_a+e ha+e,此后,只需检查顶点 a a a b b b是否属于同一连通分量

Solution

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

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

void solve() {
    int n, m, u, v, e, q, a, b;
    cin >> n >> m;
    vector<int> h(n + 1), g[n + 1], p(n + 1, -1);
    vector<tuple<int, int, int, int>> all;
    for (int i = 1; i <= n; ++i) {
        cin >> h[i];
        all.emplace_back(h[i], -1, i, 0);
    }
    for (int i = 0; i < m; ++i) {
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    cin >> q;
    vector<bool> ans(q);
    for (int i = 0; i < q; ++i) {
        cin >> u >> v >> e;
        all.emplace_back(h[u] + e, i, u, v);
    }
    ranges::sort(all);

    function<int(int)> dfs = [&](int x) -> int {
        return p[x] < 0 ? x : p[x] = dfs(p[x]);
    };

    for (auto [_, idx, u, v] : all) {
        if (idx == -1) {
            for (int v : g[u]) {
                if (h[v] <= h[u]) {
                    a = dfs(u), b = dfs(v);
                    if (a != b) {
                        if (a < b) {
                            swap(a, b);
                        }
                        p[b] += p[a];
                        p[a] = b;
                    }
                }
            }
        } else {
            ans[idx] = (dfs(u) == dfs(v));
        }
    }
    for (bool flag : ans) {
        cout << (flag ? "YES\n" : "NO\n");
    }
    cout << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值