Codeforces Round #624 (Div.3)
A. Add Odd or Subtract Even
You are given two positive integers a and b.
In one move, you can change a in the following way:
- Choose any positive odd integer x (x>0) and replace a with a+x;
- choose any positive even integer y (y>0) and replace a with a−y.
You can perform as many such operations as you want. You can choose the same numbers x and y in different moves.
Your task is to find the minimum number of moves required to obtain b from a. It is guaranteed that you can always obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer — the number of test cases.
Then t test cases follow. Each test case is given as two space-separated integers a and b .
Output
For each test case, print the answer — the minimum number of moves required to obtain b from a if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain from .
Example
input
5
2 3
10 10
2 4
7 4
9 3
output
1
0
2
2
1
Note
In the first test case, you can just add 1.
In the second test case, you don’t need to do anything.
In the third test case, you can add 1 two times.
In the fourth test case, you can subtract 4 and add 1.
In the fifth test case, you can just subtract 6.
Translation
给两个数,可以作如下两种操作:
- 给加上奇数;
- 给减去偶数。
问从变化到至少需要几次操作。
Idea
要知道,一个数加上或减去偶数奇偶性不变,加上或减去奇数,奇偶性改变。
因此,若同奇偶且,我们只需;若奇偶性不同且,我们只需。
Code
1 |
|
B. WeirdSort
You are given an array of length .
You are also given a set of distinct positions$ p_1,p_2,…,p_m$, where . The position pi means that you can swap elements and$ a[p_i+1]$. You can apply this operation any number of times for each of the given positions.
Your task is to determine if it is possible to sort the initial array in non-decreasing order $(a_1≤a_2≤⋯≤a_n) $using only allowed swaps.
For example, if and , then we can first swap elements and (because position is contained in the given set$ p$). We get the array . Then we swap and (position is also contained in ). We get the array . Finally, we swap$ a[2]$ and again and get the array , sorted in non-decreasing order.
You can see that if and$ p=[3,2]$ then you cannot sort the array.
You have to answer independent test cases.
Input
The first line of the input contains one integer — the number of test cases.
Then t test cases follow. The first line of each test case contains two integers$ n$ and — the number of elements in a and the number of elements in$ p$. The second line of the test case contains n integers . The third line of the test case contains m integers (, all $ p_i$ are distinct) — the set of positions described in the problem statement.
Output
For each test case, print the answer — “YES” (without quotes) if you can sort the initial array in non-decreasing order$ (a_1≤a_2≤⋯≤a_n)$ using only allowed swaps. Otherwise, print “NO”.
Example
input
6
3 2
3 2 1
1 2
4 2
4 1 2 3
3 2
5 1
1 2 3 4 5
1
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
5 2
2 1 2 3 3
1 4
output
YES
NO
YES
YES
NO
YES
Translation
给一个数列,一个序列。交换是合法的,问能否靠合法交换,得到不下降序列。
Idea
数据规模非常小,考虑暴力。遍历,如果发现,则交换,将上述操作进行次(将取非常大),应该就能得到下降序列了,如果还没有得到,那就是不行。
这是不禁好奇,T最多取多大能保证不TLE,于是做了个测试(我手算二分哪!),把逼近到,时有大概率会TLE。实际上T不需要取这么大。
Code
1 |
|
C. Perform the Combo
You want to perform the combo on your opponent in one popular fighting game. The combo is the string s consisting of n lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in s. I.e. if s=“abca” then you have to press ‘a’, then ‘b’, ‘c’ and ‘a’ again.
You know that you will spend m wrong tries to perform the combo and during the -th try you will make a mistake right after -th button () (i.e. you will press first pi buttons right and start performing the combo from the beginning). It is guaranteed that during the -th try you press all buttons right and finally perform the combo.
I.e. if s=“abca”, m=2 and p=[1,3] then the sequence of pressed buttons will be ‘a’ (here you’re making a mistake and start performing the combo from the beginning), ‘a’, ‘b’, ‘c’, (here you’re making a mistake and start performing the combo from the beginning), ‘a’ (note that at this point you will not perform the combo because of the mistake), ‘b’, ‘c’, ‘a’.
Your task is to calculate for each button (letter) the number of times you’ll press it.
You have to answer independent test cases.
Input
The first line of the input contains one integer — the number of test cases.
Then t test cases follow.
The first line of each test case contains two integers n and m$ (2≤n≤2⋅10^5, 1≤m≤2⋅10^5)$ — the length of s and the number of tries correspondingly.
The second line of each test case contains the string s consisting of n lowercase Latin letters.
The third line of each test case contains m integers — the number of characters pressed right during the -th try.
It is guaranteed that the sum of n and the sum of m both does not exceed$ 2⋅10^5 (∑n≤2⋅10^5, ∑m≤2⋅10^5)$.
It is guaranteed that the answer for each letter does not exceed $ 2⋅10^9$.
Output
For each test case, print the answer — 26 integers: the number of times you press the button ‘a’, the number of times you press the button ‘b’, …, the number of times you press the button ‘z’.
Example
input
3
4 2
abca
1 3
10 5
codeforces
2 8 3 2 9
26 10
qwertyuioplkjhgfdsazxcvbnm
20 10 1 2 3 5 10 5 9 4
output
4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0
2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2
Note
The first test case is described in the problem statement. Wrong tries are “a”, “abc” and the final try is “abca”. The number of times you press ‘a’ is 4, ‘b’ is 2 and ‘c’ is 2.
In the second test case, there are five wrong tries: “co”, “codeforc”, “cod”, “co”, “codeforce” and the final try is “codeforces”. The number of times you press ‘c’ is 9, ‘d’ is 4, ‘e’ is 5, ‘f’ is 3, ‘o’ is 9, ‘r’ is 3 and ‘s’ is 1.
Translation
这题不太好读,看Note对样例的解释就好理解一些。简单来说就是给一个长度为的字符串,字符位置从1开始,给一堆位置,每一次都从出发,走到,次这样的操作26个字母出现了几次。
Idea
这很明显是一个前缀和问题,因为每一次都从出发,出现了重叠的区间。
对每一个位置,维护从开始到出现的个字母个数。
获取的方法很简单,我们只需要先遍历字符串,把第位出现的字母作标记,即,再将前位出现字母的个数向位累加即可,即作一次前缀和操作。
然后只要将次操作出现的字母个数累加即可。当然,最后一次经过整个字符串的操作不算在这次里,需要单独再算一次。
!注意!:慎用初始化,因为有很多 $ casesmemset$进行了多余的初始化,容易TLE。我在此受到血的教训!
Code
1 |
|
如果想避免初始化操作,建议参考下面骚代码
1 |
|
D. Three Integers
You are given three integers .
In one move, you can add +1 or −1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations.
You have to perform the minimum number of such operations in order to obtain three integers such that is divisible by and is divisible by .
You have to answer t independent test cases.
Input
The first line of the input contains one integer — the number of test cases.
The next t lines describe test cases. Each test case is given on a separate line as three space-separated integers and .
Output
For each test case, print the answer. In the first line print res — the minimum number of operations you have to perform to obtain three integers$ A≤B≤C$ such that B is divisible by$ A$ and$ C$ is divisible by . On the second line print any suitable triple $A,B $and .
Example
input
8
1 2 3
123 321 456
5 10 15
15 18 21
100 100 101
1 22 29
3 19 38
6 30 46
output
1
1 1 3
102
114 228 456
4
4 8 16
6
18 18 18
1
100 100 100
7
1 22 22
2
1 19 38
8
6 24 48
Translation
给三个数,每一次操作,可以从三个数中选一个数,进行或两种操作,问至少几次操作使得能被整除,能被整除。输出最小次数和修改后的。
Idea
不是很大,可以考虑暴力构造一下。枚举的上界不能过小,否则就被hack了。
Code
1 |
|
F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point and has a speed vi. It is guaranteed that no two points occupy the same coordinate. All points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as .
Consider two points$ i$ and . Let be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value will be 0.
Your task is to calculate the value$\sum\limits_{{\text{1}} \leqslant i < j \leqslant n} {d(i,j)} $ (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer$ n (2≤n≤2⋅10^5)$ — the number of points.
The second line of the input contains n integers$ x_1,x_2,…,x_n (1≤x_i≤10^8)$, where xi is the initial coordinate of the i-th point. It is guaranteed that all xi are distinct.
The third line of the input contains n integers , where vi is the speed of the i-th point.
Output
Print one integer — the value$\sum\limits_{{\text{1}} \leqslant i < j \leqslant n} {d(i,j)} $ (the sum of minimum distances over all pairs of points).
Examples
input
3
1 3 2
-100 2 3
output
3
input
5
2 1 4 3 5
2 2 2 3 4
output
19
input
2
2 1
-3 0
output
0
Translation
有个动点在一维坐标轴上匀速移动,每个动点给出初始坐标和移动速度。令是在任何可能的时刻(甚至是非整数),这两点之间的最小可能距离。任务是计算$\sum\limits_{{\text{1}} \leqslant i < j \leqslant n} {d(i,j)} $。
Idea
对于所有,最小距离有两种情况:
- 若,两点会在一开始相互靠近,最小距离为0;
- 若,两点互相远离,最小距离即为初态。
为了能够进行上述的讨论,我们要将进行升序排序。然后另开一个数组存所有的速度,排序并去重。利用二分查找速度数组得出小于当前点速度的点个数。开两个树状数组,一个维护速度小于的个数,一个维护速度小于的之和。从左向右扫描一遍所有的点,每次加上,并将状态加入树状数组中维护即可。
这属于树状数组的单点更新和区间查询,假设当前第个点的在所有中排在位;我们需要查询小于的个数,和小于的之和;每次更新时,。
Code
1 |
|