Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) C. Save the Nature【枚举二分答案】

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)的个数

AC代码:

直接枚举判断是否符合

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 #define  N 210000
 5 #define int long long
 6 int sum[N];
 7 int price[N];
 8 bool cmp(int a,int b){
 9     return a>b;
10 }
11 signed  main(){
12     int _;
13     cin>>_;
14     while(_--){
15         int n;
16         scanf("%d",&n);
17         memset(price,0,sizeof(price));
18         for(int i=1;i<=n;i++){
19             scanf("%d",&price[i]);
20             sum[i]=0;
21         }
22         sort(price+1,price+1+n,cmp);
23         for(int i=1;i<=n;i++){
24             sum[i]=sum[i-1]+price[i];
25         }
26         int x,a,y,b,K;
27         scanf("%lld%lld%lld%lld%lld",&x,&a,&y,&b,&K);
28         int flag=0;
29         int GCD=(a*b)/(__gcd(a,b));
30         for(int i=1;i<=n;i++){
31             int numcom=i/GCD;
32             int numa=i/a-numcom;
33             int numb=i/b-numcom;
34             int ans1=((x+y)*sum[numcom]/100)+(x*(sum[numcom+numa]-sum[numcom])/100)+(y*(sum[numcom+numa+numb]-(sum[numcom+numa]))/100);
35             int ans2=((x+y)*sum[numcom]/100)+(y*(sum[numcom+numb]-sum[numcom])/100)+(x*(sum[numcom+numa+numb]-(sum[numcom+numb]))/100);
36             int maxn=max(ans1,ans2);
37             if(maxn>=K){
38                 flag=1;
39                 printf("%d
",i);
40                 break;
41             }
42         }
43         if(flag){
44             continue;
45         }else{
46             printf("-1
");
47         }
48     } 
49     return 0;
50 }

二分答案:

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 #define int long long 
 5 #define N 250000
 6 int price[N];
 7 
 8 int sum[N];
 9 int x,a,y,b,K;
10 bool cmp(int aa,int bb){
11     return aa>bb;
12 }
13 int ok(int m){
14     int vis[m+10];
15     int GCD=(a*b)/(__gcd(a,b));
16     int numcom=m/GCD;
17     int numa=m/a-numcom;
18     int numb=m/b-numcom;
19     int ans1=((x+y)*sum[numcom]/100)+(x*(sum[numcom+numa]-sum[numcom])/100)+(y*(sum[numcom+numa+numb]-(sum[numcom+numa]))/100);
20     int ans2=((x+y)*sum[numcom]/100)+(y*(sum[numcom+numb]-sum[numcom])/100)+(x*(sum[numcom+numa+numb]-(sum[numcom+numb]))/100);
21     int maxn=max(ans1,ans2);
22     if(maxn>=K){
23         return 1;
24     }else{
25         return 0;
26     }
27 }
28 signed  main(){
29     int _;
30     cin>>_;
31     while(_--){
32         int n;
33         scanf("%lld",&n);
34         for(int i=0;i<=n;i++)
35             price[i]=0;
36         for(int i=1;i<=n;i++){
37             scanf("%lld",&price[i]);
38         }
39         sort(price+1,price+1+n,cmp);
40         for(int i=1;i<=n;i++)
41             sum[i]=sum[i-1]+price[i];
42         
43         scanf("%lld%lld%lld%lld%lld",&x,&a,&y,&b,&K);
44         int l=1;
45         int r=n;
46         int ans=n+1;
47         while(l<=r){
48             int mid=(l+r)/2;
49             if(ok(mid)){
50                 ans=min(ans,mid);
51                 r=mid-1;
52             }else{
53                 l=mid+1;
54             }
55         }
56         if(ans==n+1){
57             printf("-1
");
58         }else{
59             printf("%lld
",ans);
60         }
61     }
62     return 0;
63 }
原文地址:https://www.cnblogs.com/pengge666/p/11630270.html