分析:题目可以转化为对于一个数,对它进行x次减法操作,n-x次加法操作,使他变成最小的非负整数.因为每减一次数就会减小,次数是一定的,所以可以二分x,就可以了.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int inf = 0x7fffffff; int n, m, u[2010], v[2010]; ll ans = inf; void erfen(int cur) { int l = 0, r = 10000000,res = 0; while (l <= r) { int mid = (l + r) >> 1; if (1LL * u[cur] * (n - mid) - 1LL * v[cur] * mid >= 0) { res = mid; l = mid + 1; } else r = mid - 1; } ans = min(ans, 1LL * u[cur] * (n - res) - 1LL * v[cur] * res); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d%d", &u[i], &v[i]); for (int i = 1; i <= m; i++) erfen(i); printf("%lld\n", ans); return 0; }