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个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...
随机推荐
- Ubuntu中配置Tomcat与Eclipse整合
Apache Tomcat 作为web服务器已经广泛用于Java Servlets 和 JSP (Java Server Pages) 开发. 环境:Ubuntu10.10 java环境的配置见另一篇 ...
- EBImage - - 给图片增加字符
EBImage中文文档 英文版出处:http://www.bioconductor.org/packages/release/bioc/vignettes/EBImage/inst/doc/EBIma ...
- Python Challenge 第七关
第七关,只有一张图片,右键源代码也什么都没有,只是这图片上有一行类似马赛克一样的部分.看来答案只有在这张图上找了.下载了图片,去网上搜一下有什么库可以处理图像.搜到了一个PIL,发现安装的python ...
- html5---音频视频基础一
//html5 音频和视频 :标签 a: audio,video b: source :视频容器 a:容器文件,类似于压缩了一组文件 -音频轨道 -视频轨道 -元数据:封面,标题,字幕等 -格式:.a ...
- Android,一条线串联实心圆布局
最近遇到一个简单的布局,不是listview的形式.就只是单纯的下图这种: 此界面布局代码: <?xml version="1.0" encoding="utf-8 ...
- linux运维(重点)
linux 添加系统变量: export log="log-all-$(date +%Y-%m-%d).php" export log="log-all-$(date + ...
- vs2013载入zlib库,即include "zlib.h"
转自wo13142yanyouxin原文vs2013载入zlib库,即include "zlib.h" 在程序中,我们经常要用到压缩,解压函数.以压缩函数compress为例进行说 ...
- wireshark学习笔记
之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括http,TCP,UDP,等网络协议包. 记得大学的 ...
- Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason:
Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason: Connection t ...
- cordova 中de.sitewaerts.cordova.documentviewer 插件 看pdf图片缩略图与实际图片不一致
//if (document == nil) // Unarchive failed so create a new ReaderDocument object //{ document = [[Re ...