链接:
https://codeforces.com/contest/1241/problem/C
题意:
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is pi. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
The x% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
The y% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x+y)% are used for environmental activities. Also, it‘s known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you‘d like to sell tickets with prices [400,100,300,200] and the cinema pays 10% of each 2-nd sold ticket and 20% of each 3-rd sold ticket, then arranging them in order [100,200,300,400] will lead to contribution equal to 100?0+200?0.1+300?0.2+400?0.1=120. But arranging them in order [100,300,400,200] will lead to 100?0+300?0.1+400?0.2+200?0.1=130.
Nature can‘t wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it‘s impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
思路:
刚开始以为可以一遍解决.看了题解发现是二分.
直接对排序后的数组前缀和, 然后记录a, b, lcm(a, b)的个数.
贪心加进去
代码:
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAXN = 2e5+10; int Val[MAXN]; LL Sum[MAXN]; LL n, x, y, a, b, lcm; LL k; bool Check(int mid) { int cnta = mid/a; int cntb = mid/b; int cnts = mid/lcm; cnta -= cnts, cntb -= cnts; LL sum = 0; sum += (Sum[cnts]/100)*(x+y); sum += ((Sum[cnts+cnta]-Sum[cnts])/100)*x; sum += ((Sum[cnts+cnta+cntb]-Sum[cnts+cnta])/100)*y; if (sum >= k) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { cin >> n; for (int i = 1;i <= n;i++) cin >> Val[i]; cin >> x >> a; cin >> y >> b; cin >> k; if (x < y) swap(x, y), swap(a, b); sort(Val+1, Val+1+n, greater<int>()); Sum[0] = 0; for (int i = 1;i <= n;i++) Sum[i] = Sum[i-1]+Val[i]; lcm = (a*b)/__gcd(a, b); int l = 1, r = n, res = n+1; while (l <= r) { int mid = (l+r)/2; if (Check(mid)) { res = min(res, mid); r = mid-1; } else l = mid+1; } if (res == n+1) puts("-1"); else printf("%d\n", res); } return 0; }