cf--B. Cow and Friend+推理

Description

在这里插入图片描述

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.

题意:

已知n个可走距离,和x,问牛从(0,0)到(x,0)最少需要走多少步?牛可以这样直走,可以斜着走,每个可走距离可以用无限次。

思路:

设某个可走距离为k,走的步数一共就三个情况。
1.一次到达,满足k==x;
2.两次到达,满足k>x;(由勾股定理得,两个最小边之和大于第三边,则两次肯定是能到达的)。
3.多次到达,k<x;(我们只需用最大可走距离 ceil(x/mx) )
举个例子来说:
3 12
3 4 5
这就是多次到达的问题,ceil(12/5)=3,也就是牛走了三次5.可以这样来分析,先让牛走直线,还剩7的距离,两个5走一个三角形的两边。

AC代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,k,x,mx=0;
		bool flag=false;
		scanf("%d%d",&n,&x);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&k);
			if(k==x)	flag=true;
			mx=max(mx,k);
		}	
		if(flag)	puts("1");
		else
			printf("%d\n",max(2,(mx+x-1)/mx));//向下取整 
	}
	return 0;
}
发布了39 篇原创文章 · 获赞 1 · 访问量 567

猜你喜欢

转载自blog.csdn.net/qq_45249273/article/details/104419234