传送门

显然可以二分答案

如果知道卖的票数,那么就能算出有多少 $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的更多相关文章

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

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

  3. CodeForces 867B Save the problem

    B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...

  4. codeforces 624A Save Luke(水题)

    A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. CodeForces 937C Save Energy! 水题

    题意: 一个炉子烤鸡,炉子打开的时候一共$T$分钟可以烤完,关闭的时候一共$2T$分钟可以烤完,炉子每$K$分钟自动关闭,厨师每$D$分钟回来检查,打开炉子 问多长时间烤完.. 题解: 用整数写比较稳 ...

  6. Codeforces Round #591 (Div. 2)

    A. CME 题目链接:https://codeforces.com/contest/1241/problem/A 题意: 你有 N 根火柴 , 多少根火柴就可以组成多大的数(如 三根火柴可以表示 3 ...

  7. Codeforces Round #591

    目录 Contest Info Solutions A. Save the Nature B. Sequence Sorting C. Paint the Tree D. Stack Extermin ...

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

  9. Technocup 2020 - Elimination Round 1补题

    慢慢来. 题目册 题目 A B C D tag math strings greedy dp 状态 √ √ √ √ //∅,√,× 想法 A. CME res tp A 题意:有\(n\)根火柴,额外 ...

随机推荐

  1. Map集合循环遍历的几种方式

    package cn.jdbc.test;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import ...

  2. scarpy crawl 爬取微信小程序文章(将数据通过异步的方式保存的数据库中)

    import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider ...

  3. Leetcode题目102.二叉树的层次遍历(队列-中等)

    题目描述: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...

  4. postgresql 字符串转整数 int、integer

    --把'1234'转成整数 select cast('1234' as integer ) ; --用substring截取字符串,从第8个字符开始截取2个字符:结果是12 select cast(s ...

  5. Kbengine游戏引擎-【4】demo-kbengine_unity3d_demo 在容器docker上安装测试

    git地址:https://github.com/kbengine/kbengine_unity3d_demo Demo中文地址:https://github.com/kbengine/kbengin ...

  6. 把文档中的数据取出并插入到excel中

    from xlrd import open_workbookfrom xlutils.copy import copy jsonfile=r'C:\Users\Administrator\Deskto ...

  7. 搞清clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop

    每个HTML元素都具有clientHeight offsetHeight scrollHeight offsetTop scrollTop 这5个和元素高度.滚动.位置相关的属性,单凭单词很难搞清楚分 ...

  8. Vim的强大配置文件

    我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2.自动插入文件头 ,新建C.C++源文件时自动插入表 ...

  9. JavaScript抽象语法树英文对照

    type:"Program" //顶级对象类型 type:"Identifier" // 标识符 type:"FuncationDeclaration ...

  10. ElasticSearch、Logstash管理和监控——blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]

    1.某一天出现Kafka堆积大量未消费的记录: 2.该主题是用logstash进行消费的,然后查询logstash的日志(logstash/logs/logstash-plain.log),出现以下提 ...