Educational Codeforces Round 50 (Rated for Div. 2)

  • 2018-09-08
  • 35
  • 0

A. Function Height

Description:

You are given a set of 2n+1 integer points on a Cartesian plane. Points are numbered from 0 to 2n inclusive. Let P_i be the i-th point. The x-coordinate of the point P_i equals i. The y-coordinate of the point P_i equals zero (initially). Thus, initially P_i=(i,0).
The given points are vertices of a plot of a piecewise function. The j-th piece of the function is the segment P_{j}P_{j + 1}.
In one move you can increase the y-coordinate of any point with odd x-coordinate (i.e. such points are P_1, P_3, \dots, P_{2n-1}) by 1. Note that the corresponding segments also change.
For example, the following plot shows a function for n=3 (i.e. number of points is 2\cdot3+1=7) in which we increased the y-coordinate of the point P_1 three times and y-coordinate of the point P_5 one time:

Let the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).
Let the height of the plot be the maximum y-coordinate among all initial points in the plot (i.e. points P_0, P_1, \dots, P_{2n}). The height of the plot on the picture above is 3.
Your problem is to say which minimum possible height can have the plot consisting of 2n+1 vertices and having an area equal to k. Note that it is unnecessary to minimize the number of moves.
It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding 10^{18}.

Input:

The first line of the input contains two integers n and k (1 \le n, k \le 10^{18}) — the number of vertices in a plot of a piecewise function and the area we need to obtain.

Output

Print one integer — the minimum possible height of a plot consisting of 2n+1 vertices and with an area equals k. It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding 10^{18}.

Sample Input:

4 3

Sample Output:

1

Sample Input:

4 12

Sample Output:

3

Sample Input:

999999999999999999 999999999999999986

Sample Output:

1

题目链接

使三角形高度最低的达到要求面积,\frac{k}{n}向上取整即可。

AC代码:

B. Diagonal Walking v.2

Description:

Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move:

  • (1, 0);

  • (1, 1);

  • (0, 1);

  • (-1, 1);

  • (-1, 0);

  • (-1, -1);

  • (0, -1);

(1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 \ne x2 and y1 \ne y2, then such a move is called a diagonal move.
Mikhail has q queries. For the i-th query Mikhail’s target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves.
Note that Mikhail can visit any point any number of times (even the destination point!).

Input:

The first line of the input contains one integer q (1 \le q \le 10^4) — the number of queries.
Then q​ lines follow. The i​-th of these q​ lines contains three integers n_i​, m_i​ and k_i​ (1 \le n_i, m_i, k_i \le 10^{18}​) — x​-coordinate of the destination point of the query, y​-coordinate of the destination point of the query and the number of moves in the query, correspondingly.

Output

Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements.

Sample Input:

3
2 2 3
4 3 7
10 1 9

Sample Output:

1
6
-1

题目链接

求在直角坐标系上从原点用k步走向目标点的最大斜线行走次数。

先用min(n,m)步次走向目标点所在长边,再计算当前位置和目标点的水平或竖直距离,若此距离为偶数并且剩余步数也为偶数则剩下的步数皆可斜线行走,若剩余步数为奇数则应该在原点时就应该水平行走一次、竖直行走一次,再进行计算,若水平或竖直距离为奇数则应该水平或竖直(在长边上)向目标点走一次,剩余步数皆可斜线行走。

例如此图目标点为(2,4),若K=4,应先向右上行走两步(到达(2,2))到达目标点所在长边(x=2),之后判断水平或竖直距离为2剩余步数为2则剩余步数皆可斜线行走,可先向右上行走一次到达(3,3),再向左上行走到达目标点(2,4),若K为更大的偶数,设到达目标点后剩余步数为m,则可在到达目标点后任意方向行走\frac{m}{2}次后原路返回。

若在斜线行走到目标点所在长边后判断水平距离或竖直距离为偶数,且剩余步数为奇数则应该在原点开始时水平竖直各走一次,之后再把(1,1)看做原点按照规则进行行走,如图:

若到达目标点所在长边之后水平或竖直距离为奇数则应该水平或竖直(在长边上)向目标点走一次之后再按照偶数规则行走,如图:

AC代码:

D. Vasya and Arrays

Description:

Vasya has two arrays A and B of lengths n and m, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [1, 10, 100, 1000, 10000] Vasya can obtain array [1, 1110, 10000], and from array [1, 2, 3] Vasya can obtain array [6].
Two arrays A and B are considered equal if and only if they have the same length and for each valid i A_i = B_i.
Vasya wants to perform some of these operations on array A, some on array B, in such a way that arrays A and B become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays A and B equal.

Input:

The first line contains a single integer n~(1 \le n \le 3 \cdot 10^5) — the length of the first array.
The second line contains n integers a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9) — elements of the array A.
The third line contains a single integer m~(1 \le m \le 3 \cdot 10^5) — the length of the second array.
The fourth line contains m integers b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9) – elements of the array B.

Output

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays A and B in such a way that they became equal.
If there is no way to make array equal, print “-1”.

Sample Input:

5
11 2 3 5 7
4
11 7 3 7

Sample Output:

3

Sample Input:

2
1 2
1
100

Sample Output:

-1

Sample Input:

3
1 2 3
3
1 2 3

Sample Output:

3

题目链接

两个数列,对第一个数列进行数字合成(连续数字合成为其之和),求使得两个数列完全相等(每位数字相等)的最大长度。

对两个数列求其每个位置上的前缀和,判断每个位置上两个数列的前缀和是否相等,若相等则进行合成、记次。

AC代码:

评论

还没有任何评论,你来说两句吧