Codeforces Round #621 (Div. 1 + Div. 2)B. Cow and Friend

解决兔子从(0,0)到(x,0)跳跃的最短路径问题,通过分析兔子喜爱的跳跃距离,确定最少跳跃次数。

B. Cow and Friend
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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).

题意:给了n个兔子喜欢的数字,每次只能跳他喜欢的数字,他想从(0,0) 到(x,0) 最少需要多少步

思路:首先,如果这个x大于了喜欢的数字的最大值,那么答案就是2,因为 两个这个数字和x 满足两边之和大于第三边 显然可以组成一个三角形。
等等? 真的是2吗?不一定
因为最大数字大于了x **那么是不是存在一个喜欢的数字a_i恰好等于x呢 那么这样答案就是1了 **
那么对于最大值ma小于x的,我们就是要一直沿着x轴走 那么答案就是ceil(x/ma)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> a;
int main()
{
   int t;cin>>t;
   while(t--){
     int n,x;cin>>n>>x;
     a.resize(n);
     int flag=0;
     for(int i=0;i<n;i++) {
        cin>>a[i];
        if(a[i]==x) flag=1;
     }
     sort(a.begin(),a.end());
     int ans=ceil(1.0*x/a[n-1]);
     if(flag) puts("1");
     else if(a[n-1]>x) puts("2");
     else cout<<ans<<endl;
   }

   return 0;
}

Codeforces Round 1036 是一场同时面向 Div.1Div.2 参赛者的比赛,通常这类比赛会包含多个具有挑战性的编程题目,涵盖算法、数据结构、数学等多个领域。比赛的题解和题目信息可以帮助参赛者回顾解题思路,提升编程能力。 ### 比赛基本信息 - **比赛名称**:Codeforces Round #1036 (Div. 1 and Div. 2) - **比赛时间**:具体时间为 UTC+X(根据实际举办日期和时间表) - **比赛链接**:[Codeforces 官方页面](https://codeforces.com/contest/1343) - **题解发布位置**:通常在比赛结束后不久,官方或社区成员会在 Codeforces 博客、GitHub 或其他技术平台上发布题解。 ### 题目类型与难度分布 该轮比赛通常包括 5 到 7 道题目,难度从简单实现到复杂算法不等。例如: - **A题**:通常是简单的模拟或数学问题。 - **B题**:可能涉及字符串处理或基础贪心策略。 - **C题**:中等难度,可能需要掌握基本的数据结构如数组、排序等。 - **D题及以后**:较高难度,可能涉及图论、动态规划、数论等高级算法。 ### 参赛情况与亮点 - **参与人数**:通常超过 10,000 名选手参加。 - **热门话题**:比赛中某些题目可能会引发广泛讨论,尤其是那些需要用到巧妙构造或优化技巧的问题。 - **知名选手表现**:顶尖选手如 tourist、Um_nik 等通常会以极快的速度完成所有题目,并占据排行榜前列。 ### 示例代码片段 以下是一个典型的 Codeforces 题目解法示例,适用于某道中等难度题目: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long l, r; cin >> l >> r; // 假设 e 是一个预处理好的符合条件的数组 // 使用二分查找来统计区间 [l, r] 内的有效数字个数 long long ans = upper_bound(e.begin(), e.end(), r) - lower_bound(e.begin(), e.end(), l); cout << ans << endl; } return 0; } ``` ### 题解资源推荐 - **Codeforces 官方博客**:通常会有详细的题解和作者说明。 - **GitHub 仓库**:许多参赛者会将自己的解法上传至 GitHub,便于他人学习。 - **知乎专栏 / 优快云 / 博客园**:中文社区中也常有高质量的赛后总结与分析文章。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值