Codeforces Round 920 (Div. 3) A B C D E

A. Square

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

A square of positive (strictly greater than 0 0 0) area is located on the coordinate plane, with sides parallel to the coordinate axes. You are given the coordinates of its corners, in random order. Your task is to find the area of the square.

Input

Each test consists of several testcases. The first line contains one integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of testcases. The following is a description of the testcases.

Each testcase contains four lines, each line contains two integers x i , y i x_i, y_i xi,yi ( − 1000 ≤ x i , y i ≤ 1000 -1000\le x_i, y_i\le 1000 1000xi,yi1000), coordinates of the corners of the square.

It is guaranteed that there is a square with sides parallel to the coordinate axes, with positive (strictly greater than 0 0 0) area, with corners in given points.

Output

For each test case, print a single integer, the area of the square.

Example

input

3
1 2
4 5
1 5
4 2
-1 1
1 -1
1 1
-1 -1
45 11
45 39
17 11
17 39

output

9
4
784

Tutorial

因为给出的四个点围起来是一个边与坐标轴平行的正方形,所以如果 x x x 的值相等,那 y y y 值的差就是边长,如果 y y y 的值相等,那 x x x 值的差就是边长,直接找出一条边的长度即可

Solution

for _ in range(int(input())):
    a = sorted(tuple(map(int, input().split())) for _ in range(4))
    print((a[1][1] - a[0][1]) ** 2)

B. Arranging Cats

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

In order to test the hypothesis about the cats, the scientists must arrange the cats in the boxes in a specific way. Of course, they would like to test the hypothesis and publish a sensational article as quickly as possible, because they are too engrossed in the next hypothesis about the phone's battery charge.

Scientists have n n n boxes in which cats may or may not sit. Let the current state of the boxes be denoted by the sequence b 1 , … , b n b_1, \dots, b_n b1,,bn: b i = 1 b_i = 1 bi=1 if there is a cat in box number i i i, and b i = 0 b_i = 0 bi=0 otherwise.

Fortunately, the unlimited production of cats has already been established, so in one day, the scientists can perform one of the following operations:

  • Take a new cat and place it in a box (for some i i i such that b i = 0 b_i = 0 bi=0, assign b i = 1 b_i = 1 bi=1).
  • Remove a cat from a box and send it into retirement (for some i i i such that b i = 1 b_i = 1 bi=1, assign b i = 0 b_i = 0 bi=0).
  • Move a cat from one box to another (for some i , j i, j i,j such that b i = 1 , b j = 0 b_i = 1, b_j = 0 bi=1,bj=0, assign b i = 0 , b j = 1 b_i = 0, b_j = 1 bi=0,bj=1).

It has also been found that some boxes were immediately filled with cats. Therefore, the scientists know the initial position of the cats in the boxes s 1 , … , s n s_1, \dots, s_n s1,,sn and the desired position f 1 , … , f n f_1, \dots, f_n f1,,fn.

Due to the large amount of paperwork, the scientists do not have time to solve this problem. Help them for the sake of science and indicate the minimum number of days required to test the hypothesis.

Input

Each test consists of several 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. This is followed by descriptions of the test cases.

Each test case consists of three lines.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105) — the number of boxes.

The second line of each test case contains a string s s s of n n n characters, where the i i i-th character is ‘1’ if there is a cat in the i i i-th box and ‘0’ otherwise.

The third line of each test case contains a string f f f of n n n characters, where the i i i-th character is ‘1’ if there should be a cat in the i i i-th box and ‘0’ otherwise.

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

Output

For each test case, output a single integer on a separate line — the minimum number of operations required to obtain the desired position from the initial position. It can be shown that a solution always exists.

Example

input

6
5
10010
00001
1
1
1
3
000
111
4
0101
1010
3
100
101
8
10011001
11111110

output

2
0
3
2
1
4

Note

In the first test case, you can first move the cat from the first box to the fifth, and then remove the cat from the fourth box.

In the second test case, there is nothing to do — the only cat is already sitting in the correct box.

In the third test case of input data, it takes three days to place a cat in each box.

Tutorial

