Codeforces Round 933 (Div. 3)

A. Rudolf and the Ticket

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

Rudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed k k k.

Rudolf has two pockets with coins. In the left pocket, there are n n n coins with denominations b 1 , b 2 , … , b n b_1, b_2, \dots, b_n b1,b2,,bn. In the right pocket, there are m m m coins with denominations c 1 , c 2 , … , c m c_1, c_2, \dots, c_m c1,c2,,cm. He wants to choose exactly one coin from the left pocket and exactly one coin from the right pocket (two coins in total).

Help Rudolf determine how many ways there are to select indices f f f and s s s such that b f + c s ≤ k b_f + c_s \le k bf+csk.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of test cases. Then follows the description of each test case.

The first line of each test case contains three natural numbers n n n, m m m, and k k k ( 1 ≤ n , m ≤ 100 , 1 ≤ k ≤ 2000 1 \le n, m \le 100, 1 \le k \le 2000 1n,m100,1k2000) — the number of coins in the left and right pockets, and the maximum sum of two coins for the ticket payment at the counter, respectively.

The second line of each test case contains n n n integers b i b_i bi ( 1 ≤ b i ≤ 1000 1 \le b_i \le 1000 1bi1000) — the denominations of coins in the left pocket.

The third line of each test case contains m m m integers c i c_i ci ( 1 ≤ c i ≤ 1000 1 \le c_i \le 1000 1ci1000) — the denominations of coins in the right pocket.

Output

For each testcase, output a single integer — the number of ways Rudolf can select two coins, taking one from each pocket, so that the sum of the coins does not exceed k k k.

Example

input

4
4 4 8
1 5 10 14
2 1 8 1
2 3 4
4 8
1 2 3
4 2 7
1 1 1 1
2 7
3 4 2000
1 1 1
1 1 1 1

output

6
0
4
12

Note

Note that the pairs indicate the indices of the coins in the array, not their denominations.

In the first test case, Rudolf can choose the following pairs of coins: [ 1 , 1 ] , [ 1 , 2 ] , [ 1 , 4 ] , [ 2 , 1 ] , [ 2 , 2 ] , [ 2 , 4 ] [1, 1], [1, 2], [1, 4], [2, 1], [2, 2], [2, 4] [1,1],[1,2],[1,4],[2,1],[2,2],[2,4].

In the second test case, Rudolf cannot choose one coin from each pocket in any way, as the sum of any two elements from the first and second arrays will exceed the value of k = 4 k=4 k=4.

In the third test case, Rudolf can choose: [ 1 , 1 ] , [ 2 , 1 ] , [ 3 , 1 ] , [ 4 , 1 ] [1, 1], [2, 1], [3, 1], [4, 1] [1,1],[2,1],[3,1],[4,1].

In the fourth test case, Rudolf can choose any coin from the left pocket and any coin from the right pocket.

Tutorial

第一题怎么快怎么来,直接暴力循环破解

Solution

for _ in range(int(input())):
    n, m, k = map(int, input().split())
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    print(sum(b[i] + c[j] <= k for j in range(m) for i in range(n)))

B. Rudolf and 121

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

Rudolf has an array a a a of n n n integers, the elements are numbered from 1 1 1 to n n n.

In one operation, he can choose an index i i i ( 2 ≤ i ≤ n − 1 2 \le i \le n - 1 2in1) and assign:

  • a i − 1 = a i − 1 − 1 a_{i - 1} = a_{i - 1} - 1 ai1=ai11
  • a i = a i − 2 a_i = a_i - 2 ai=ai2
  • a i + 1 = a i + 1 − 1 a_{i + 1} = a_{i + 1} - 1 ai+1=ai+11

Rudolf can apply this operation any number of times. Any index i i i can be used zero or more times.

Can he make all the elements of the array equal to zero using this 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 in the test.

The first line of each case contains a single integer n n n ( 3 ≤ n ≤ 2 ⋅ 1 0 5 3 \le n \le 2 \cdot 10^5 3n2105) — the number of elements in the array.

