题解:AT_abc476_d [ABC476D] Automat

· · 题解

AT_abc476_d [ABC476D] Automat 题解

思路

一道贪心题。

首先要把两种商品排序,然后从价格低的开始买,才能买到尽可能多的商品。

然后枚举第一种商品选多少个就行了。

为什么是选第一种商品枚举呢?因为第一种商品两种钱都能用,比较方便。同时,可以用前缀和还有二分优化。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e6 + 5;
ll a[N], b[N], sub[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n, m, k;
    cin >> n >> m >> k;
    ll x, y;
    cin >> x >> y;
    for(ll i = 1; i <= n; i++) cin >> a[i];
    for(ll i = 1; i <= m; i++) cin >> b[i];
    sort(a + 1, a + n + 1);
    sort(b + 1, b + m + 1);
    for(ll i = 0; i <= n; i++) sub[i] = sub[i - 1] + a[i];
    ll ans = upper_bound(sub + 1, sub + n + 1, y * k + x) - sub - 1;
    for(ll i = 1; i <= m; i++) {
        ll tmp = (b[i] + k - 1) / k;
        if(y < tmp) break;
        y -= tmp;
        x += tmp * k - b[i];
        ll temp = upper_bound(sub + 1, sub + n + 1, y * k + x) - sub - 1;
        ans = max(ans, temp + i);
    }
    cout << ans << endl;
    return 0;
}

后记

求管理员通过,管理员辛苦了。

如果这篇题解对你有帮助,不妨点个赞再走吧。