s s s f f f 不相等的坐标中,统计 01 的数量分别记作 c n t 0 cnt0 cnt0 c n t 1 cnt1 cnt1,如果将一只猫从其中一个盒子移动到另一个盒子,那么那么 c n t 0 cnt0 cnt0 c n t 1 cnt1 cnt1 的数量都会减少 1,如果选择放入一只新猫,那么 c n t 0 cnt0 cnt0 的数量会减少 1,如果选择取出一只猫,那么 c n t 1 cnt1 cnt1 的数量会减少 1,当 c n t 0 cnt0 cnt0 c n t 1 cnt1 cnt1 的数量都为 0 时即符合要求

综上所述,答案为 max ⁡ ( c n t s 和 f 不相等且 s i 为 0 , c n t s 和 f 不相等且 s i 为 1 ) \max(cnt_{s和f不相等且s_i为0},cnt_{s和f不相等且s_i为1}) max(cntsf不相等且si0,cntsf不相等且si1)

Solution

for _ in range(int(input())):
    n = int(input())
    s = input().strip()
    f = input().strip()
    cnt1 = sum(s[i] != f[i] and s[i] == '0' for i in range(n))
    cnt2 = sum(s[i] != f[i] and s[i] == '1' for i in range(n))
    print(max(cnt1, cnt2))


C. Sending Messages

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

Stepan is a very busy person. Today he needs to send n n n messages at moments m 1 , m 2 , … m n m_1, m_2, \dots m_n m1,m2,mn ( m i < m i + 1 m_i < m_{i + 1} mi<mi+1). Unfortunately, by the moment 0 0 0, his phone only has f f f units of charge left. At the moment 0 0 0, the phone is turned on.

The phone loses a a a units of charge for each unit of time it is on. Also, at any moment, Stepan can turn off the phone and turn it on later. This action consumes b b b units of energy each time. Consider turning on and off to be instantaneous, so you can turn it on at moment x x x and send a message at the same moment, and vice versa, send a message at moment x x x and turn off the phone at the same moment.

If at any point the charge level drops to 0 0 0 (becomes ≤ 0 \le 0 0), it is impossible to send a message at that moment.

Since all messages are very important to Stepan, he wants to know if he can send all the messages without the possibility of charging the phone.

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. This is followed by the descriptions of the test cases.

The first line of each test case contains four integers n n n, f f f, a a a, and b b b ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105, 1 ≤ f , a , b ≤ 1 0 9 1 \le f, a, b \le 10^9 1f,a,b109) — the number of messages, the initial phone’s charge, the charge consumption per unit of time, and the consumption when turned off and on sequentially.

The second line of each test case contains n n n integers m 1 , m 2 , … , m n m_1, m_2, \dots, m_n m1,m2,,mn ( 1 ≤ m i ≤ 1 0 9 1 \le m_i \le 10^9 1mi109, m i < m i + 1 m_i < m_{i + 1} mi<mi+1) — the moments at which messages need to be sent.

It is guaranteed that in a test 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 Stepan can send all the messages, and “NO” otherwise.

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

6
1 3 1 5
3
7 21 1 3
4 6 10 13 17 20 26
5 10 1 2
1 2 3 4 5
1 1000000000 1000000000 1000000000
1000000000
3 11 9 6
6 8 10
12 621526648 2585904 3566299
51789 61859 71998 73401 247675 298086 606959 663464 735972 806043 806459 919683

output

NO
YES
YES
NO
NO
YES

Note

In the first test case of the example, at moment 0 0 0, the phone’s charge is 3 3 3. When sending a message at moment 3 3 3 without turning it off, ( 3 − 0 ) ⋅ 1 = 3 (3 - 0) \cdot 1 = 3 (30)1=3 units of charge will be spent. In this case, the charge will drop to 0 0 0 and Stepan will not be able to send the message. When turning off and on, the phone’s charge will decrease by 5 5 5, so it will not be possible to send the message in this way.

