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代码:

直接枚举判断是否符合

 #include<bits/stdc++.h>

 using namespace std;
#define N 210000
#define int long long
int sum[N];
int price[N];
bool cmp(int a,int b){
return a>b;
}
signed main(){
int _;
cin>>_;
while(_--){
int n;
scanf("%d",&n);
memset(price,,sizeof(price));
for(int i=;i<=n;i++){
scanf("%d",&price[i]);
sum[i]=;
}
sort(price+,price++n,cmp);
for(int i=;i<=n;i++){
sum[i]=sum[i-]+price[i];
}
int x,a,y,b,K;
scanf("%lld%lld%lld%lld%lld",&x,&a,&y,&b,&K);
int flag=;
int GCD=(a*b)/(__gcd(a,b));
for(int i=;i<=n;i++){
int numcom=i/GCD;
int numa=i/a-numcom;
int numb=i/b-numcom;
int ans1=((x+y)*sum[numcom]/)+(x*(sum[numcom+numa]-sum[numcom])/)+(y*(sum[numcom+numa+numb]-(sum[numcom+numa]))/);
int ans2=((x+y)*sum[numcom]/)+(y*(sum[numcom+numb]-sum[numcom])/)+(x*(sum[numcom+numa+numb]-(sum[numcom+numb]))/);
int maxn=max(ans1,ans2);
if(maxn>=K){
flag=;
printf("%d\n",i);
break;
}
}
if(flag){
continue;
}else{
printf("-1\n");
}
}
return ;
}

二分答案:

 #include<bits/stdc++.h>

 using namespace std;
#define int long long
#define N 250000
int price[N]; int sum[N];
int x,a,y,b,K;
bool cmp(int aa,int bb){
return aa>bb;
}
int ok(int m){
int vis[m+];
int GCD=(a*b)/(__gcd(a,b));
int numcom=m/GCD;
int numa=m/a-numcom;
int numb=m/b-numcom;
int ans1=((x+y)*sum[numcom]/)+(x*(sum[numcom+numa]-sum[numcom])/)+(y*(sum[numcom+numa+numb]-(sum[numcom+numa]))/);
int ans2=((x+y)*sum[numcom]/)+(y*(sum[numcom+numb]-sum[numcom])/)+(x*(sum[numcom+numa+numb]-(sum[numcom+numb]))/);
int maxn=max(ans1,ans2);
if(maxn>=K){
return ;
}else{
return ;
}
}
signed main(){
int _;
cin>>_;
while(_--){
int n;
scanf("%lld",&n);
for(int i=;i<=n;i++)
price[i]=;
for(int i=;i<=n;i++){
scanf("%lld",&price[i]);
}
sort(price+,price++n,cmp);
for(int i=;i<=n;i++)
sum[i]=sum[i-]+price[i]; scanf("%lld%lld%lld%lld%lld",&x,&a,&y,&b,&K);
int l=;
int r=n;
int ans=n+;
while(l<=r){
int mid=(l+r)/;
if(ok(mid)){
ans=min(ans,mid);
r=mid-;
}else{
l=mid+;
}
}
if(ans==n+){
printf("-1\n");
}else{
printf("%lld\n",ans);
}
}
return ;
}

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

  1. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) D. Sequence Sorting

    链接: https://codeforces.com/contest/1241/problem/D 题意: You are given a sequence a1,a2,-,an, consistin ...

  2. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) B. Strings Equalization

    链接: https://codeforces.com/contest/1241/problem/B 题意: You are given two strings of equal length s an ...

  3. 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 ...

  4. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) A. CME

    链接: https://codeforces.com/contest/1241/problem/A 题意: Let's denote correct match equation (we will d ...

  5. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1)

    Virtual participate 的,D题不会做,打了1:30就打不动了,过了ABCE. A - CME 题意:? 题解:? void test_case() { int n; scanf(&q ...

  6. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解

    A..B略 C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可. #include<iostream> #incl ...

  7. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2)

    A - Forgetting Things 题意:给 \(a,b\) 两个数字的开头数字(1~9),求使得等式 \(a=b-1\) 成立的一组 \(a,b\) ,无解输出-1. 题解:很显然只有 \( ...

  8. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

    A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...

  9. 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...

随机推荐

  1. 20191030-Python实现闭包

    打算在过年前每天总结一个知识点,所以把自己总结的知识点分享出来,中间参考了网络上很多大神的总结,但是发布时候因为时间太久可能没有找到原文链接,如果侵权请联系我删除 20191030:闭包 首先一个函数 ...

  2. 【BFS】Help the Princess!

    题目描述 The people of a certain kingdom make a revolution against the bad government of the princess. T ...

  3. 把axios挂载到vue实例上面/==Axios 各种请求方式传递参数格式

    /*ajax请求*/   import axios from 'axios'   axios.defaults.baseURL = 'https://api.douban.com/v2/movie' ...

  4. asp.net core-8. 配置的热更新

    在asp.net core 发布了以后,在修改配置文件以后不需要重新发布,要实现只需要修改@inject IOptions<WebApplication1.Class> ClassAcce ...

  5. shell习题第24题:杀进程

    [题目要求] 一台机器负载高,top查看到有很多sh的进程,然后top -c查看可以看到对应的进程命令是sh -c /bin/clear.sh 经分析后发现是因为该脚本执行时间太长,导致后续执行时,上 ...

  6. (六)springmvc之ModelAndView、Model、Map、ModelMap

    <a href="<%=request.getContextPath()%>/responseData/response_1">使用原生的作用域</a ...

  7. (十一)web服务与javaweb结合(2)

    一.解决问题及解决方法 解决问题:上章节用监听器的方式是有缺陷的:web服务的端口和web工程的端口不能一致. 解决方案:将webService绑定到web工程中,使得共用一个端口. 二.案例 2.1 ...

  8. linux 用户切换组

    问题: 因为默认的的网站路径 /var/www/html 是root 用户 root组的, 想要修改什么的需要用sudo 很麻烦. 解决: 将当前用户 hehecat加入至root组,使之有权限对目录 ...

  9. redis cluster异地数据迁移,扩容,缩容

    由于项目的服务器分布在重庆,上海,台北,休斯顿,所以需要做异地容灾需求.当前的mysql,redis cluster,elastic search都在重庆的如果重庆停电了,整个应用都不能用了. 现在考 ...

  10. 让一个父级div根据子级div高度而自适应高度

    需求是点击上传的时候进行子级div高度不定,相对来说父级div高度也不能固定,把元素都设置成普通标准流,然后样式可以使用margin内边距或者padding外边距来进行调节 放上代码供参考: .opu ...