Codeforces Round #625 (Div. 2)
A. Contest for Robots
Polycarp is preparing the first programming contest for robots. There are problems in it, and a lot of robots are going to participate in it. Each robot solving the problem$ i$ gets$ p_i$ points, and the score of each robot in the competition is calculated as the sum of over all problems$ i$ solved by it. For each problem, is an integer not less than$ 1$.
Two corporations specializing in problem-solving robot manufacturing, “Robo-Coder Inc.” and “BionicSolver Industries”, are going to register two robots (one for each corporation) for participation as well. Polycarp knows the advantages and flaws of robots produced by these companies, so, for each problem, he knows precisely whether each robot will solve it during the competition. Knowing this, he can try predicting the results — or manipulating them.
For some reason (which absolutely cannot involve bribing), Polycarp wants the “Robo-Coder Inc.” robot to outperform the “BionicSolver Industries” robot in the competition. Polycarp wants to set the values of in such a way that the “Robo-Coder Inc.” robot gets strictly more points than the “BionicSolver Industries” robot. However, if the values of will be large, it may look very suspicious — so Polycarp wants to minimize the maximum value of over all problems. Can you help Polycarp to determine the minimum possible upper bound on the number of points given for solving the problems?
Input
The first line contains one integer — the number of problems.
The second line contains$ n$ integers$ r_1, r_2, …, r_n (0≤r_i≤1)$. means that the “Robo-Coder Inc.” robot will solve the -th problem,$ r_i=0$ means that it won’t solve the $ i$-th problem.
The third line contains integers . means that the “BionicSolver Industries” robot will solve the -th problem, $b_i=0 $means that it won’t solve the -th problem.
Output
If “Robo-Coder Inc.” robot cannot outperform the “BionicSolver Industries” robot by any means, print one integer .
Otherwise, print the minimum possible value of , if all values of$ p_i$ are set in such a way that the “Robo-Coder Inc.” robot gets strictly more points than the “BionicSolver Industries” robot.
Examples
input
5
1 1 1 0 0
0 1 1 1 1
output
3
input
3
0 0 0
0 0 0
output
-1
input
4
1 1 1 1
1 1 1 1
output
-1
input
9
1 0 0 0 0 0 0 0 1
0 1 1 0 1 1 1 1 0
output
4
Note
In the first example, one of the valid score assignments is$ p=[3,1,3,1,1]$. Then the “Robo-Coder” gets points, the “BionicSolver” — points.
In the second example, both robots get$ 0$ points, and the score distribution does not matter.
In the third example, both robots solve all problems, so their points are equal.
Translation
两个机器人比赛,已知两个机器人分别会做出哪几道题,设计每一题的分数,使得第一个机器人赢,要求所有题目分数最大值最小化。
Idea
我们不会关心平局,只会关心能分出胜负的题目。假设第一个机器人能答出而第二个机器人不能答出的题目有,而第二个机器人能答出而第一个机器人不能答出的题目有,两者平局的题有显然,当,那么无论怎么改变两者最后都是平局;当如果,那么只需要将每道题设置为1;如果,那么将其中某一道题设置为2就能使第一个机器人胜出;如果,假如的话,那么第一个机器人必输,假如,那么我们将所有设置为1分,将分平均分配到个题中即可。
Code
1 |
|
B. Journey Planning
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from to $ n$.
Tanya plans her journey as follows. First of all, she will choose some city c1 to start her journey. She will visit it, and after that go to some other city , then to some other city , and so on, until she chooses to end her journey in some city . So, the sequence of visited cities$ [c_1,c_2,…,c_k]$ should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value bi associated with it. If there is only one city in Tanya’s journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities and , the condition $c_{i+1}−c_i=b_{c_{i+1}}−b_{c_i}$ must hold.
For example, if and , there are several three possible ways to plan a journey:
;
;
(a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer — the number of cities in Berland.
The second line contains integers $ b_1, b_2, …, b_n (1≤b_i≤4⋅10^5)$, where is the beauty value of the -th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
input
6
10 7 1 9 10 15
output
26
input
1
400000
output
400000
input
7
8 9 26 11 12 29 14
output
55
Note
The optimal journey plan in the first example is $ c=[2,4,5]$.
The optimal journey plan in the second example is $ c=[1]$.
The optimal journey plan in the third example is .
Translation
给一个有个元素的数组,请设计一个含个元素的数组,要求满足$c_{i+1}−c_i=b_{c_{i+1}}−b_{c_i}$,输出满足条件的$\sum\limits_{i = 1}^m {{b_{{c_i}}}} $的最大值。
Idea
$c_{i+1}−c_i=b_{c_{i+1}}−b_{c_i} \Leftrightarrow b_{c_{i+1}}-c_{i+1}=b_{c_i}-c_i$翻译成人话,中的所有元素使得,也就是说要选取的是中相同的分成一组,每一组分别组成一个数列,我们只要找到其中最大的$\sum\limits_{i = 1}^m {{b_{{c_i}}}} $即可,由于可能性较多,可以用map作离散化处理,也可实现去重。
Code
1 |
|
C. Remove Adjacent
You are given a string consisting of lowercase Latin letters. Let the length of be . You may perform several operations on this string.
In one operation, you can choose some index and remove the -th character of if at least one of its adjacent characters is the previous letter in the Latin alphabet for . For example, the previous letter for ‘b’ is ‘a’, the previous letter for ‘s’ is ‘r’, the letter ‘a’ has no previous letters. Note that after each removal the length of the string decreases by one. So, the index should satisfy the condition during each operation.
For the character adjacent characters are and . The first and the last characters of both have only one adjacent character (unless ).
Consider the following example. Let = “bacabcab”.
- During the first move, you can remove the first character =‘b’ because = ‘a’. Then the string becomes = “acabcab”.
- During the second move, you can remove the fifth character = ‘c’ because = ‘b’. Then the string becomes =“acabab”.
- During the third move, you can remove the sixth character =‘b’ because = ‘a’. Then the string becomes = “acaba”.
- During the fourth move, the only character you can remove is = ‘b’, because = ‘a’ (or = ‘a’). The string becomes = “acaa” and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
Input
The first line of the input contains one integer — the length of .
The second line of the input contains one string consisting of lowercase Latin letters.
Output
Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
Examples
input
8
bacabcab
output
4
input
4
bcda
output
3
input
6
abbbbb
output
5
Note
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is .
In the second example, you can remove all but one character of . The only possible answer follows.
- During the first move, remove the third character = d, becomes bca.
- During the second move, remove the second character = c, becomes ba.
- And during the third move, remove the first character = b, becomes a.
Translation
给一个长度为的字符串,对于其中的每一个字符若相邻的字符只要其中一个是,那么就可以将从其中去掉,问最多可以去掉几个字符。
Idea
每次去除的必须是所有满足条件(存在或)的字符中最大的那个个,只需要每次进行一步这的贪心计算,直到不能改动为止。
因为字符串较短,允许每一次去除操作后扫描一遍剩余字符串,寻找到满足条件的最大。
Code
1 |
|
D. Navigation System
The map of Bertown can be represented as a set of n intersections, numbered from to and connected by one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection to another intersection is the path that starts in , ends in and has the minimum length among all such paths.
Polycarp lives near the intersection and works in a building near the intersection . Every day he gets from to by car. Today he has chosen the following path to his workplace: , where , and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp’s path beforehand (it is fixed), and it is not necessarily one of the shortest paths from to .
Polycarp’s car has a complex navigation system installed in it. Let’s describe how it works. When Polycarp starts his journey at the intersection , the system chooses some shortest path from to and shows it to Polycarp. Let’s denote the next intersection in the chosen path as . If Polycarp chooses to drive along the road from to , then the navigator shows him the same shortest path (obviously, starting from as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection instead, the navigator rebuilds the path: as soon as Polycarp arrives at , the navigation system chooses some shortest path from to and shows it to Polycarp. The same process continues until Polycarp arrives at : if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path ():
When Polycarp starts at , the system chooses some shortest path from to . There is only one such path, it is ;
Polycarp chooses to drive to , which is not along the path chosen by the system. When Polycarp arrives at , the navigator rebuilds the path by choosing some shortest path from to , for example, (note that it could choose );
Polycarp chooses to drive to , which is not along the path chosen by the system. When Polycarp arrives at , the navigator rebuilds the path by choosing the only shortest path from to , which is ;
Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get rebuilds in this scenario. Note that if the system chose instead of during the second step, there would be only rebuild (since Polycarp goes along the path, so the system maintains the path during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp’s path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers and — the number of intersections and one-way roads in Bertown, respectively.
Then lines follow, each describing a road. Each line contains two integers and denoting a road from intersection to intersection . All roads in Bertown are pairwise distinct, which means that each ordered pair appears at most once in these lines (but if there is a road , the road can also appear).
The following line contains one integer — the number of intersections in Polycarp’s path from home to his workplace.
The last line contains integers $p_1, p_2, …, p_k $(, all these integers are pairwise distinct) — the intersections along Polycarp’s path in the order he arrived at them. is the intersection where Polycarp lives (), and is the intersection where Polycarp’s workplace is situated (). It is guaranteed that for every the road from to exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
output
1 2
input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
output
0 0
input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
output
0 3
Translation
给一个个节点,条边的有向图,Polycarp想从走到,中间经过。Polycarp有一个导航,他从出发,导航已经规划了从到的最短路径,但是Polycarp会按照自己想好的路线走,他想的路线和导航想的路线可能不一样,这时导航就会重新规划(rebuild)最短路线。求重新规划路线的最小值和最大值。
Idea
首先解答一个问题,重新规划路线的次数为什么会有最大值和最小值。我们不妨来看看题中所给的例子。
最短路是,但是Polycarp走到了去,于是就重新规划了路线,这时的最短路有两条,即 和 ;如果选择,Polycarp会再次偏离路线,然后导航又会重新规划路线;如果选,这和Polycarp想的一样,这样导航就不会重新规划路线了。
通过上面的例子可以看到,当在需要重新规划路线时,和都会增加;若不需要规划路线但是最短路不止一条,选择的是最接近Polycarp的路线的最短路,选择的是偏离Polycarp的路线的最短路,因此还会再加。
我们可以建一个反图,通过一次得到所有点到的最短路径长度,当不是的父亲节点,说明走的就不是最短路,那就要重建路线;如果走的是最短路,但是最短路有多条,可以选择重建和不重建。
Code
1 |
|