In the third test case of the example, at moment 0 0 0, the phone’s charge is 10 10 10. The phone loses 1 1 1 unit of charge per unit of time, and when turned off and on, it loses 2 2 2 units of charge. To send all messages, the following actions can be taken:

  • Turn off the phone at moment 0 0 0 and turn it on at moment 1 1 1, after which 10 − 2 = 8 10 - 2 = 8 102=8 units of charge will remain;
  • send a message at moment 1 1 1;
  • send a message at moment 2 2 2, after which 8 − ( 2 − 1 ) ⋅ 1 = 7 8 - (2 - 1) \cdot 1 = 7 8(21)1=7 units of charge will remain;
  • Turn off the phone at moment 2 2 2 and turn it on at moment 3 3 3, after which 7 − 2 = 5 7 - 2 = 5 72=5 units of charge will remain;
  • send a message at moment 3 3 3;
  • Turn off the phone at moment 3 3 3 and turn it on at moment 4 4 4, after which 5 − 2 = 3 5 - 2 = 3 52=3 units of charge will remain;
  • send a message at moment 4 4 4;
  • Turn off the phone at moment 4 4 4 and turn it on at moment 5 5 5, after which 3 − 2 = 1 3 - 2 = 1 32=1 unit of charge will remain;
  • send a message at moment 5 5 5.

The last (sixth) test set of the example may fail if there is an integer overflow in your solution.

Tutorial

记录从这一次发消息到下一次发消息这个间隔里(题目给的是时间的前缀和,所以直接将前后相减即为时间间隔),是保持开机状态更好还是关机再开机状态更好,对所有情况取最优解求和贪心即可,如果需要的电量大于所剩电量则不能发送所有消息,否则可以发送所有消息

Solution

for _ in range(int(input())):
    n, f, a, b = map(int, input().split())
    m = [0] + list(map(int, input().split()))
    print("YES" if sum(min((m[i + 1] - m[i]) * a, b) for i in range(n)) < f else "NO")

D. Very Different Array

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

Petya has an array a i a_i ai of n n n integers. His brother Vasya became envious and decided to make his own array of n n n integers.

To do this, he found m m m integers b i b_i bi ( m ≥ n m\ge n mn), and now he wants to choose some n n n integers of them and arrange them in a certain order to obtain an array c i c_i ci of length n n n.

To avoid being similar to his brother, Vasya wants to make his array as different as possible from Petya’s array. Specifically, he wants the total difference D = ∑ i = 1 n ∣ a i − c i ∣ D = \sum_{i=1}^{n} |a_i - c_i| D=i=1naici to be as large as possible.

Help Vasya find the maximum difference D D D he can obtain.

Input

Each test consists of multiple test cases. The first line 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 n n n and m m m ( 1 ≤ n ≤ m ≤ 2 ⋅ 1 0 5 1\le n\le m\le 2 \cdot 10^5 1nm2105).

The second line of each test case contains n n n integers a i a_i ai ( 1 ≤ a i ≤ 1 0 9 1\le a_i\le 10^9 1ai109). The third line of each test case contains m m m integers b i b_i bi ( 1 ≤ b i ≤ 1 0 9 1\le b_i\le 10^9 1bi109).

It is guaranteed that in a test, the sum of 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 a single integer — the maximum total difference D D D that can be obtained.

Example

input

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

output

16
0
12
11
10
23
15
25
7

Note

In the first example, Vasya can, for example, create the array ( 1 , 5 , 7 , 2 ) (1, 5, 7, 2) (1,5,7,2). Then the total difference will be D = ∣ 6 − 1 ∣ + ∣ 1 − 5 ∣ + ∣ 2 − 7 ∣ + ∣ 4 − 2 ∣ = 5 + 4 + 5 + 2 = 16 D = |6-1|+|1-5|+|2-7|+|4-2| = 5+4+5+2 = 16 D=∣61∣+∣15∣+∣27∣+∣42∣=5+4+5+2=16.

In the second example, all the integers available to Vasya are equal to 1, so he can only create the array ( 1 , 1 , 1 ) (1, 1, 1) (1,1,1), for which the difference D = 0 D = 0 D=0.

