Codeforces 1240A. Save the Nature
显然可以二分答案
如果知道卖的票数,那么就能算出有多少 $a$ 倍数但不是 $b$ 倍数的位置,多少 $b$ 倍数但不是 $a$ 倍数的位置,多少既是 $a$ 又是 $b$ 倍数的位置
然后贪心地把每张票分配给那些位置即可
把价格从大到小排序并预处理前缀和就可以 $O(1)$ 求出最大收益了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
inline ll read()
{
ll x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e5+;
ll Q,n,p[N],X,Y,A,B,K;
ll sum[N];
ll gcd(ll x,ll y) { return y ? gcd(y,x%y) : x; }
bool check(ll p)
{
ll ab=p/(1ll*A/gcd(A,B)*B),a=p/A-ab,b=p/B-ab;
return sum[ab]/*(X+Y)+(sum[ab+a]-sum[ab])/*X+(sum[ab+a+b]-sum[ab+a])/*Y>=K;
}
int main()
{
Q=read();
while(Q--)
{
n=read();
for(int i=;i<=n;i++) p[i]=read();
sort(p+,p+n+); reverse(p+,p+n+);
for(int i=;i<=n;i++) sum[i]=sum[i-]+p[i];
X=read(),A=read();
Y=read(),B=read();
if(X<Y) swap(X,Y),swap(A,B);
K=read();
ll L=,R=n,Ans=N;
while(L<=R)
{
int mid=L+R>>;
if(check(mid)) Ans=mid,R=mid-;
else L=mid+;
}
if(Ans==N) printf("-1\n");
else printf("%lld\n",Ans);
}
return ;
}
Codeforces 1240A. Save the Nature的更多相关文章
- 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 rea ...
- 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 867B Save the problem
B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...
- codeforces 624A Save Luke(水题)
A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- CodeForces 937C Save Energy! 水题
题意: 一个炉子烤鸡,炉子打开的时候一共$T$分钟可以烤完,关闭的时候一共$2T$分钟可以烤完,炉子每$K$分钟自动关闭,厨师每$D$分钟回来检查,打开炉子 问多长时间烤完.. 题解: 用整数写比较稳 ...
- Codeforces Round #591 (Div. 2)
A. CME 题目链接:https://codeforces.com/contest/1241/problem/A 题意: 你有 N 根火柴 , 多少根火柴就可以组成多大的数(如 三根火柴可以表示 3 ...
- Codeforces Round #591
目录 Contest Info Solutions A. Save the Nature B. Sequence Sorting C. Paint the Tree D. Stack Extermin ...
- 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 ...
- Technocup 2020 - Elimination Round 1补题
慢慢来. 题目册 题目 A B C D tag math strings greedy dp 状态 √ √ √ √ //∅,√,× 想法 A. CME res tp A 题意:有\(n\)根火柴,额外 ...
随机推荐
- 预处理、const、static与sizeof-用#define实现宏并求最大值和最小值
1:实现代码: #define MAX(x,y) (((x)>(y)) ? (x):(y)) #define MIN(x,y) (((x)>(y)) ? (x):(y)) 需要注意的几点: ...
- 2 ArrayList 详解
List 是有序.可重复的容器.List中每个元素都有索引标记,可以根据元素的索引标记访问元素,从而精确控制这些元素. List 接口常用的实现类:ArrayList.LinkedList.Vecto ...
- 使用Git上传文件至Github
记录一下怎么把文件上传到Github,因为之前都存在本地,没上传过Github,自己以后看起来也有个记忆.因为我自己已经安装好Git和注册好Github账号了,设置好了SSH key.这部分不懂的,就 ...
- 数据库 | SQL 诊断优化套路包,套路用的对,速度升百倍
本文出自头条号老王谈运维,转载请说明出处. 前言 在DBA的日常工作中,调整个别性能较差的SQL语句是一项富有挑战性的工作.面对慢SQL,一些DBA会心烦,会沮丧,会束手无措,也会沉着冷静.斗智斗勇! ...
- sed与awk
sed 格式 sed 选项 控制命令 文件或标准输入 sed 流程: (循环打印) sed是将文件里的每一行读入模式空间进行操作, sed选项 -r 支持正则表达 -n 取消默认打印 清空当前模式空间 ...
- 映射器Mapping
1) org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping(核心) 将程序员定义的Action所对应的<bean& ...
- Myeclipse 启动tomcat项目报Out of memory: java heap space
问题: 在Myeclipse中启动tomcat,程序启动过程中报内存不足,java.lang.OutOfMemoryError: Java heap space 从错误可以看出是堆内存太小,需要配置j ...
- postgres serial创建自增列
Sequence是数据库中一类特殊的对象,其用于生成唯一数字标识符.一个典型的应用场景就是手动生成一系列主键.Sequence和MySQL中的AUTO_INCREMENT的概念很像. 创建序列Sequ ...
- Java Unsigned Bytes
Having had to use unsigned bytes for the first time, I also had to learn how Java references these d ...
- delphi数据集查找不定位
procedure TForm1.Button2Click(Sender: TObject); var R: Variant; begin R := MemTableEh1.Lookup('Name' ...