浪在ACM集训队寒假集训第一场

C CodeForces 1036A Function Height

  You are given a set of 2n+1 integer points on a Cartesian plane. Points are numbered from 0 to 2n inclusive. Let Pi be the i-th point. The x-coordinate of the point Pi equals i. The y-coordinate of the point Pi equals zero (initially). Thus, initially Pi=(i,0).
  The given points are vertices of a plot of a piecewise function. The j-th piece of the function is the segment PjPj+1.
  In one move you can increase the y-coordinate of any point with odd x-coordinate (i.e. such points are P1,P3,…,P2n−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⋅3+1=7) in which we increased the y-coordinate of the point P1 three times and y-coordinate of the point P5 one time:
1
  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 P0,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+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 1018.

Input

  The first line of the input contains two integers n and k (1≤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+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 1018.

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:
2
  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.

解析

  思维题目,嗯,考你思维的.~其实是考英语吧
  在一个平面直角坐标系里,给你n个三角形的顶点,要求出一个h,来使得三角形面积和为k.
  啥意思,就是说.找x个三角形,每个三角形的高为​(x<n),底边长为2.要求出图1
1
在所有 1 里每个情况最大值的最小值.
如果所给顶点比面积大,那么高为1时,必定成立.
如果不是,若面积整除顶点,那么当h=k/n时,该情况的最大值最小.
若不整除顶点.那么就令h=k/n+1

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<map>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

int main()
{
	long long k, s;
	cin >> k >> s;
	if (k >= s)
		cout << 1 << endl;
	else
		if (s%k == 0)
			cout << s / k << endl;
		else
			cout << s / k + 1 << endl;
	return 0;
}

D CodeForces 1036B Diagonal Walking v.2

  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≠x2 and y1≠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 (ni,mi) from the point (0,0) in exactly ki 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 (ni,mi) in ki 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≤q≤104) — the number of queries.
  Then q lines follow. The i-th of these q lines contains three integers ni, mi and ki (1≤ni,mi,ki≤1018) — 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 (ni,mi) in exactly ki moves described above. Otherwise the i-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).
  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).
  In the third test case Mikhail cannot reach the point (10,1) in 9 moves.

解析

  这是一个思维题,没错,是思维题,不是搜索题.
  题目大意是说这货脑抽,只想沿着对角线走,问给你k个步数,能否k次从(0,0)走到(n,m).如果可以,问最多能走对角线几次.不可以就输出-1.
  注,你可以走过(n,m),在走回来==
  搜素题对不对,多组输入,bfs,对角线优先搜索,之后步数超过k次就剪枝,nice啊,但是会mle.
  所以说思维题目啊,是吧,*******.
  首先可知,当max(n,m)>k的时候,必定是走不过去的.
  其次,如果abs(m-n)是大于0的偶数.我们可以走到大于n或者m的点,再走回来.如果是奇数,那么我们没法通过折返来到达目的地.那么我们就需要横着或者竖着走一次.于是k–
  如下图1,分别对应上面两种情况,第一种是从(0,0)->(4,3).第二种是从(0,0)->(4,2)
1
  之后,当abs(m-n)是偶数的时候,如果k-max(m,n)是偶数.那么我们可以通过终点和相邻对角线的"秘籍`反复横跳"来消磨掉剩余的步数,如果不是,我们需要横着走一步,之后竖着再走一步,来消磨掉剩余的步数.

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

int main()
{
	int p;
	cin >> p;
	while (p--) {
		long long n, m,k;
		cin >> n >> m>>k;
		if (n < m)
			swap(n, m);
		if (n > k) {
			cout << -1 << endl;
			continue;
		}
		else if (n - m & 1)
			k--;
		else if (k - n & 1)
			k -= 2;
		cout << k<<endl;
	}
	return 0;
}
发布了62 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/UnKfrozen/article/details/86537995