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 ...
随机推荐
- 详解Cookie、Session和缓存
1 Cookie和Session Cookie和Session都为了用来保存状态信息,都是保存客户端状态的机制,它们都是为了解决HTTP无状态的问题而所做的努力. Session可以用Cookie来实 ...
- 数值类型与std::string的相互转换
1.使用std::stringstream: //将in_value值转换成out_type类型 template<class out_type, class in_value> out_ ...
- re 模块与正则表达式
目录 re 模块 re 模块的基本使用 re 模块 正则表达式与re模块的关系 1:正则表达式是一门独立的技术. 2:正则在任何语言中均可以使用. 3:python中要想使用正则表达式需要通过re模块 ...
- c语言 c++程序运行过程
我们写好的 .c .cpp 文件在计算机中如何运行. 一个.c 文件 .cpp 文件 首先经过 预编译形成 . i 文件 在这个过程中 主要处理程序中的# 以及进行宏替换 然后编译 ...
- SysTick系统定时器(功能框图和优先级配置)
SysTick—系统定时器是属于 CM3 内核中的一个外设,内嵌在 NVIC 中.系统定时器是一个 24bit (2^24)的向下递减的计数器,计数器每计数一次的时间为 1/SYSCLK,一般我们设置 ...
- CAS 5.x搭建常见问题系列(2).PKIX path building failed
错误原因 服务端的证书是不安全的,Cas的客户端在调用时因为安全提醒造成调用失败. CAS的客户端需要导入服务端的证书后,就正常了. 具体操作步骤如下: 1. 首先启动tomcat,看下之前搭建的ca ...
- C#获取Excel表格所有sheet名(Epplus)
原文:C#获取Excel表格所有sheet名(Epplus) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog. ...
- 同一个url对应多个视图函数,取第一个视图函数有效
# -*- coding: utf-8 -*- from flask import Flask app = Flask(__name__) @app.route('/') def index(): r ...
- WeixinJSBridge目前还能够直接使用的功能(2019)
参考地址:http://mmlike.sinaapp.com/ 其余功能不是不能用了,而是有各种条件了. 以下列出的功能,均可直接使用,无需appid等任何东东,只要在手机微信网页环境中 发送邮件 W ...
- 深度学习_1_Tensorflow_2_数据_文件读取
tensorflow 数据读取 队列和线程 文件读取, 图片处理 问题:大文件读取,读取速度, 在tensorflow中真正的多线程 子线程读取数据 向队列放数据(如每次100个),主线程学习,不用全 ...