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代码:
直接枚举判断是否符合
#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【枚举二分答案】的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解
A..B略 C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可. #include<iostream> #incl ...
- 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. 题解:很显然只有 \( ...
- 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) 思路:我们想一下如果题目说的是最 ...
- 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...
随机推荐
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...
- Robot 安装
安装一个测试环境的 下载python27 升级pip E:\Python27\python -m pip install --upgrade pip 安装robotramework E:\Python ...
- golang之map的使用声明
1.map的基本介绍 map是key-value数据结构,又称为字段或者关联数组.类似其它编程语言的集合,在编程中是经常使用到的 2.map的声明 1)基本语法 var map 变量名 map[key ...
- QLineEdit的信号函数
QLineEdit一共有6个信号函数,并不多,很好理解. ·void cursorPositionChanged( intold, intnew ) 当鼠标移动时发出此信号,old为先前的位置,new ...
- 写文章 通俗易懂 悲观锁、乐观锁、可重入锁、自旋锁、偏向锁、轻量/重量级锁、读写锁、各种锁及其Java实现!
网上关于Java中锁的话题可以说资料相当丰富,但相关内容总感觉是一大串术语的罗列,让人云里雾里,读完就忘.本文希望能为Java新人做一篇通俗易懂的整合,旨在消除对各种各样锁的术语的恐惧感,对每种锁的底 ...
- codefroce 854 A.Fraction
题解:贪心,每次从能够出发的飞机中取一个最大的就好啦,用一个队列维护一下~ ac代码: #include <cstdio> #include <iostream> #inclu ...
- (十)springmvc之文件的处理
一.同步上传文件 导入common-fileupload这个jar包. 配置 springmvc-servlet.xml <?xml version="1.0" en ...
- linux gcc安装
2004年4月20日最新版本的GCC编译器3.4.0发布了.目前,GCC可以用来编译C/C++.FORTRAN.java.OBJC.ADA等语言的程序,可根据需要选择安装支持的语言.GCC 3.4.0 ...
- elment 中tree组件展开所有和收缩所有
upAll () { // 全部展开 遍历变成true let self = this; // 将没有转换成树的原数据 let treeList = this.sourceData; for (let ...
- 使用CDS view开发SAP Marketing contact的facet追溯工具
这篇SAP社区博客里,我的一位同事介绍了SAP Marketing里contact facet数据模型的存储表: https://blogs.sap.com/2016/07/01/how-does-s ...