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个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...
随机推荐
- 【CF1073A】Diverse Substring(签到)
题意:给定一个由小写字母组成的串,要求找出一个子串使得其中出现最多的字母不超过它长度的一半下取整 n<=1e3 思路: #include<cstdio> #include<cs ...
- Codeforces 899 C.Dividing the numbers-规律
C. Dividing the numbers time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces 754A(搜索)
设s[i][j]为序列i到j的和,当s[i][j]≠0时,即可从i跳到j+1.目标为从1跳到n+1,所以按照题意暴力即可. #include <bits/stdc++.h> using n ...
- STM32命名
STM32产品命名 示例: STM32 F 100 C 6 T 6 B XXX 1 2 3 4 5 6 7 8 9 从上面的料号可以看出以下信息: ST品牌ARM Cortex-Mx系列内核32位超值 ...
- 基于WPF系统框架设计(7)-TextBox/PasswordBox在ViewModel中支持回车命令
应用场景 我现在做一个系统登录功能,要求在PasswordBox上输完密码后回车,能够响应Enter事件,并执行ViewModel中对应的方法.如果登录成功则隐藏当前窗口显示主窗体,登录失败则焦点返回 ...
- 聊聊、Zookeeper 数据结构和操作命令
Zookeeper 的视图结构跟标准的 Unix 文件系统很像,都有一个根节点 / .在根节点下面就是一个个的子节点,我们称为 ZNode.ZNode 是 Zookeeper 中最小数据单位,在 ZN ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
- DotnetBrowser入门教程-入门
在.net core时代,web开发基本可以用.net core 2.0取代了.但是在传统领域,桌面开发仍然是不可以抛弃的,譬如: 1.用户需要和串口或者硬件打交道. 2.用户只想简单的安装好就使用, ...
- HDFS冗余数据块的自动删除
HDFS冗余数据块的自动删除 在日常维护hadoop集群的过程中发现这样一种情况: 某个节点由于网络故障或者DataNode进程死亡,被NameNode判定为死亡,HDFS马上自动开始数据块的容错拷贝 ...
- UVA1406 - A Sequence of Numbers(树状数组)
UVA1406 - A Sequence of Numbers(树状数组) 题目链接 题目大意: 给定N个数字.给两种操作:C x: 把这N个数字都加上x. Q x:查询这N个数里面有多少个数字和2^ ...