The second line of each case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 0 ≤ a j ≤ 1 0 9 0 \le a_j \le 10^9 0aj109) — the elements of the array.

It is guaranteed that 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 “YES” if it is possible to make all the elements of the array zero using the described operations. 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
1 3 5 5 2
5
2 4 4 5 1
5
0 1 3 3 1
6
5 6 0 2 3 0
4
1 2 7 2
3
7 1 0
4
1 1 1 1

output

YES
NO
YES
NO
NO
NO
NO

Note

In the first example, the original array is [ 1 , 3 , 5 , 5 , 2 ] [1, 3, 5, 5, 2] [1,3,5,5,2], to make all its elements zero, Rudolf can act as follows:

  • apply the operation at i = 4 i=4 i=4 and get the array [ 1 , 3 , 4 , 3 , 1 ] [1, 3, 4, 3, 1] [1,3,4,3,1];
  • apply the operation at i = 3 i=3 i=3 and get the array [ 1 , 2 , 2 , 2 , 1 ] [1, 2, 2, 2, 1] [1,2,2,2,1];
  • apply the operation at i = 2 i=2 i=2 and get the array [ 0 , 0 , 1 , 2 , 1 ] [0, 0, 1, 2, 1] [0,0,1,2,1];
  • apply the operation at i = 4 i=4 i=4 and get the array [ 0 , 0 , 0 , 0 , 0 ] [0, 0, 0, 0, 0] [0,0,0,0,0].

Tutorial

可以将题目中的操作变形为:

  • a i = a i − 1 a_{i} = a_{i} - 1 ai=ai1
  • a i + 1 = a i + 1 − 2 a_{i + 1} = a_{i + 1} - 2 ai+1=ai+12
  • a i + 2 = a i + 2 − 1 a_{i + 2} = a_{i + 2} - 1 ai+2=ai+21

所以可以直接一次从最左边的元素直接遍历到倒数第三个元素,最后判断最后两个数是否为 0 即可

Solution

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    ans = "YES"
    for i in range(n - 2):
        if a[i] >= 0:
            a[i + 1] -= a[i] * 2
            a[i + 2] -= a[i]
            a[i] = 0
        else:
            ans = "NO"
            break
    if a[-1] or a[-2]:
        ans = "NO"
    print(ans)

C. Rudolf and the Ugly String

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

Rudolf has a string s s s of length n n n. Rudolf considers the string s s s to be ugly if it contains the substring † ^\dagger “pie” or the substring “map”, otherwise the string s s s will be considered beautiful.

For example, “ppiee”, “mmap”, “dfpiefghmap” are ugly strings, while “mathp”, “ppiiee” are beautiful strings.

Rudolf wants to shorten the string s s s by removing some characters to make it beautiful.

The main character doesn’t like to strain, so he asks you to make the string beautiful by removing the minimum number of characters. He can remove characters from any positions in the string (not just from the beginning or end of the string).

† ^\dagger String a a a is a substring of b b b if there exists a consecutive segment of characters in string b b b equal to a a a.

Input

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

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 6 1 \le n \le 10^6 1n106) — the length of the string s s s.

The next line of each test case contains the string s s s of length n n n. The string s s s consists of lowercase Latin letters.

The sum of n n n over all test cases does not exceed 1 0 6 10^6 106.

Output

For each test case, output a single integer — the minimum number of characters that need to be deleted to make the string s s s beautiful. If the string is initially beautiful, then output 0 0 0.

Example

input

6
9
mmapnapie
9
azabazapi
8
mappppie
18
mapmapmapmapmapmap
1
p
11
pppiepieeee

output

2
0
2
6
0
2

Note

In the first test case, for example, you can delete the 4 4 4th and 9 9 9th characters to make the string beautiful.

In the second test case, the string is already beautiful.

Tutorial

遇到 map 或者 pie 都需要在三个数字中选择一个字母删除,但遇到 mapie 时只需要删除中间的 p 即可,所以答案就是 map 的数量 + pie 的数量 - mapie 的数量

Solution

