题意:给n张票的价格pi,每a张票价格的x%用作环保项目1,每b张票价格的y%用作环保项目2,为了使投入环保项目的总额至少为k,求出重新排列票的价格后需要卖出的最少的票数
思路:二分票数,用大顶堆来存储票的价格,显然a和b的公倍数位置上应该放最大的价格,其次比较x和y,较大的那个对应的位置上放较大价格,最后把剩余的没放的位置再放剩下的价格,每次从堆中取价格,判断当前票数是否满足价格至少为k,满足则减小右边界,存储答案,否则增加左边界,最后答案即为最小票数。
坑点有long long问题,还有double精度问题,之前把x和y转化为double类型的百分比,但这样使得答案和预想的不一样,实际上题目指定票的价格为100的倍数,故直接用票的价格除以100乘上百分率。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5+10;
const int inf = 0x3f3f3f3f;
ll q, n, x, y, a, b, k;
priority_queue<ll> que;
ll lcm(ll x1, ll y1)
{
return x1*y1/__gcd(x1, y1);
}
bool check(ll z)
{
ll cur = 0;
priority_queue<ll> que2 = que;
ll c = lcm(a, b);
if (z >= c) {
for (ll i = c; i <= z; i += c) {
ll t = que2.top(); que2.pop();
cur += t*(x+y)/100;
}
}
if (z >= a) {
for (ll i = a; i <= z; i += a) {
if (i%c != 0) {
ll t = que2.top(); que2.pop();
cur += t*x/100;
}
}
}
for (ll i = b; i <= z; i += b) {
if (i%c != 0) {
ll t = que2.top(); que2.pop();
cur += t*y/100;
}
}
return cur >= k;
}
int main()
{
cin >> q;
while (q--) {
cin >> n;
while (!que.empty())
que.pop();
for (int i = 0; i < n; i++) {
ll t;
scanf("%lld", &t);
que.push(t);
}
scanf("%lld%lld", &x, &a);
scanf("%lld%lld", &y, &b);
scanf("%lld", &k);
if (x < y) {
swap(x, y);
swap(a, b);
}
ll l = 0, r = 2*n, ans = -1;
while (l <= r) {
ll mid = (l+r)>>1;
if (check(mid)) {
r = mid - 1;
ans = mid;
}
else
l = mid + 1;
}
if (ans > n)
ans = -1;
cout << ans << "\n";
}
return 0;
}