Educational Codeforces Round 146 (Rated for Div. 2)

112 阅读2分钟

A robot is placed in a cell (0,0) of an infinite grid. This robot has adjustable length legs. Initially, its legs have length 1.

Let the robot currently be in the cell (x,y) and have legs of length m. In one move, it can perform one of the following three actions:

  • jump into the cell (x+m,y);
  • jump into the cell (x,y+m);
  • increase the length of the legs by 1, i. e. set it to m+1.

What's the smallest number of moves robot has to make to reach a cell (a,b)?

题意:就是给你两个点,让你从(0,0)走到(a,b)最少几步

思路:其实我们发现单独对每一个坐标他的最优步数是sqrt(a),或者在这上面加一,那么我们要对这两个点都取最优不难发现他应该是在sqrt(a+b)这个范围,所以我们只要枚举一遍取最小的就可以了

代码:

// Problem: B. Long Legs

// Contest: Codeforces - Educational Codeforces Round 146 (Rated for Div. 2)

// URL: https://codeforces.com/problemset/problem/1814/B

// Memory Limit: 256 MB

// Time Limit: 2000 ms

//

// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define sc(x) scanf("%lld",&x)
#define pr(x) printf("%lld\n",x)
#define YES puts("YES");
#define NO puts("NO");
#define Yes puts("Yes");
#define No puts("No");
#define fo(i,j,k) for(int i = j;i <= k;i ++)
#define pre(i,j,k) for(int i = j;i >= k;i --)
#define ls idx*2
#define rs idx*2+1
int pow_mod(int a1, int n1, int m1)
{
    if(n1 == 0) return 1;
    int x = pow_mod(a1, n1/2, m1);
    long long ans = (long long)x * x % m1;
    if(n1 % 2 == 1) ans = ans *a1 % m1;
    return (int)ans;
}
inline __int128 read(){
    __int128 x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if(ch == '-')
        f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
    x = x * 10 + ch - '0';
    ch = getchar();
    }
    return x * f;
}
inline void print(__int128 x){
    if(x < 0){
    putchar('-');
    x = -x;
    }
    if(x > 9)
    print(x / 10);
    putchar(x % 10 + '0');
}
// bool operator<(NOW a, NOW b) {
// if(a.site == b.site)
// return a.x > b.x;
// return a.site< b.site;
// }
const int N = 1e6 + 10;
int n,m;
void solve()
{
    cin >> n >> m;
    int ans = 1e9;
    int num = 0;
    for(int i = 1;i <= 100000;i ++)
    {
        num += (n+i-1)/i;
        num += (m+i-1)/i;
        num += (i-1);
        //cout << num << '\n';
        ans = min(ans,num);
        num = 0;
    }
    cout << ans << '\n';
}
signed main()
{
    int tt = 1;
    sc(tt);
    while(tt--)
    {
    solve();
    }
}

然后我们可以发现最大的数是2e9,所以直接枚举到1e5就可以了