for _ in range(int(input())):
    n = int(input())
    s = input().strip()
    print(s.count("map") + s.count("pie") - s.count("mapie"))

D. Rudolf and the Ball Game

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

Rudolf and Bernard decided to play a game with their friends. n n n people stand in a circle and start throwing a ball to each other. They are numbered from 1 1 1 to n n n in the clockwise order.

Let’s call a transition a movement of the ball from one player to his neighbor. The transition can be made clockwise or counterclockwise.

Let’s call the clockwise (counterclockwise) distance from player y 1 y_1 y1 to player y 2 y_2 y2 the number of transitions clockwise (counterclockwise) that need to be made to move from player y 1 y_1 y1 to player y 2 y_2 y2. For example, if n = 7 n=7 n=7 then the clockwise distance from 2 2 2 to 5 5 5 is 3 3 3, and the counterclockwise distance from 2 2 2 to 5 5 5 is 4 4 4.

Initially, the ball is with the player number x x x (players are numbered clockwise). On the i i i-th move the person with the ball throws it at a distance of r i r_i ri ( 1 ≤ r i ≤ n − 1 1 \le r_i \le n - 1 1rin1) clockwise or counterclockwise. For example, if there are 7 7 7 players, and the 2 2 2nd player, after receiving the ball, throws it a distance of 5 5 5, then the ball will be caught by either the 7 7 7th player (throwing clockwise) or the 4 4 4th player (throwing counterclockwise). An illustration of this example is shown below.

The game was interrupted after m m m throws due to unexpected rain. When the rain stopped, the guys gathered again to continue. However, no one could remember who had the ball. As it turned out, Bernard remembered the distances for each of the throws and the direction for some of the throws (clockwise or counterclockwise).

Rudolf asks you to help him and based on the information from Bernard, calculate the numbers of the players who could have the ball after m m m throws.

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

The first line of each test case contains three integers n , m , x n, m, x n,m,x ( 2 ≤ n ≤ 1000 2 \le n \le 1000 2n1000, 1 ≤ m ≤ 1000 1 \le m \le 1000 1m1000, 1 ≤ x ≤ n 1 \le x \le n 1xn) — the number of players, the number of throws made, and the number of the player who threw the ball first, respectively.

The next m m m lines contain information about each throw in order. Each of them contains an integer r i r_i ri ( 1 ≤ r i ≤ n − 1 1 \le r_i \le n - 1 1rin1) — the distance at which the i i i-th throw was made, and a symbol c i c_i ci, equal to ‘0’, ‘1’, or ‘?’:

  • if c i c_i ci=‘0’, then the i i i-th throw was made clockwise,
  • if c i c_i ci=‘1’, then the i i i-th throw was made counterclockwise,
  • if c i c_i ci=‘?’, then Bernard does not remember the direction and the i i i-th throw could have been made either clockwise or counterclockwise.

It is guaranteed that the sum n ⋅ m n \cdot m nm ( n n n multiplied by m m m) 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 the number of players k k k ( 1 ≤ k ≤ n 1 \le k \le n 1kn) who could have the ball at the end of the game.

In the next line, output k k k numbers b i b_i bi ( 1 ≤ b i ≤ n 1 \le b_i \le n 1bin) — the numbers of the players in increasing order. All numbers must be different.

Example

input

5
6 3 2
2 ?
2 ?
2 ?
12 1 2
3 1
10 7 4
2 ?
9 1
4 ?
7 0
2 0
8 1
5 ?
5 3 1
4 0
4 ?
1 ?
4 1 1
2 ?

output

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

Note

Below is an illustration of three throws for the first test case. The arrows denote possible throw directions. Players who could have the ball after the throw are highlighted in gray.

Tutorial

从初始值开始,根据情况暴力模拟出每种情况即可

Solution

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

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

