题目链接:点击打开链接

题意:

给定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. shell特殊符号用法大全

      # 注释符号(Hashmark[Comments]) 1.在shell文件的行首,作为shebang标记,#!/bin/bash; 2. 其他地方作为注释使用,在一行中,#后面的内容并不会被执行, ...

  2. vim技巧记录底行模式的使用(1)

    若正在编辑文件,临时须要查看目录: (1)直接在底行模式下使用ls命令,完整的为:!ls 在我这里就显示如下的结果: functionpointer    helloWorld.s       tes ...

  3. golang xorm MSSQL where查询案例

    xorm官方中文文档 参考 http://xorm.io/docs/ 以sqlserver为例 先初始化连接等... engine, err := xorm.NewEngine("mssql ...

  4. Centos7 安装docker ce

    一. 安装docker 1.升级rpm包 yum -y update 2. 通过命令设置Docker CE 资源库: yum install -y yum-utils yum-config-manag ...

  5. LeetCode OJ-- Letter Combinations of a Phone Number ***

    https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ 使用递归,深搜,使用 map 保存已经处理过的结果 cl ...

  6. LeetCode OJ--Merge Intervals @

    https://oj.leetcode.com/problems/merge-intervals/ 合并区间 //排序 sort(intervals.begin(),intervals.end(),C ...

  7. 导入Excel表中的数据

    第一步:转换导入的文件 private void btnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new ...

  8. python-urllib/urllib2模块

    urllib与urllib2: urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL.这意味着,你不可以伪装你的User Agent字符串等. ...

  9. gradients的一些注意点

    Each variable has a [.grad_fn] attribute that references a Function that has created the Variable(ex ...

  10. fauxbar.bak

    {"options": { "almostdone":"0", "backup_searchEngines":" ...