题目:
You get to work and turn on the computer. You start coding and give little thought to the RAM role in the whole process. In this problem your task is to solve one of the problems you encounter in your computer routine.
We'll consider the RAM as a sequence of cells that can contain data. Some cells already contain some data, some are empty. The empty cells form the so-called memory clusters. Thus, a memory cluster is a sequence of some consecutive empty memory cells.
You have exactly n memory clusters, the i-th cluster consists of ai cells. You need to find memory for m arrays in your program. The j-th array takes 2bj consecutive memory cells. There possibly isn't enough memory for all m arrays, so your task is to determine what maximum number of arrays can be located in the available memory clusters. Of course, the arrays cannot be divided between the memory clusters. Also, no cell can belong to two arrays.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 106). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The next line contains mintegers b1, b2, ..., bm (1 ≤ 2bi ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
5 3 8 4 3 2 2 3 2 2
Output
2
Input
10 6 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
Output
6
Note
In the first example you are given memory clusters with sizes 8, 4, 3, 2, 2 and arrays with sizes 8, 4, 4. There are few ways to obtain an answer equals 2: you can locate array with size 8 to the cluster with size 8, and one of the arrays with size 4 to the cluster with size 4. Another way is to locate two arrays with size 4 to the one cluster with size 8.
In the second example you are given 10 memory clusters with size 1 and 6 arrays with size 1. You can choose any 6 clusters and locate all given arrays to them.
题意:第一行 输入 n,m,n代表有n个容器,m代表有m个物体,第二行 输入n个数,代表n个容器的体积,第三行输入m个物体
体积,是用二进制的指数bi表示的(真正的体积是2^bi),求最多能放多少个物体
思路:贪心想法,先放小的,因为m个物体,就是用二进制表示的,可以想想能不能用位运算,可以把n个容器用二进制表示,进行二进制减法运算
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define M 41
#define Max 1000010
#define ll long long
ll s[M];
ll b[M];
ll a[M];
ll n,m;
int jin(ll k)
{
ll jie = 0,now = 0;
for(ll i = k+1;i<M;i++)
{
if(a[i])
{
jie = 1;
now = i;
break;
}
}
if(!jie)
return 0;
ll tt = b[k] / s[now - k]; //看看能不能从now为借一个位直接全部放到k位上
if(tt == 0 ) // 若不能就和做减法一样,从 k~now-1 位为上每一位都加上9(这里是二进制,所以每一位上都加1)
{
for(ll i = k +1 ;i < now; i++)
a[i] += 1;
a[k] += 2;
a[now] -= 1;
return 1;
}
ll pp = min(tt,a[now]);
a[now] -= pp;
a[k] += pp*s[now - k];
return 1;
}
int main()
{
for(int i = 0;i<M;i++)
s[i] = ((ll)1<<i); // 位运算 左移运算符 一定要把1化为 LL型;
while(~scanf("%lld%lld",&n,&m))
{
ll k;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(ll i = 0; i < n;i++) // 把n个容器化作2进制
{
scanf("%lld",&k);
for(ll j = 0;j<M && k>=s[j]; j++)
if(k&s[j])
a[j]++;
}
for(ll i = 0;i<m;i++)
{
scanf("%lld",&k);
b[k] ++;
}
ll ans = 0;
int flag = 1;
for(ll i = 0;i<M;i++) // 进行二进制减法运算
{
while(b[i])
{
ll tt = min(a[i],b[i]);
a[i] -= tt;
b[i] -= tt;
ans += tt;
if(b[i])
{
if(!jin(i))
{
flag = 0;
break;
}
}
else break;
}
if(!flag)
break;
}
printf ("%lld\n",ans);
}
return 0;
}