CF 2018.09.07 22:35 题解(赛)

A. Function Height

You are given a set of 2n+12n+1 integer points on a Cartesian plane. Points are numbered from 00 to 2n2n inclusive. Let PiPi be the ii-th point. The xx-coordinate of the point PiPi equals ii. The yy-coordinate of the point PiPi equals zero (initially). Thus, initially Pi=(i,0)Pi=(i,0).

The given points are vertices of a plot of a piecewise function. The jj-th piece of the function is the segment PjPj+1PjPj+1.

In one move you can increase the yy-coordinate of any point with odd xx-coordinate (i.e. such points are P1,P3,…,P2n−1P1,P3,…,P2n−1) by 11. Note that the corresponding segments also change.

For example, the following plot shows a function for n=3n=3 (i.e. number of points is 2⋅3+1=72⋅3+1=7) in which we increased the yy-coordinate of the point P1P1 three times and yy-coordinate of the point P5P5 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 yy-coordinate among all initial points in the plot (i.e. points P0,P1,…,P2nP0,P1,…,P2n). 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+12n+1 vertices and having an area equal to kk. 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 10181018.

Input

The first line of the input contains two integers nn and kk (1≤n,k≤10181≤n,k≤1018) — 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+12n+1 vertices and with an area equals kk. 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 10181018.

Examples

input

4 3

output

1

input

4 12

output

3

input

999999999999999999 999999999999999986

output

1

Note

One of the possible answers to the first example:

The area of this plot is 3, the height of this plot is 1.

There is only one possible answer to the second example:

The area of this plot is 12, the height of this plot is 3.

题意:求三角形面积(懒得说了)

注意:无

代码:

#include<iostream>
using namespace std;
int main() {
	long long int n,s,ans;
	scanf("%lld%lld",&n,&s);
	ans=s/n;
	if(s%n!=0) ans++;
	printf("%lld",ans);
}

 

B. Diagonal Walking v.2

Mikhail walks on a Cartesian plane. He starts at the point (0,0)(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)(0,0), he can go to any of the following points in one move:

  • (1,0)(1,0);
  • (1,1)(1,1);
  • (0,1)(0,1);
  • (−1,1)(−1,1);
  • (−1,0)(−1,0);
  • (−1,−1)(−1,−1);
  • (0,−1)(0,−1);
  • (1,−1)(1,−1).

If Mikhail goes from the point (x1,y1)(x1,y1) to the point (x2,y2)(x2,y2) in one move, and x1≠x2x1≠x2 and y1≠y2y1≠y2, then such a move is called a diagonal move.

Mikhail has qq queries. For the ii-th query Mikhail's target is to go to the point (ni,mi)(ni,mi) from the point (0,0)(0,0) in exactly kiki 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)(0,0) to the point (ni,mi)(ni,mi) in kiki 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 qq (1≤q≤1041≤q≤104) — the number of queries.

Then qq lines follow. The ii-th of these qq lines contains three integers nini, mimi and kiki (1≤ni,mi,ki≤10181≤ni,mi,ki≤1018) — xx-coordinate of the destination point of the query, yy-coordinate of the destination point of the query and the number of moves in the query, correspondingly.

Output

Print qq integers. The ii-th integer should be equal to -1 if Mikhail cannot go from the point (0,0)(0,0) to the point (ni,mi)(ni,mi) in exactly kiki moves described above. Otherwise the ii-th integer should be equal to the the maximum number of diagonal moves among all possible movements.

Example

input

3
2 2 3
4 3 7
10 1 9

output

1
6
-1

Note

One of the possible answers to the first test case: (0,0)→(1,0)→(1,1)→(2,2)(0,0)→(1,0)→(1,1)→(2,2).

One of the possible answers to the second test case: (0,0)→(0,1)→(1,2)→(0,3)→(1,4)→(2,3)→(3,2)→(4,3)(0,0)→(0,1)→(1,2)→(0,3)→(1,4)→(2,3)→(3,2)→(4,3).

In the third test case Mikhail cannot reach the point (10,1)(10,1) in 9 moves.

题意:分析走地图时最多有多少步走对角线

思路:讨论各种情况

代码:

#include<iostream>
#include<math.h>
using namespace std;
int main() {
	long long int num,x,y,n,i,min,ans;
	scanf("%lld",&num);
	for(i=0;i<num;i++) {
		scanf("%lld%lld%lld",&x,&y,&n);
		if(max(x,y)>n) ans=-1;
		else {
			if(abs(x-y)%2==0) {
				if((n-max(x,y))%2==0) ans=n; 
				else ans=n-2;
			}
			else ans=n-1;
		}
		printf("%lld\n",ans);
	}
}

 

D. Vasya and Arrays

Vasya has two arrays AA and BB of lengths nn and mm, 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][1,10,100,1000,10000] Vasya can obtain array [1,1110,10000][1,1110,10000], and from array [1,2,3][1,2,3] Vasya can obtain array [6][6].

Two arrays AA and BB are considered equal if and only if they have the same length and for each valid ii Ai=BiAi=Bi.

Vasya wants to perform some of these operations on array AA, some on array BB, in such a way that arrays AA and BB 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 AA and BBequal.

Input

The first line contains a single integer n (1≤n≤3⋅105)n (1≤n≤3⋅105) — the length of the first array.

The second line contains nn integers a1,a2,⋯,an (1≤ai≤109)a1,a2,⋯,an (1≤ai≤109) — elements of the array AA.

The third line contains a single integer m (1≤m≤3⋅105)m (1≤m≤3⋅105) — the length of the second array.

The fourth line contains mm integers b1,b2,⋯,bm (1≤bi≤109)b1,b2,⋯,bm (1≤bi≤109) - elements of the array BB.

Output

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays AA and BB in such a way that they became equal.

If there is no way to make array equal, print "-1".

Examples

input

5
11 2 3 5 7
4
11 7 3 7

output

3

input

2
1 2
1
100

output

-1

input

3
1 2 3
3
1 2 3

output

3

题意:有两个数组,现在可以将相邻数组元素相加变成一个元素,问是否能通过有限次相加使两个数组相同,若不能,输出-1,若能,输出相加后数组元素个数的最大值。

思路:从左向右找即可。

注意:比赛的时候本来都A了,但是第二天竟然被人Hack了!发现是第37组样例超时了,于是又优化了一下(害我又debug了好久)。

代码:

#include<iostream>
#include<math.h>
long long int n1,n2,a[400005],b[400005];
using namespace std;
int main() {
	long long int suma=0,sumb=0,ans=0,i,j,sa=0,sb=0;
	scanf("%lld",&n1);
	for(i=1;i<=n1;i++) {
		scanf("%lld",&a[i]);
		suma+=a[i];
	} 
	scanf("%lld",&n2);
	for(i=1;i<=n2;i++) {
		scanf("%lld",&b[i]);
		sumb+=b[i];
	} 
	if(suma!=sumb) printf("-1");
	else {
		j=0;
		for(i=1;i<=n1;i++) {
			sa=sa+a[i];
			//printf("sa=%d\n",sa);
			while(j<=n2) {
				//printf("sa=%d  sb=%d\n",sa,sb); 
				if(sb==sa) {
					//printf("相等\n");
					sa=0;
					sb=0;
					ans++;
					//j++;
					//printf("j=%d\n",j);
					break;
				}
				if(sb>sa) break;
				if(sb<sa) {
					j++;
					sb=sb+b[j];
				}
			}
		}
		//printf("sa=%d  sb=%d\n",sa,sb);
		printf("%lld",ans);
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值