Luogu P3462 [POI2007]ODW-Weights
题目描述
While moving to a new compound the Byteotian Institute of Experimental Physics has encountered a logisticalproblem - the transfer of its vast collection of precision weights turned out to be non-trivial.
The Institute has a certain number of containers of limited strength at its disposal. As many weightsas possible are to be put into the containers, the remaining ones will be discarded. There is no limit onthe number of weights to be put in a container apart from the requirement of not exceeding its strength. Acontainer may also be empty.
Any two weights in the Institute have a peculiar property: the mass of one of them is an integer multipleof the mass of the other. Particularly, they may have the same mass.
TaskWrite a programme which:
reads the durabilities of the containers and masses of the weights from the standard input, determines the maximal number of weights that can be put in the containers, writes the outcome to the standard output.
在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作。公司有一些固定容量的容器可以装这些砝码。他们想装尽量多的砝码以便搬运,并且丢弃剩下的砝码。每个容器可以装的砝码数量有限制,但是他们能够装的总重量不能超过每个容器的限制。一个容器也可以不装任何东西。任何两个砝码都有一个特征,他们的中总有一个的重量是另外一个的整数倍,当然他们也可能相等。
输入输出格式
输入格式:
The first line of the standard input contains two integers nnn and mmm(1≤n,m≤100000)(1≤n,m≤100 000)(1≤n,m≤100000), separated by a singlespace, denoting respectively: the number of containers and the number of weights. The second line of thestandard input consists of nnn integers wiw_iwi (1≤wi≤1 000 000 0001≤wi≤1000000000,for1≤i≤n1≤i≤n)(1\le w_i\le 1\ 000\ 000\ 0001≤w_i ≤1 000 000 000 ,for 1\le i\le n1≤i≤n)(1≤wi≤1 000 000 0001≤wi≤1000000000,for1≤i≤n1≤i≤n), separated by single spaces,denoting the strengths of containers in milligrammes. In the third line there are mmm integers mj(1≤mj≤1 000 000 000 for1≤j≤m)m_j(1\le m_j\le 1\ 000\ 000\ 000\ for 1\le j\le m)mj(1≤mj≤1 000 000 000 for1≤j≤m), separated by single spaces, denoting masses of the weights in milligrammes.
输出格式:
The first and only line of the standard output should contain a single integer -the maximal number ofweights that can be placed in the containers without exceeding their durability.
输入输出样例
输入样例#1:
2 4
13 9
4 12 2 4
输出样例#1:
3
贪心
我们要充分利用题目中“任意两个砝码中有一个是另一个的整数倍”的性质。首先一个很显然的结论就是尽量取小的砝码。我们要考虑的是取小砝码的时候放在哪些容器中。根据题目的特殊性质,可以将砝码的重量看做一个进制(进制数不固定)。然后将容器进行进制拆分,每一位上的数全部累加起来。放砝码的时候就是高精度减法,直到不能放为止。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<ctime>
#include<queue>
#include<iomanip>
#define ll long long
#define N 100005 using namespace std;
inline int Get() {int 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;} int n,m,cc;
int c[N],w[N];
int d[N];
int ans;
int cnt[N];
int a[N];
struct node {
int v;
int key;
bool operator <(const node &a)const {
if(key!=a.key) return key<a.key;
return v<a.v;
}
};
priority_queue<node>q; bool reduce(int v) {
a[v]--;
while(a[v]<&&v<cc) {
a[v]+=d[v+]/d[v];
a[++v]--;
}
return (v<cc);
} int main() {
n=Get(),m=Get();
for(int i=;i<=n;i++) c[i]=Get();
for(int i=;i<=m;i++) w[i]=Get();
sort(c+,c++n);
sort(w+,w++m);
for(int i=;i<=n;i++) c[i]/=w[];
for(int i=m;i>=;i--) w[i]/=w[];
for(int i=;i<=m;i++) d[i]=w[i];
sort(d+,d++m);
cc=unique(d+,d++m)-d;
int now=;
d[cc]=d[cc-]*;
for(int i=;i<=n;i++) {
for(int j=;j<cc&&c[i];c[i]/=d[j+]/d[j],j++) {
if(j<cc-) a[j]+=c[i]%(d[j+]/d[j]);
else a[j]+=c[i];
}
}
for(int i=;i<=m;i++) {
if(w[i]!=w[i-]) now++;
if(reduce(now)) ans++;
else break;
}
cout<<ans<<"\n";
return ;
}
Luogu P3462 [POI2007]ODW-Weights的更多相关文章
- [Luogu P3455] [POI2007]ZAP-Queries (莫比乌斯反演 )
题面 传送门:洛咕 Solution 这题比这题不懂简单到哪里去了 好吧,我们来颓柿子. 为了防止重名,以下所有柿子中的\(x\)既是题目中的\(d\) 为了方便讨论,以下柿子均假设\(b>=a ...
- 洛谷 P3462 [POI2007]ODW-Weights
题面: https://www.luogu.org/problemnew/show/P3462 https://www.lydsy.com/JudgeOnline/problem.php?id=111 ...
- Luogu P3455 [POI2007]ZAP-Queries
由于之前做了Luogu P2257 YY的GCD,这里的做法就十分套路了. 建议先看上面一题的推导,这里的话就略去一些共性的地方了. 还是和之前一样设: \[f(d)=\sum_{i=1}^a \su ...
- Luogu P3459 [POI2007]MEG-Megalopolis(线段树)
P3459 [POI2007]MEG-Megalopolis 题意 题目描述 Byteotia has been eventually touched by globalisation, and so ...
- Luogu 3457 [POI2007]POW-The Flood
感觉自己什么题都写不动了. 又是一个神贪心:把所有城市中的点按照高度从小到大排序之后拿出来逐个计算,枚举其他高度小于它的点向四周扩展,如果这个点不能被之前放过的抽水机覆盖,那么把答案加一,并在这个点放 ...
- BZOJ 1107: [POI2007]驾驶考试egz / Luogu P3463 [POI2007]EGZ-Driving Exam (树状数组 LIS)
能从iii走到所有跑道 相当于 能从iii走到111和nnn. 边反向后就相当于 能从111和nnn走到iii. 为了方便叙述,把111~nnn叫做x坐标,111~(m+1)(m+1)(m+1)叫做y ...
- Luogu P2522 [HAOI2011]Problem b
如果你做过[Luogu P3455 POI2007]ZAP-Queries就很好办了,我们发现那一题求的是\(\sum_{i=1}^a\sum_{j=1}^b[\gcd(i,j)=d]\),就是这道题 ...
- NOIp2018停课刷题记录
Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...
- BZOJ 1110: [POI2007]砝码Odw
1110: [POI2007]砝码Odw Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 547 Solved: 296[Submit][Status ...
随机推荐
- 前端回顾:2016年 JavaScript 之星
JavasScript社区在创新的道路上开足了马力,曾经流行过的也许一个月之后就过时了.2016已经结束了.你可能会想你是否错过一些重要的东西?不用担心,让我们来回顾2016年前端有哪些主流.通过比较 ...
- php中的heredoc和nowdoc对比
两者的不同就相当于php双引号和单引号的不同,双引号会解析里边的变量,而单引号不会 heredoc语法 <?php $a = 'cqh'; $str = <<<HTML hel ...
- 深入学习sequoiadb巨杉数据库及python连接方式
随着公司日益复杂与多变的需求,以及迅速扩展带来的海量数据业务,我们需要在提供高效服务的同时,降低其设备与程序维护成本.算了,不吹了,说白了就是需要从巨杉数据库中抓取大量的数据,但是我现在不会,所以需要 ...
- 前后端分离demo 旅馆管理系统
模型设计 旅馆管理系统,主要涉及到登记入住,退房以及客房和客人信息管理:经过分析抽像出涉及到的实体以及各实体之间的关系: 可以看出整个业务以客房为中心,入住,退房,定价,收费都是以客房为基本单 ...
- IdentityServer4 中文文档 -10- (快速入门)使用密码保护API
IdentityServer4 中文文档 -10- (快速入门)使用密码保护API 原文:http://docs.identityserver.io/en/release/quickstarts/2_ ...
- [android] 手机卫士读取联系人
获取ContentResolver内容解析器对象,通过getContentResolver()方法 调用ContentResolver对象的query()方法,得到raw_contacts表里面的数据 ...
- 外机连接本机的虚拟机服务器_VM端口映射
说明:有时候我们把服务器放在虚拟机上的时候只能本机在网页上连接,但是如果想要别的电脑也能访问的话,需要在VM上做一个映射.实现如下: 设置VM端口映射 一.打开VM->编辑->虚拟网络编辑 ...
- js查询数组或者List类型是否包含某个元素
方法一:arr.indexOf(某元素) 实际用法:if(arr.indexOf(某元素) > -1){//则包含该元素} 例: var fruits = ["Banana" ...
- Frobenius norm(Frobenius 范数)
Frobenius 范数,简称F-范数,是一种矩阵范数,记为||·||F. 矩阵A的Frobenius范数定义为矩阵A各项元素的绝对值平方的总和,即 可用于 利用低秩矩阵来近似单一数据矩阵. 用数学表 ...
- javaSE总结
1 java的历史 1991-至今 詹姆斯-高斯林 SUN公司 ORACLE 2009年 2 java的版本 javaSE java的标准桌面级开发 javaEE 企业级web开发 javaM ...