Codeforces 309C Memory for Arrays 二进制模拟进位
题目链接:点击打开链接
题意:
给定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 二进制模拟进位的更多相关文章
- Educational Codeforces Round 2 A. Extract Numbers 模拟题
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #454 C. Shockers【模拟/hash】
C. Shockers time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #528-A. Right-Left Cipher(字符串模拟)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Codeforces Round #249 (Div. 2) (模拟)
C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 740C. Alyona and mex 思路模拟
C. Alyona and mex time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- codeforces 723B Text Document Analysis(字符串模拟,)
题目链接:http://codeforces.com/problemset/problem/723/B 题目大意: 输入n,给出n个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...
随机推荐
- jQuery1.4与json格式兼容问题
原文发布时间为:2010-10-10 -- 来源于本人的百度文章 [由搬家工具导入] 原来使用jQuery1.3.2编写的代码,更换到1.4.2后,使用jQuery.ajax()加载的json文件,不 ...
- 基于websocket的页面聊天程序
注:主要的问题是当浏览器窗口直接关闭时,后台会报异常,因为后台还在继续向浏览器端写数据,但是浏览器已经关闭了,会报java.net.SocketException:peer:Socket write ...
- 宝宝舌苔发白,消化不好 http://wenwen.soso.com/z/q103192661.htm
你好,宝宝咳嗽,如果舌苔是白的,则是风寒咳嗽,说明孩子寒重,咳嗽的痰也较稀.白黏,并兼有鼻塞流涕,这时应吃一些温热.化痰止咳的食品.如果孩子的舌苔是黄.红,则是风热咳嗽,说明孩子内热较大,咳嗽的痰黄. ...
- 转:C#制作ORM映射学习笔记三 ORM映射实现
现在开始实现ORM的主体模块,首先需要在项目中新建一个类,命名为DbAccess,然后在项目的引用中添加两个dll,分别是MySql.Data.dll和System.Data.SQLite.dll,这 ...
- sprak 环境搭建的坑
1,/etc/hosts下的ip master/slave 的对照 /etc/sysconfig/network 配置: NETWORKING=yes HOSTNAME=master 以及spark/ ...
- HDU 1998 奇数阶魔方【模拟填数/注意边界和细节】
奇数阶魔方 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- httperf+autobench测试web应用
测试性能相关的概念理解 httperf使用 主页: http://www.hpl.hp.com/research/linux/httperf/ 下载: http://httperf.googleco ...
- oracle中的替换函数replace和translate函数
.translate 语法:TRANSLATE(char, from, to) 用法:返回将出现在from中的每个字符替换为to中的相应字符以后的字符串. 若from比to字符串长,那么在from中比 ...
- MySQL复制表结构和内容到另一张表(转)
MySQL不要看它小,一个开源的产物,要学习它的东西真的很多.而它的一切是SQL Server无法比拟的. 复制表结构及数据到新表 create table 新表 select * from 旧表 只 ...
- Tiny4412 支持 adb reboot-bootloader
硬件版本: Tiny4412ADK + S700 4GB u-boot 版本: u-boot-2010-12 linux版本: Linux-3.0.8 版本一 支持 adb re ...