Gym 100886J Sockets(二分+贪心)

原创 2016年08月29日 21:18:58

Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the i-th plug multiplier has ai sockets.

A device or plug multiplier is supplied with electricity if it is either plugged into the electrical socket, or if it is plugged into some plug multiplier which is supplied with electricity.

For each device j, Valera knows the safety value bj which is the maximum number of plug multipliers on the path between the device and the electrical socket in his flat. For example, if bj = 0, the device should be directly plugged into the socket in his flat.

What is the maximum number of devices Valera could supply with electricity simultaneously? Note that all devices and plug multipliers take one socket to plug, and that he can use each socket to plug either one device or one plug multiplier.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 2·105), the number of plug multipliers and the number of devices correspondingly.

The second line contains n space-separated integers a1, a2, ..., an (2 ≤ ai ≤ 2·105). Here, the number ai stands for the number of sockets on the i-th plug multiplier.

The third line contains m space-separated integers b1, b2, ..., bm (0 ≤ bj ≤ 2·105). Here, the number bj stands for the safety value of the j-th device.

Output

Print a single integer: the maximum number of devices that could be supplied with electricity simultaneously.

Examples
Input
3 5
3 2 2
1 2 2 1 1
Output
4
Input
3 3
2 2 2
1 2 2
Output
3

题意:给n个设备和m个排插,开始有一个插口,每个排插有ai个插孔,每个设备有一个安全值,表示它最多接到第bi层过度排插上,问最多能插多少电器。


分析:将b值从大到小排序后,最优方案一定可以表示成一段b的前缀,于是我们可以二分这个前缀的长度来贪心地验证,优先放b值小的,优先接a值大的插板。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include <unordered_map>
#define INF 0x3f3f3f3f
#define eps 1e-9  
#define MOD 1000000007 
using namespace std;
int n,m,a[200005],b[200005];
bool check(int mid)
{
	if(mid == 1) return true;
	if(b[mid] == 0) return false;
	int res = a[1],nowdep = 1,now = mid,nowi = 1;
	while(true) 
	{
		while(b[now] == nowdep && now && res) 
		{
			res--;
			now--;
		}
		if(now && !res) return false;
		if(!now) return true;
		int tot = 0;
		while(res)
		{
			if(nowi == n) break;
			tot += a[++nowi];
			res--;
		}
		nowdep++;
		res += tot;
	}
}
bool camp(int x,int y)
{
	return x > y;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	for(int i = 1;i <= m;i++) scanf("%d",&b[i]);
	sort(a+1,a+1+n,camp);
	sort(b+1,b+1+m,camp);
	int l = 0,r = m;
	while(l != r)
	{
		int mid = (l+r)/2 + 1;
		if(check(mid)) l = mid;
		else r = mid-1;
	}
	cout<<l<<endl;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Educational Codeforces Round 12 E. Beautiful Subarrays

One day, ZS the Coder wrote down an array of integers a with elements a1,  a2,  ...,  an. A subarr...

Uva 11134 Fabled Rooks

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_proble...

Hdu-5573 Binary Tree(贪心构造)

The Old Frog King lives on the root of an infinite tree. According to the law, each node should conn...

Codeforces Round #283 (Div. 2) D. Tennis Game

Petya and Gena love playing table tennis. A single match is played according to the following rules:...

ICPCCamp 2017-Day1 E.Lowest Common Ancestor(树链剖分/lct)

题意:给一棵带权树,问每个点和所有标号小于它的点的带权lca和。 分析:这种lca和可以看成是两段权值不同的链的差,这样我们用lct来维护树上每段链的带权重量和,每次插入一个点。 ...

Codeforces Round #355 (Div. 2) D. Vanya and Treasure

Vanya is in the palace that can be represented as a grid n × m. Each room contains a single chest, ...

Hdu-5289 Assignment (二分+RMQ || 单调队列)

Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro...

Hdu-5371 Hotaru's problem(马拉车算法)

Problem Description Hotaru Ichijou recently is addicated to math problems. Now she is playing with ...

Codeforces Round #347 (Div. 1) B. International Olympiad

International Abbreviation Olympiad takes place annually starting from 1989. Each year the competiti...

HYSBZ - 1036 树的统计Count

一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w。我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)