[BZOJ3874/AHOI2014]宅男计划
Description
Input
Output
输出仅包含一行一个整数表示JYY可以宅的最多的天数。
Sample Input
5 0
10 2
Sample Output
HINT
【样例说明】
#include <cstdio>
#include <algorithm>
#define MAXN 205
using namespace std;
typedef long long ll;
struct Food { ll p, s; } tmp[MAXN], a[MAXN];
struct Cmp1
{
bool operator () (Food x, Food y)
{
return x.s == y.s ? x.p < y.p : x.s < y.s;
}
};
Cmp1 cmp1;
struct Cmp2
{
bool operator () (Food x, Food y)
{
return x.p < y.p;
}
};
Cmp2 cmp2;
ll t, m, f, n, tot;
ll getAns(ll o)
{
ll nowm = m - f * o, d = 0, res = 0, tx;
if (nowm < 0) return 0;
for (int i = 1; i <= tot; i++)
{
ll s = a[i].s, p = a[i].p;
if (d <= s) tx = min(s - d + 1, nowm / (p * o)), res += tx * o, nowm -= p * o * tx, d += tx;
if (d <= s) tx = min(o, nowm / p), res += tx, nowm -= p * tx, d++;
}
return res;
}
void init()
{
tot = 1;
scanf("%I64d %I64d %I64d", &m, &f, &n);
for (int i = 1; i <= n; i++) scanf("%I64d %I64d", &tmp[i].p, &tmp[i].s);
sort(tmp + 1, tmp + n + 1, cmp1), a[1] = tmp[1];
for (int i = 1; i <= n; i++) if (tmp[i].s > a[tot].s) a[++tot] = tmp[i];
sort(a + 1, a + tot + 1, cmp2);
}
int main()
{
freopen("food.in", "r", stdin);
freopen("food.out", "w", stdout);
scanf("%I64d", &t);
while (t--)
{
init();
if (f + a[1].p > m) { printf("0\n"); continue; }
ll l = 1, r = m / (f + a[1].p), ans = max(getAns(l), getAns(r));
while (l <= r)
{
ll tot = r - l + 1, ml = l + tot / 3, ansl = getAns(ml), mr = l + tot * 2 / 3, ansr = getAns(mr);
if (ansl < ansr) ans = max(ansl, ans), l = ml + 1;
else ans = max(ansr, ans), r = mr - 1;
}
for (int i = l; i <= r; i++) ans = max(ans, getAns(i));
printf("%I64d\n",ans);
}
return 0;
}