void solve() {
    int n, m, x;
    cin >> n >> m >> x;
    vector<int> now(1, x);
    while (m--) {
        int r;
        char c;
        set<int> s;
        vector<int> mid(now);
        cin >> r >> c;
        if (c == '0') {
            for (int ai : mid) {
                s.insert((ai + r) % n ? (ai + r) % n : n);
            }
        } else if (c == '1') {
            for (int ai : mid) {
                s.insert((ai - r + n) % n ? (ai - r + n) % n : n);
            }
        } else {
            for (int ai : mid) {
                s.insert((ai + r) % n ? (ai + r) % n : n);
                s.insert((ai - r + n) % n ? (ai - r + n) % n : n);
            }
        }
        now.clear();
        for (int ai : s) {
            now.emplace_back(ai);
        }
    }
    cout << now.size() << endl;
    for (int ai : now) {
        cout << ai << " \n"[ai == now.back()];
    }
}

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

E. Rudolf and k Bridges

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

Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem.

The river is a grid of n n n rows and m m m columns. The intersection of the i i i-th row and the j j j-th column contains the number a i , j a_{i,j} ai,j — the depth in the corresponding cell. All cells in the first and last columns correspond to the river banks, so the depth for them is 0 0 0.

The river may look like this.

Rudolf can choose the row ( i , 1 ) , ( i , 2 ) , … , ( i , m ) (i,1), (i,2), \ldots, (i,m) (i,1),(i,2),,(i,m) and build a bridge over it. In each cell of the row, he can install a support for the bridge. The cost of installing a support in the cell ( i , j ) (i,j) (i,j) is a i , j + 1 a_{i,j}+1 ai,j+1. Supports must be installed so that the following conditions are met:

  1. A support must be installed in cell ( i , 1 ) (i,1) (i,1);
  2. A support must be installed in cell ( i , m ) (i,m) (i,m);
  3. The distance between any pair of adjacent supports must be no more than d d d. The distance between supports ( i , j 1 ) (i, j_1) (i,j1) and ( i , j 2 ) (i, j_2) (i,j2) is ∣ j 1 − j 2 ∣ − 1 |j_1 - j_2| - 1 j1j21.

Building just one bridge is boring. Therefore, Rudolf decided to build k k k bridges on consecutive rows of the river, that is, to choose some i i i ( 1 ≤ i ≤ n − k + 1 1 \le i \le n - k + 1 1ink+1) and independently build a bridge on each of the rows i , i + 1 , … , i + k − 1 i, i + 1, \ldots, i + k - 1 i,i+1,,i+k1. Help Rudolf minimize the total cost of installing supports.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 3 ) (1 \le t \le 10^3) (1t103) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains four integers n n n, m m m, k k k, and d d d ( 1 ≤ k ≤ n ≤ 100 1 \le k \le n \le 100 1kn100, 3 ≤ m ≤ 2 ⋅ 1 0 5 3 \le m \le 2 \cdot 10^5 3m2105, 1 ≤ d ≤ m 1 \le d \le m 1dm) — the number of rows and columns of the field, the number of bridges, and the maximum distance between supports.

Then follow n n n lines, i i i-th line contains m m m positive integers a i , j a_{i, j} ai,j ( 0 ≤ a i , j ≤ 1 0 6 0 \le a_{i, j} \le 10^6 0ai,j106, a i , 1 = a i , m = 0 a_{i, 1} = a_{i, m} = 0 ai,1=ai,m=0) — the depths of the river cells.

It is guaranteed that the sum of n ⋅ m n \cdot m nm for all sets of input data does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single number — the minimum total cost of supports installation.

Example

input

5
3 11 1 4
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 2 1 2 3 3 2 0
0 1 2 3 5 5 5 5 5 2 0
4 4 2 1
0 3 3 0
0 2 1 0
0 1 2 0
0 3 3 0
4 5 2 5
0 1 1 1 0
0 2 2 2 0
0 2 1 1 0
0 3 2 1 0
1 8 1 1
0 10 4 8 4 4 2 0
4 5 3 2
0 8 4 4 0
0 3 4 8 0
0 8 1 10 0
0 10 1 5 0

output

4
8
4
15
14

Note

In the first test case, it is most profitable to build a bridge on the second row.

It is not a top view, but side view: gray cells — bridge itself, white cells are empty, black cells — supports, blue cells — water, brown cells — river bottom.

