「AHOI2014/JSOI2014」宅男计划

「AHOI2014/JSOI2014」宅男计划

传送门

我们首先要发现一个性质:存货天数随买食物的次数的变化类似于单峰函数。

具体证明不会啊,好像是二分加三分来证明?但是没有找到明确的严格证明。

感性理解一下就是:买的食物太少,很容易饿死;买太多就没钱了,也活不长。

所以我们考虑如何对于当前三分的答案如何 \(\text{check}\)

有一个显而易见的性质就是我们不会用价格更高,质量更劣的食品。

也就是说我们希望价格高的食品质量也一定要更好。

所以我们可以把所有食物按照价格或者质量排序,然后开个单调栈扫一遍即可。

然后贪心策略就是说我们肯定是在一段时间内坚持吃一种食物直到过期或者没钱,这个就很好算了。

参考代码:

#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

typedef long long LL;
const int _ = 202;

LL M, F; int n, top, stk[_]; struct node { LL p, s; } t[_];
inline bool cmp(const node& x, const node& y) { return x.p > y.p; }

inline LL calc(LL x) {
    LL m = M - x * F, res = 0, sum = 0;
    for (rg int i = n; i >= 1; --i) {
        LL tmp = min(m / (t[i].p * x), t[i].s - sum);
        sum += tmp, res += tmp * x, m -= tmp * x * t[i].p;
        if (sum < t[i].s) { res += m / t[i].p; break; }
    }
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
    file("cpp");
#endif
    read(M), read(F), read(n);
    for (rg int i = 1; i <= n; ++i) read(t[i].p), read(t[i].s), ++t[i].s;
    sort(t + 1, t + n + 1, cmp);
    top = 0;
    for (rg int i = 1; i <= n; ++i) { while (top && t[i].s >= t[stk[top]].s) --top; stk[++top] = i; }
    n = top; for (rg int i = 1; i <= n; ++i) t[i] = t[stk[i]];
    LL l = 1, r = M / F;
    while (l < r) {
        LL lmid = (2 * l + r) / 3;
        LL rmid = (2 * r + l + 2) / 3;
        if (calc(lmid) < calc(rmid)) l = lmid + 1; else r = rmid - 1;
    }
    printf("%lld\n", calc(l));
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zsbzsb/p/12264334.html
今日推荐