In the third example, Vasya can, for example, create the array ( 5 , 4 , 3 , 2 , 1 ) (5, 4, 3, 2, 1) (5,4,3,2,1). Then the total difference will be D = ∣ 1 − 5 ∣ + ∣ 2 − 4 ∣ + ∣ 3 − 3 ∣ + ∣ 4 − 2 ∣ + ∣ 5 − 1 ∣ = 4 + 2 + 0 + 2 + 4 = 12 D = |1-5|+|2-4|+|3-3|+|4-2|+|5-1| = 4+2+0+2+4 = 12 D=∣15∣+∣24∣+∣33∣+∣42∣+∣51∣=4+2+0+2+4=12.

Tutorial

如题意所示,需要满足 ∑ i = 1 n ∣ a i − c i ∣ \sum_{i=1}^{n} |a_i - c_i| i=1naici 尽可能大,所以就需要 a a a 中最大的 x x x 个元素和 b b b 中最小的 x x x 个元素匹配, a a a 中最小的 n − x n - x nx 个元素和 b b b 中最大的 n − x n - x nx 个元素匹配,此时先将 a a a b b b 排序,问题就变成了在 a a a 中找一个分界点,前面的元素和 b b b 中最大的几个元素匹配,后面的元素和 b b b 中最小的几个元素匹配,此时用前缀和和后缀和计算最大差值,然后直接暴力循环寻找分界点,答案为 max ⁡ 0 < i ≤ n ( p r e i + s u f i ) \max\limits_{0 < i \leq n}(pre_i+ suf_i) 0<inmax(prei+sufi)

Solution

for _ in range(int(input())):
    n, m = map(int, input().split())
    a = sorted(list(map(int, input().split())))
    b = sorted(list(map(int, input().split())))
    pre, suf = [0 for _ in range(n + 1)], [0 for _ in range(n + 1)]
    l, r = 0, m - 1
    for i in range(1, n + 1):
        pre[i] = pre[i - 1] + abs(b[r] - a[i - 1])
        r -= 1
    for i in range(n, 0, -1):
        suf[i - 1] = suf[i] + abs(b[l] - a[i - 1])
        l += 1
    print(max(pre[i] + suf[i] for i in range(n + 1)))

E. Eat the Chip

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

Alice and Bob are playing a game on a checkered board. The board has h h h rows, numbered from top to bottom, and w w w columns, numbered from left to right. Both players have a chip each. Initially, Alice’s chip is located at the cell with coordinates ( x a , y a ) (x_a, y_a) (xa,ya) (row x a x_a xa, column y a y_a ya), and Bob’s chip is located at ( x b , y b ) (x_b, y_b) (xb,yb). It is guaranteed that the initial positions of the chips do not coincide. Players take turns making moves, with Alice starting.

On her turn, Alice can move her chip one cell down or one cell down-right or down-left (diagonally). Bob, on the other hand, moves his chip one cell up, up-right, or up-left. It is not allowed to make moves that go beyond the board boundaries.

More formally, if at the beginning of Alice’s turn she is in the cell with coordinates ( x a , y a ) (x_a, y_a) (xa,ya), then she can move her chip to one of the cells ( x a + 1 , y a ) (x_a + 1, y_a) (xa+1,ya), ( x a + 1 , y a − 1 ) (x_a + 1, y_a - 1) (xa+1,ya1), or ( x a + 1 , y a + 1 ) (x_a + 1, y_a + 1) (xa+1,ya+1). Bob, on his turn, from the cell ( x b , y b ) (x_b, y_b) (xb,yb) can move to ( x b − 1 , y b ) (x_b - 1, y_b) (xb1,yb), ( x b − 1 , y b − 1 ) (x_b - 1, y_b - 1) (xb1,yb1), or ( x b − 1 , y b + 1 ) (x_b - 1, y_b + 1) (xb1,yb+1). The new chip coordinates ( x ′ , y ′ ) (x', y') (x,y) must satisfy the conditions 1 ≤ x ′ ≤ h 1 \le x' \le h 1xh and 1 ≤ y ′ ≤ w 1 \le y' \le w 1yw.

Example game state. Alice plays with the white chip, Bob with the black one. Arrows indicate possible moves.

A player immediately wins if they place their chip in a cell occupied by the other player’s chip. If either player cannot make a move (Alice—if she is in the last row, i.e. x a = h x_a = h xa=h, Bob—if he is in the first row, i.e. x b = 1 x_b = 1 xb=1), the game immediately ends in a draw.