In the second test case, it is most profitable to build bridges on the second and third rows. The supports will be placed in cells ( 2 , 3 ) (2, 3) (2,3), ( 3 , 2 ) (3, 2) (3,2), and on the river banks.

In the third test case the supports can be placed along the river banks.

Tutorial

通过单调队列(可见滑动窗口)计算出每一行建桥所需要的最小价格,再通过前缀和计算出连续 k k k​ 行的最小总和

队列前面所有大于窗口大小(长度大于 d d d)的值全部 pop 出去,此时第一个值必定是范围内最小的花费,直接加上当前位置的花费后重新 push 进队列中即可,最后遍历队列前面,第一个坐标为 m − 1 m - 1 m1 的花费即为这一行的最小花费

Solution

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

#define ff first
#define ss second
#define endl '\n'
#define int long long
#define PII pair<int, int>

void solve() {
    int n, m, k, d;
    cin >> n >> m >> k >> d;
    vector<int> pre(n);
    for (int i = 0; i < n; ++i) {
        vector<int> a(m);
        for (int j = 0; j < m; ++j) {
            cin >> a[j];
        }
        priority_queue<PII, vector<PII>, greater<PII>> q;
        q.emplace(1, 0);
        for (int j = 1; j < m; ++j) {
            while (q.size() and q.top().ss < j - d - 1) {
                q.pop();
            }
            q.emplace(q.top().ff + a[j] + 1, j);
        }
        while (q.size() and q.top().ss != m - 1) {
            q.pop();
        }
        pre[i] = q.top().ff;
    }
    int cnt = accumulate(pre.begin(), pre.begin() + k, 0ll);
    int ans = cnt;
    for (int i = k; i < n; ++i) {
        cnt += pre[i] - pre[i - k];
        ans = min(ans, cnt);
    }
    cout << ans << endl;
}

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

F. Rudolf and Imbalance

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

Rudolf has prepared a set of n n n problems with complexities a 1 < a 2 < a 3 < ⋯ < a n a_1 < a_2 < a_3 < \dots < a_n a1<a2<a3<<an. He is not entirely satisfied with the balance, so he wants to add at most one problem to fix it.

For this, Rudolf came up with m m m models of problems and k k k functions. The complexity of the i i i-th model is d i d_i di, and the complexity of the j j j-th function is f j f_j fj. To create a problem, he selects values i i i and j j j ( 1 ≤ i ≤ m 1 \le i \le m 1im, 1 ≤ j ≤ k 1 \le j \le k 1jk) and by combining the i i i-th model with the j j j-th function, he obtains a new problem with complexity d i + f j d_i + f_j di+fj (a new element is inserted into the array a a a).

To determine the imbalance of the set, Rudolf sorts the complexities of the problems in ascending order and finds the largest value of a i − a i − 1 a_i - a_{i - 1} aiai1 ( i > 1 i > 1 i>1).

What is the minimum value of imbalance that Rudolf can achieve by adding at most one problem, created according to the described rules?

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 testcases.

The first line of each test case contains three integers n n n, m m m, and k k k ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105, 1 ≤ m , k ≤ 2 ⋅ 1 0 5 1 \le m, k \le 2 \cdot 10^5 1m,k2105) — the number of prepared problems, the number of models, and the number of functions, respectively.

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, \dots a_n a1,a2,a3,an ( 1 ≤ a i ≤ 2 ⋅ 1 0 9 1 \le a_i \le 2 \cdot 10^9 1ai2109, a i < a i + 1 a_i < a_{i+1} ai<ai+1) — the complexities of the prepared problems.

The third line of each test case contains m m m integers d 1 , d 2 , d 3 , … d m d_1, d_2, d_3, \dots d_m d1,d2,d3,dm ( 1 ≤ d i ≤ 1 0 9 1 \le d_i \le 10^9 1di109) — the complexities of the models.

The fourth line of each test case contains k k k integers f 1 , f 2 , f 3 , … f k f_1, f_2, f_3, \dots f_k f1,f2,f3,fk ( 1 ≤ f i ≤ 1 0 9 1 \le f_i \le 10^9 1fi109) — the complexities of the functions.

