记录编号 124478 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [HZOI 2014] 采姑娘的小蘑菇 最终得分 100
用户昵称 GravatarOI永别 是否通过 通过
代码语言 C++ 运行时间 2.622 s
提交时间 2014-10-04 10:09:45 内存使用 1.46 MiB
显示代码纯文本
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100100
int a[N],b[N];
int h[N];
int n,m;
int tot = 0;
inline int getint()
{
	int x = 0;
	char ch;
	while (!isdigit(ch = getchar()));
	x = x * 10 + ch - 48;
	while (isdigit(ch = getchar())) x = x * 10 + ch - 48;
	return x;
}
 
 
void up(int x)
{
	int p = x >> 1;
	while (p)
	{
		if (h[p] < h[x]) swap(h[p],h[x]);
		else return;
		x = p;
		p = x >> 1;
	}
	return;
}
 
void down(int x)
{
	int p = x << 1;
	while (p <= tot)
	{
		if (h[p] < h[p+1] && p < tot) ++p;
		if (h[p] > h[x]) swap(h[p],h[x]);
		else return;
		x = p;
		p = x << 1;
	}
	return;
}
 
void push(int x)
{
	h[++tot] = x;
	if (tot == 1) return;
	up(tot);
	return;
}
 
void pop()
{
    h[1] = h[tot--];
	if (!tot) return;
	down(1);
	return;
}
 
 
bool can(int x)
{
	memset(h,0,sizeof(h));
	tot = 0;
	for (int i = 1; i <= n; i++) 
		push(a[i]);
	for (int i = x; i >= 1; i--)
	{
		if (tot == 0) return 0;
		int tem = h[1];
		pop();
		if (b[i] > tem)
		{
			return 0;
		}
		else{
			tem -= b[i];
			push(tem);
		}
	}
	return 1;
}
 
int main()
{
	freopen("mushro.in","r",stdin);
	freopen("mushro.out","w",stdout);
	n = getint();
	m = getint();
	for (int i = 1; i <= n; i++)
	{
		a[i] = getint();
	}
	int l = 0,r = m,ans = 0;
	for (int i = 1; i <= m; i++) b[i] = getint();
	sort(b + 1, b + 1 + m);
	while (l <= r)
	{
		int mid = (l + r) >> 1;
		if (can(mid)) ans = mid,l = mid + 1;
		else r = mid - 1;
	}
	printf("%d\n",ans);
	return 0;
}