Save the Nature CodeForces - 1241C (二分)

Save the Nature

题意:

在第n * a张卖掉的电影票能够提供x%的收益

在第n * b张卖掉的电影票能够提供y%的收益

问收益为k时,最少需要卖多少张电影票

思路:

二分一下答案就好了

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<vector>
 7 #include<queue>
 8 #define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
 9 #define mem(a,x) memset(a,x,sizeof(a))
10 #define lson rt<<1,l,mid
11 #define rson rt<<1|1,mid + 1,r
12 #define P pair<ll,ll>
13 #define ull unsigned long long
14 using namespace std;
15 typedef long long ll;
16 const int maxn = 2e5 + 10;
17 const int inf = 0x3f3f3f3f;
18 ll q, n, k;
19 ll val[maxn];
20 ll tmp[maxn];
21 ll x, y, a, b;
22 priority_queue<P, vector<P>, less<P>>que, tque;
23 int gcd(int a,int b){while (b ^= a ^= b ^= a %= b); return a;}
24 int lcm(int a, int b) { return a * b / gcd(a, b); }
25 
26 bool check(int mid)
27 {
28     tque = que;
29     ll pos = n, sum = 0;
30     ll cnt = 0;
31     while (!tque.empty() && sum < k && pos > 0)
32     {
33         P cur = tque.top();
34         tque.pop();
35         if (cur.second > mid) continue;
36         sum += val[pos--] / 100 * cur.first;
37         ++cnt;
38         if (cnt >= mid) break;
39     }
40     return sum >= k;
41 }
42 
43 int main() 
44 {
45     scanf("%lld", &q);
46     while (q--)
47     {
48         while (!que.empty()) que.pop();
49         bool flag = false;
50         scanf("%lld", &n);
51         for (int i = 1; i <= n; ++i)
52         {
53             scanf("%lld", &val[i]);
54         }
55         sort(val + 1, val + 1 + n);
56         scanf("%lld %lld", &x, &a);
57         scanf("%lld %lld", &y, &b);
58         scanf("%lld", &k);
59         mem(tmp, 0);
60         for (int i = 1; i * a <= n; ++i)
61             tmp[i * a] += x;
62         for (int i = 1; i * b <= n; ++i)
63             tmp[i * b] += y;
64         for (int i = 1; i <= n; ++i)
65             if (tmp[i] > 0)
66                 que.push(P(tmp[i], i));
67         int l = 1, r = n + 1;
68         while (r > l)
69         {
70             int mid = (l + r) >> 1;
71             if (check(mid))
72             {
73                 flag = true;
74                 r = mid;
75             }
76             else
77             {
78                 l = mid + 1;
79             }
80         }
81         if (flag) printf("%d\n", r);
82         else printf("-1\n");
83     }
84 
85     return 0;
86 }
AC代码

 

posted @ 2020-04-15 20:10  当然是斗笠呀  阅读(179)  评论(0编辑  收藏  举报