B. Long Legs Educational Codeforces Round 146 (Rated for Div. 2)

Problem - 1814B - Codeforces

The general idea of ​​the question: The chess piece is actually at point (0,0) and needs to move to point (a,b). The initial step length m is 1. There are 3 operations in each round. Operation 1 means the abscissa +m, operation 2 means the ordinate +m, operation 3 lets m+1, what is the minimum number of rounds required?

1<=a,b<=1e9

Idea: If this problem is 1-dimensional, that is, from 0 to a, then we can easily think that the optimal strategy should be to increase the step size to a number x first, and then keep walking x to the end. If a cannot divide x, just take an extra step of a%x, so the minimum operand is (a-1)/x+1+x-1.

The same is true for two dimensions. Find x1, x2 for a and b respectively according to the above method. First, let the step length increase to min(x1,x2), complete one dimension, and then increase it to max(x1,x2) to complete the other dimension. Just dimension.

Then when considering what value x takes, (a-1)/x+1+(b-1)/x+1+x-1, that is, (a+b-2)/x+x+1 is the smallest, we find The first part of this formula, y1=(a+b-2)/x, is monotonically decreasing, and y2=x+1 is monotonically increasing. Then we only need to find where the changes in the two functions are the same, that is, to solve for y1 =y2, the item-shifting reduction results in x being approximately equal to sqrt(a+b). According to the range of a+b and the number of test data groups, we can definitely find the global minimum by enumerating x from 1 to 1e5.

//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
void solve()
{
	cin >> n;
	int m;
	cin >> m;
	ll ans = 0x7fffffff;
	for (int i = 1; i <= 100000; i++)
	{
		ll temp = (n - 1) / i + 1 + (m - 1) / i + 1 + i - 1;
		ans = min(ans, temp);
	}
	cout << ans;
	cout << '\n';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
}

Guess you like

Origin blog.csdn.net/ashbringer233/article/details/134167368