It is guaranteed that the sum of n n n over all testcases does not exceed 1 0 5 10^5 105.

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

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

Output

For each testcase, output a single number — the minimum imbalance that Rudolf can achieve.

Example

input

7
5 5 5
5 10 15 20 26
11 14 16 13 8
16 4 5 3 1
7 6 5
1 4 7 10 18 21 22
2 3 5 7 4 2
6 8 9 3 2
7 6 5
1 4 7 10 18 21 22
2 3 5 7 4 2
6 8 13 3 2
5 6 3
2 10 13 20 25
11 6 10 16 14 5
6 17 15
4 2 2
11 12 14 15
19 14
10 6
8 4 2
3 10 16 18 21 22 29 30
9 13 16 15
4 2
2 4 7
4 21
4 15 14 5
20 1 15 1 12 5 11

output

5
4
5
8
2
7
11

Tutorial

二分答案出可能的答案,通过双指针判断出是否能构造出满足题意的值使得数组 a a a 中的差值满足条件

需要注意只能插入一个值,如果 a a a 中有两个不同位置的差值都大于 m i d mid mid,则不能满足题意

Solution

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

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

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n), d(m), f(k);
    for (int &ai : a) {
        cin >> ai;
    }
    for (int &di : d) {
        cin >> di;
    }
    for (int &fi : f) {
        cin >> fi;
    }
    sort(d.begin(), d.end());
    sort(f.begin(), f.end());

    function<bool(int)> check = [&](int mid) -> bool {
        int cnt = 0, idx = 0;
        for (int i = 1; i < n; ++i) {
            if (a[i] - a[i - 1] > mid) {
                if (++cnt > 1) { // 需要分割的区间最多只能有一个
                    return false;
                } 
                idx = i;
            }
        }
        if (not cnt) { // 已经满足条件
            return true;
        }
        int down = a[idx] - mid, up = a[idx - 1] + mid; // 肯定是把数组塞到这个区间内
        for (int left = 0, right = k - 1; left < m; ++left) {
            while (~right and d[left] + f[right] > up) {
                --right;
            }
            if (~right and d[left] + f[right] >= down) {
                return true;
            }
        }
        return false;
    };

    
    int left = 0, right = 2e9 + 1;
    while (left + 1 < right) { // 二分答案
        int mid = (left + right) >> 1;
        (check(mid) ? right : left) = mid;
    }
    cout << left + 1 << endl;
}

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

G. Rudolf and Subway

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

Building bridges did not help Bernard, and he continued to be late everywhere. Then Rudolf decided to teach him how to use the subway.

Rudolf depicted the subway map as an undirected connected graph, without self-loops, where the vertices represent stations. There is at most one edge between any pair of vertices.

Two vertices are connected by an edge if it is possible to travel directly between the corresponding stations, bypassing other stations. The subway in the city where Rudolf and Bernard live has a color notation. This means that any edge between stations has a specific color. Edges of a specific color together form a subway line. A subway line cannot contain unconnected edges and forms a connected subgraph of the given subway graph.

An example of the subway map is shown in the figure.

Rudolf claims that the route will be optimal if it passes through the minimum number of subway lines.

Help Bernard determine this minimum number for the given departure and destination stations.

Input

The first line contains an 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 descriptions of the test cases.

The first line of each test case contains two integers n n n and m m m ( 2 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ m ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5, 1 \le m \le 2 \cdot 10^5 2n2105,1m2105) — the number of subway stations and the number of direct routes between stations (i.e., graph edges).

This is followed by m m m lines — the description of the edges. Each line of the description contains three integers u u u, v v v, and c c c ( 1 ≤ u , v ≤ n , u ≠ v , 1 ≤ c ≤ 2 ⋅ 1 0 5 1 \le u, v \le n, u \ne v, 1 \le c \le 2 \cdot 10^5 1u,vn,u=v,1c2105) — the numbers of the vertices between which there is an edge, and the color of this edge. It is guaranteed that edges of the same color form a connected subgraph of the given subway graph. There is at most one edge between a pair of any two vertices.

