Save the Nature CF-1241C(二分、贪心)

题意:

有$n$件商品,每天可以卖一件,每件商品的售价是$p[i]$元,在$a$的倍数天可以获得$x$%的利润,在$b$的倍数天可以获得$y$%的利润,利润可叠加。

如果需要获得$k$的利润,问最少需要卖多少天。

思路:

二分答案。对于售价高的商品分配给前$mid$天中利润高的时间。

代码:

 1 //#include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define pll pair<ll,ll>
16 #define pii pair<int,int>
17 #define bug printf("*********
")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 #define pb push_back
24 
25 using namespace std;
26 const int inf = 2e9 + 7;
27 const ll Inf = 1e18 + 7;
28 const int maxn = 2e5 + 5;
29 const int mod = 1e9 + 7;
30 
31 bool cmp(const ll& a, const ll& b)
32 {
33     return a > b;
34 }
35 
36 ll p[maxn], n, a[maxn], k, tmp[maxn];
37 
38 bool check(ll mid)
39 {
40     for (int i = 1; i <= mid; ++i) tmp[i] = a[i];
41     sort(tmp + 1, tmp + 1 + mid, cmp);
42     ll res = 0;
43     for (int i = 1; i <= mid; ++i)
44     {
45         res += p[i] / 100 * tmp[i];
46     }
47     return res >= k;
48 }
49 
50 int main()
51 {
52     int T;
53     scanf("%d", &T);
54     while (T--)
55     {
56         scanf("%lld", &n);
57         for (int i = 1; i <= n; ++i)
58         {
59             scanf("%lld", &p[i]);
60             a[i] = 0;
61         }
62         ll x1, y1, x2, y2;
63         scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2);
64         scanf("%lld", &k);
65         for (int i = 1; i <= n; ++i)
66         {
67             if (i % y1 == 0)    a[i] += x1;
68             if (i % y2 == 0)    a[i] += x2;
69         }
70         sort(p + 1, p + 1 + n, cmp);
71         ll ans = -1;
72         ll l = 1, r = n;
73         while (l <= r)
74         {
75             ll mid = l + r >> 1;
76             if (check(mid))
77                 r = mid - 1, ans = mid;
78             else
79                 l = mid + 1;
80         }
81         printf("%lld
", ans);
82     }
83 }
原文地址:https://www.cnblogs.com/zhang-Kelly/p/12701009.html