题目链接:点击打开链接

题意:

给定n个箱子m个物品

以下n个数字表示箱子的容量

以下m个数字b1-bm 表示物品体积为2^bi大

问最多有多少个物品能够放入箱子。

思路:

贪心,先放小的,小的不能放再放大的

显然我们把n个箱子拆成二进制,然后模拟二进制减法运算。

剩下就是简单模拟

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<queue>
#include<map>
#include<vector>
using namespace std; #define ll __int64 #define N 50
ll n,_gcd,m,k;
ll a[N],b[N],er[N];
bool jie(ll pos, ll ge){
bool hav = false;
ll now;
for(ll i = pos+1; i < N; i++)
if(a[i]){
hav = true;
now = i;
break;
}
if(hav==false)return false;
ll ned = ge / er[now-pos];
if(ned==0){
a[now]--;
a[pos]++;
for(ll i = pos; i < now; i++)
a[i]++;
return true;
}
ned = min(ned, a[now]);
a[now] -= ned;
a[pos] += ned*er[now-pos];
return true;
}
int main(){
ll i, u;
for(i=0, u = 1;i<N;i++, u<<=1)
er[i] = u;
while(cin>>n>>m) {
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
while(n--){
scanf("%I64d",&u);
for(i=0;i<N && u>=er[i];i++){
if(u&er[i]){
a[i]++;
}
}
}
ll ans = 0;
while(m--){
scanf("%I64d",&u);
b[u]++;
}
bool hav = false;
for(i=0;i<N;i++)if(b[i]){
while(b[i]){
ll now = min(a[i],b[i]);
ans += now;
a[i]-=now;
b[i]-=now;
if(b[i])
{
if(!jie(i, b[i]))
{
hav = false;
break;
}
}
else break;
}
if(hav)break;
}
cout<<ans<<endl;
}
return 0;
}

Codeforces 309C Memory for Arrays 二进制模拟进位的更多相关文章

  1. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  2. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. Educational Codeforces Round 11B. Seating On Bus 模拟

    地址:http://codeforces.com/contest/660/problem/B 题目: B. Seating On Bus time limit per test 1 second me ...

  4. CodeForces 388A Fox and Box Accumulation (模拟)

    A. Fox and Box Accumulation time limit per test:1 second memory limit per test:256 megabytes Fox Cie ...

  5. Codeforces Round #454 C. Shockers【模拟/hash】

    C. Shockers time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  6. Codeforces Round #528-A. Right-Left Cipher(字符串模拟)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. Codeforces Round #249 (Div. 2) (模拟)

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Codeforces 740C. Alyona and mex 思路模拟

    C. Alyona and mex time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  9. codeforces 723B Text Document Analysis(字符串模拟,)

    题目链接:http://codeforces.com/problemset/problem/723/B 题目大意: 输入n,给出n个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...

随机推荐

  1. IOS-内存检测以及优化

    IOS-内存检测以及优化 2014年01月23日 Jason PS:开始写这个系列的笔记:主要是对过去自己比较模糊的一些概念进行测试,明确结果,提高自己 IOS 应用如果占用系统的内容过大(8GB), ...

  2. TCP/IP协议详解笔记——ARP协议和RARP协议

    ARP:地址解析协议 对于以太网,数据链路层上是根据48bit的以太网地址来确定目的接口,设备驱动程序从不检查IP数据报中的目的IP地址.ARP协议为IP地址到对应的硬件地址之间提供动态映射. 工作过 ...

  3. 教学生jni编程(一)

    我的所有文件的目录在/home/michelle/WORKM/DAYCODE/day0426/,请根据个人情况适当调整 1)创建一个HelloWorld.Java文件,如下: public class ...

  4. Python学习杂记_5_列表常用操作

    列表操作 列表时用方括号括起来的一组元素值,是可变变量,可通过下表取值,也可以通过下表来修改值,列表中的元素是有序的,可以是不同的基本数据类型,如: names=[1, 2, 3, “abc”, “d ...

  5. 通过vSphere-client虚拟化服务器

    一.什么是vClientvClient是vSphere的重要组件之一.用于用户连接ESXi或vCenter管理和分配vSphere的各种资源.有vClient和WebvClient两个版本.安装部署了 ...

  6. LeetCode OJ--Combination Sum **

    https://oj.leetcode.com/problems/combination-sum/ 给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得 ...

  7. java gc --- 四种引用

    古龙有<七种武器>,java里有四种引用. 下文主要是对 <understanding-weak-references>这一博文的重点进行翻译 强引用,strong refer ...

  8. Linux每日一坑002

    0.删除软连接目录时,目录后面一定不要有斜杠!最好用mv代替rm. 1.数据库安装后要初始化数据库,不然无法登陆,会报权限错误,原谅我的无知,跪了. mysql_install_db --user=m ...

  9. (9)JavaScript-DOM(文档对象模型)

    DOM是针对 HTML 和 XML 文档的一个 API ,描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分  一.节点层次 <html> <head> &l ...

  10. Eclipse 最常用的 10 组快捷键,个个牛逼!

    虽然栈长我现在不怎么用 Eclipse 了,但 Eclipse 的快捷键还是忘不了的,可以说 Eclipse 的快捷键很方便,恰到好处. 今天,我大概整理了 10 组 Eclipse 我觉得比较常用的 ...