What will be the outcome of the game if both opponents play optimally?

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. This is followed by the description of the test cases.

Each test case consists of a single line containing six integers h h h, w w w, x a x_a xa, y a y_a ya, x b x_b xb, y b y_b yb ( 1 ≤ x a , x b ≤ h ≤ 1 0 6 1 \le x_a, x_b \le h \le 10^6 1xa,xbh106, 1 ≤ y a , y b ≤ w ≤ 1 0 9 1 \le y_a, y_b \le w \le 10^9 1ya,ybw109) — the dimensions of the board and the initial positions of Alice’s and Bob’s chips. It is guaranteed that either x a ≠ x b x_a \ne x_b xa=xb or y a ≠ y b y_a \ne y_b ya=yb.

It is guaranteed that the sum of h h h over all test cases does not exceed 1 0 6 10^6 106.

Output

For each test case, output “Alice” if Alice wins, “Bob” if Bob wins, and “Draw” if neither player can secure a victory. You can output each letter in any case (lowercase or uppercase). For example, the strings “bOb”, “bob”, “Bob”, and “BOB” will be accepted as Bob’s victory.

Example

input

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

output

Alice
Bob
Draw
Draw
Draw
Alice
Draw
Draw
Bob
Alice
Alice
Draw

Tutorial

根据题意,可以发现两个人无论怎么移动他们的棋子一定会向前走一格

  • 如果开局两个棋子在同一列上,两个棋子相隔 l l l 行,如果 l ≤ 0 l \leq 0 l0 那么必定平局,当 l l l 为偶数的时候,Alice和Bob各走了 l 2 \frac {l} 2 2l 步, A l i c e Alice Alice 可能获胜, B o b Bob Bob 必定不可能获胜,当 l l l 为奇数的时候, A l i c e Alice Alice 走了 ⌊ l + 1 2 ⌋ ⌊\frac {l+1} 2⌋ 2l+1 B o b Bob Bob 各走了 ⌊ l 2 ⌋ ⌊\frac {l} 2⌋ 2l 步, B o B BoB BoB 可能获胜, A l i c e Alice Alice 必定不可能获胜,并且此时败者移动一步,胜者就能同样移动一步使得两个棋子一直保持在同一列上,这时胜者必胜

  • 如果开局两个棋子在不同列上,如果 A l i c e Alice Alice 是有把握获胜并且两个棋子列数就差 1,那么 A l i c e Alice Alice 可以选择移动到和 B o b Bob Bob 同一列的位置上,之后和上面一样,这时 A l i c e Alice Alice 必胜。反之如果 A l i c e Alice Alice 是可能的败者并且两个棋子列数就差1,那么 A l i c e Alice Alice 一定会想办法逃离 B o b Bob Bob 棋子所在列,即向远离 B o b Bob Bob 棋子所在列的方向移动,如果到地图边界的距离足够远,那么 A l i c e Alice Alice 可以一直耗到两个棋子行间隔(即 l l l )为0,这时候 B o b Bob Bob 吃不到 A l i c e Alice Alice,之后就永远吃不到了,达成平局。否则胜者获胜。如果到达地图边界,则无路可逃,只能呆在地图边缘等待失败到来

  • B o b Bob Bob 同理

Solution

for _ in range(int(input())):
    h, w, xa, ya, xb, yb = map(int, input().split())
    if xa >= xb:
        print("Draw")
        continue
    d = abs(xa - xb)
    if d & 1:
        if ya > yb:
            ya -= (d + 1) // 2
            yb = max(yb - d // 2, 1)
            print("Alice" if ya <= yb else "Draw")
        else:
            ya += (d + 1) // 2
            yb = min(yb + d // 2, w)
            print("Alice" if ya >= yb else "Draw")
    else:
        if yb > ya:
            yb -= d // 2
            ya = max(ya - (d + 1) // 2, 1)
            print("Bob" if ya >= yb else "Draw")
        else:
            ya = min(ya + (d + 1) // 2, w)
            yb += d // 2
            print("Bob" if ya <= yb else "Draw")

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值