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 ...
随机推荐
- Vue路由系统
Vue路由系统 一切分离都是为了更好的结合,本文详细介绍了前后端分离架构之后,前端如何实现路由控制,即Vue路由系统. 一.VueRouter实现原理 VueRouter的实现原理是根据监控锚点值的改 ...
- Nginx关联php安装及启动
Nginx 1.10.2 php 5.6.30 [root@nginx local]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (C ...
- 将自定义dockerfile生成的image推送到仓库中
本文为以阿里云的案例方法,其他方法未尝试 1.注册阿里云账号 2.设置密码并通过docker登录 3.创建命名空间 4.创建镜像仓库 创建仓库时,需要选择代码源的仓库储存的方式,这里我用的是gitHu ...
- (二)手动配置第一个HelloWorld程序
上例的HelloWorld是由Android sutudio 自动生成的,现在我们手动来配置. 1. 重新创建工程 2. 创建空的Activity 生成的MainActivity.java 文件: p ...
- 移动端 H5 上拉刷新,下拉加载
http://www.mescroll.com/api.html#options 这里有几个重要的设置 1.down 下不启用下拉刷新是因为再手机端有默认的下拉刷新,会冲突,待解决 2.up 中的 a ...
- 微信小程序wx:key以及wx:key=" *this"详解:
今天写微信小程序无意中看到控制台给出了这样一行提示: 求解百度才知道,给大家分享一下: 1.wx:for定义 官方文档:在组件上使用 wx:for 控制属性绑定一个数组,即可使用数组中各项的数据重复渲 ...
- Docker多阶段构建实战(multi-stage builds)
在编写Dockerfile构建docker镜像时,常遇到以下问题: RUN命令会让镜像新增layer,导致镜像变大,虽然通过&&连接多个命令能缓解此问题,但如果命令之间用到docker ...
- ElementUI 复杂顶部和左侧导航栏实现
描述:如图 项目路径如下图所示: 代码实现: 首先在store.js中添加两个状态: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vue ...
- [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步
本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- 记录下js几种常见的数组排序和去重的方法
冒泡排序 , , , , , , , ]; function test(){ ; i < arr.length - ; i++){ ; j < arr.length; j++){ var ...