⭐ 冶炼金属
输入样例
3
75 3
53 2
59 2
输出样例
20 25
⭐ 二分解法
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), a, b;
int min = 1;
int max = Integer.MAX_VALUE;
while (n-- > 0)
{
a = sc.nextInt();
b = sc.nextInt();
min = Math.max(min, get(a, b));
max = Math.min(max, get(a, b - 1) - 1);
}
System.out.println(min + " " + max);
}
private static int get(int a, int b)
{
int l = 1;
int r = (int) 1e9 + 1;
while (l < r)
{
int mid = l + r >> 1;
if (a / mid <= b)
r = mid;
else
{
l = mid + 1;
}
}
return l;
}
}
⭐ 推公式
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), a, b;
int min = 1;
int max = Integer.MAX_VALUE;
while (n-- > 0)
{
a = sc.nextInt();
b = sc.nextInt();
min = Math.max(min, a / (b + 1) + 1);
max = Math.min(max, a / b);
}
System.out.println(min + " " + max);
}
}