暴力做法
#include<iostream>
#include<vector>
using namespace std;void solve()
{int n; cin >> n;vector<int>a(n), b(n);for (int i = 0; i < n; i++){cin >> a[i] >> b[i];}for (int i = 1; i < 1e6; i++)//从小到大,找最小值{bool flag = true;for (int j = 0; j < n; j++){if(b[j]!=a[j]/i) {flag = false;break;}}if (flag){cout << i << " ";break;}}for (int i = 1e6; i>0; i--)//从大到小,找最大值{bool flag = true;for (int j = 0; j < n; j++){if (b[j] != a[j] / i) {flag = false;break;}}if (flag){cout << i << " ";break;}}
}
int main()
{solve();return 0;
}
二分法
#include<iostream>
using namespace std;
const int N = 1e4 + 10;
int a[N], b[N];
int n;
bool check_min(int mid)
{for (int i = 0; i < n; i++){if (b[i] < a[i] / mid)return false;}return true;
}
bool check_max(int mid)
{for (int i = 0; i < n; i++){if (b[i] > a[i] / mid)return false;}return true;
}void solve()
{cin >> n;for (int i = 0; i < n; i++){cin >> a[i] >> b[i];}int lmin = 1, rmin = 1e9;while (lmin < rmin){int mid = lmin + rmin >> 1;//求最小值时不用+1if (check_min(mid)) rmin = mid;else lmin = mid+1;}int lmax = 1, rmax = 1e9;while (lmax < rmax){int mid = lmax + rmax +1>> 1;//求最大值时要+1if (check_max(mid)) lmax = mid;else rmax = mid - 1;}cout << lmin << " " << lmax << " ";
}int main()
{solve();return 0;
}