题解:CF1307B Cow and Friend

· · 题解

题目传送门

思路

考虑贪心。

先判断能不能一次整好跳过去,如果可以,输出 1。 如果不能一次整好跳到,就用最长的跳。

两次最长的可以跳到 0 到最长距离中间的任何一个距离。 跳的次数等于:(x + \max (a) - l) \div \max (a)

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
    int T;
    cin >> T;
    while(T --) {
        int n, x;
        cin >> n >> x;
        int l = 0;
        int res = 0;
        for(int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            if(x == temp) res = 1;
            l = max(l, temp);
        }
        if(res == 1) {
            cout << res << endl;
            continue;
        }
        res = 2;
        res = max(res, (x + l - 1) / l);
        cout << res << endl;
    }
}