B. Cow and Friend----------------------思维

Bessie has way too many friends because she is everyone’s favorite cow! Her new friend Rabbit is trying to hop over so they can play!

More specifically, he wants to get from (0,0) to (x,0) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its n favorite numbers: a1,a2,…,an. What is the minimum number of hops Rabbit needs to get from (0,0) to (x,0)? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.

Recall that the Euclidean distance between points (xi,yi) and (xj,yj) is (xi−xj)2+(yi−yj)2−−−−−−−−−−−−−−−−−−√.

For example, if Rabbit has favorite numbers 1 and 3 he could hop from (0,0) to (4,0) in two hops as shown below. Note that there also exists other valid ways to hop to (4,0) in 2 hops (e.g. (0,0) → (2,−5–√) → (4,0)).
在这里插入图片描述

Here is a graphic for the first example. Both hops have distance 3, one of Rabbit’s favorite numbers.
In other words, each time Rabbit chooses some number ai and hops with distance equal to ai in any direction he wants. The same number can be used multiple times.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases. Next 2t lines contain test cases — two lines per test case.

The first line of each test case contains two integers n and x (1≤n≤105, 1≤x≤109) — the number of favorite numbers and the distance Rabbit wants to travel, respectively.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — Rabbit’s favorite numbers. It is guaranteed that the favorite numbers are distinct.

It is guaranteed that the sum of n over all the test cases will not exceed 105.

Output
For each test case, print a single integer — the minimum number of hops needed.

Example
inputCopy

4
2 4
1 3
3 12
3 4 5
1 5
5
2 10
15 4
outputCopy
2
3
1
2
Note
The first test case of the sample is shown in the picture above. Rabbit can hop to (2,5–√), then to (4,0) for a total of two hops. Each hop has a distance of 3, which is one of his favorite numbers.

In the second test case of the sample, one way for Rabbit to hop 3 times is: (0,0) → (4,0) → (8,0) → (12,0).

In the third test case of the sample, Rabbit can hop from (0,0) to (5,0).

In the fourth test case of the sample, Rabbit can hop: (0,0) → (5,102–√) → (10,0).

题意:
给定起点(0,0) n个喜欢的数,终点(x,0)。问至少跳多少步能跳到终点
注意:每次跳必须跳自己喜欢的数

解析:
最优的就是每次跳最大步,所以找出n个喜欢的数中的最大值记为mx。

如果mx>x,那么一定跳两步,因为mx>x 一定构成锐角三角形,所以需要两步。
如果mx==x 只需要一步即可

如果mx<x 那么我们需要最大化的跳最大步(即跳到离终点0~mx-1),剩下来的距离可以构成三角形需要两步

但是发现倒数第二根和最后一根也可以构成三角形 。所以构成三角形这一步只需要1步即可

假设最后一段距离为x 0<=x<=mx-1 。再加上倒数第二根距离 mx+x mx<=x+mx<=mx+mx-1 。两边之和为2mx,那么一定大于 mx+x。所以一定构成三角形。

#include<bits/stdc++.h>
using namespace std;
int t,mx,n,d;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>d;
		mx=0;
		int f=0;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			if(x==d) f=1;
			mx=max(mx,x);
		}
		if(f==1) 
		{
			cout<<1<<endl;
			continue;
		}
		if(mx>d)
		{
			cout<<2<<endl;
			continue;
		}
		if(d%mx==0) cout<<d/mx<<endl;
		else cout<<d/mx+1<<endl;
	}
 } 
发布了491 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104646791