This is followed by two integers b b b and e e e ( 1 ≤ b , e ≤ n 1 \le b, e \le n 1b,en) — the departure and destination stations.

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

Output

For each testcase, output a single integer — the minimum number of subway lines through which the route from station b b b to station e e e can pass.

Example

input

8
6 6
1 2 1
2 3 1
5 2 2
2 4 2
4 6 2
3 6 3
1 3
6 6
1 2 1
2 3 1
5 2 2
2 4 2
4 6 2
3 6 3
1 6
6 6
1 2 1
2 3 1
5 2 2
2 4 2
4 6 2
3 6 3
6 6
4 3
1 2 1
1 3 1
4 1 1
2 3
6 7
1 2 43
1 3 34
4 6 43
6 3 43
2 3 43
5 3 43
4 5 43
1 6
7 9
2 4 1
3 6 1
2 3 5
1 7 1
4 7 1
2 5 4
5 4 4
3 4 1
3 7 1
5 3
6 5
6 5 83691
4 1 83691
5 4 83691
3 2 83691
4 3 83691
5 1
6 7
6 1 83691
6 2 83691
2 5 83691
5 6 83691
2 3 83691
5 4 83574
3 5 83691
1 4

output

1
2
0
1
1
2
1
2

Note

The subway graph for the first example is shown in the figure in the problem statement.

In the first test case, from vertex 1 1 1 to vertex 3 3 3, you can travel along the path 1 → 2 → 3 1 \rightarrow 2 \rightarrow 3 123, using only the green line.

In the second test case, from vertex 1 1 1 to vertex 6 6 6, you can travel along the path 1 → 2 → 3 → 6 1 \rightarrow 2 \rightarrow 3 \rightarrow 6 1236, using the green and blue lines.

In the third test case, there is no need to travel from vertex 6 6 6 to the same vertex, so the number of lines is 0 0 0.

In the fourth test case, all edges of the graph belong to one line, so the answer is 1 1 1.

Tutorial

对每个颜色建虚拟点,若在点 u u u 和点 v v v 之间有条边,则在其中添加一个新点 c c c 表示这个条线的颜色,其中点 u u u 和点 v v v 到颜色点 c c c 的距离是 1 1 1,颜色点 c c c 到点 u u u 和点 v v v 的距离是 0 0 0,然后对这个新图进行 D i j k s t r a Dijkstra Dijkstra,其中初始点 b b b 的距离为 0 0 0,结束点 e e e 的距离即为答案

Solution

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

#define ff first
#define ss second
#define endl '\n'
#define int long long
#define PII pair<int, int>

const int INF = 0x3f3f3f3f;

void solve() {
    int n, m, b, e;
    cin >> n >> m;
    vector<vector<PII>> g(n + m + 1);
    map<int, int> mp;
    while (m--) {
        int u, v, c;
        cin >> u >> v >> c;
        if (not mp.count(c)) { // 对每个颜色建一个虚拟点
            mp[c] = ++n;
        }
        // 每到一个新颜色就加一
        g[u].emplace_back(mp[c], 1);
        g[v].emplace_back(mp[c], 1);
        g[mp[c]].emplace_back(u, 0);
        g[mp[c]].emplace_back(v, 0);
    }
    cin >> b >> e;
    function<int()> dijkstra = [&]() -> int {
        priority_queue<PII, vector<PII>, greater<PII>> q;
        vector<int> dis(n + 1, INF);
        vector<bool> memo(n + 1);
        dis[b] = 0;
        q.emplace(0, b);
        while (q.size()) {
            auto [dist, u] =  q.top();
            q.pop();
            if (u == e) {
                return dis[e];
            }
            if (memo[u]) {
                continue;
            }
            memo[u] = true;
            for (auto [v, w] : g[u]) {
                dis[v] = min(dis[v], dist + w);
                q.emplace(dist + w, v);
            }
        }
        return 0;
    };
    
    cout << dijkstra() << endl;
}

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

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值