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;
}


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

Gym - 101173J Jazz Journey 图论、枚举贪心、Interesting

题意:给出一个图,要求把图上的所有边按照给出的顺序跑一遍,求最小代价。其中有单向边u->v、v->u, 和方程u->v->u、v->u->v,对于u->v->u必须先走u->v再走v->u但中间可以断...

codeforces732E Sockets(贪心)

题意: 给出一些给定功率的电脑和插座,只有功率匹配才能连接,另外有一些适配器,连在插座上可以把插座功率降为1/2,求最多匹配电脑数和需要的适配器数以及具体映射。 要点: 这题就是贪心+STL的应...

Codeforces 732E Sockets【贪心】

E. Sockets time limit per test 2 seconds memory limit per test 256 megabytes input stand...

【CF 732E】Sockets(优先队列+贪心)

【CF 732E】Sockets(优先队列+贪心)题目大意: n台电脑,m个供电器。 每台电脑和每个供电器都有电量,当电脑i的电量和供电器j电量相同时,可以连接供电。现在提供变压器,可以连接到供电...

Gym100694A(贪心+背包)

链接:点击打开链接 题意:给出n个物品,和背包容量m,给出每个物品的价值,重量,和取得这个物品获得的额外的背包容量,可以最多选择两个物品获得额外的背包容量,问最后的最大价值 代码:#include...

Gym 100703K Word order 简单贪心

K. Word order time limit per test 2 seconds memory limit per test 256 megabytes inp...

Gym - 101194E Bet [2016-2017 ACM-ICPC CHINA-Final E][贪心]

点击打开题目 题意: 有n个球队比赛, 你可以赌哪个队获胜. 如果你下注的队赢了, 你将获得c + ai / bi * ci的钱, 如果输了, 那么你将输ci (ci为你下注的钱数). 问, 在...

GYM 100090 I.Retakes(贪心+优先队列)

Description 补考n场考试,每场考试有一个开始日期和补考费用,而且超过日期也需付出和超出日期天数一样的费用,问如果安排补考使得每场考试的花费中的最大值最小 Input 第一行一整数n表...
  • V5ZSQ
  • V5ZSQ
  • 2017年03月07日 19:06
  • 170

贪心——Playing with numbers ( Gym 101061 E )

题目链接: http://codeforces.com/gym/101061/problem/E 分析: 给出一个长度最大为100000位的数字,求从中剔除掉N个数字后得到到最大数和最小数。 题解...
  • FeBr2
  • FeBr2
  • 2016年08月16日 10:55
  • 343

Gym - 101102F F. Exchange 贪心、简单题

题意:可以对字符串进行一个操作,把2个字母互换(这2个字母是不同的),一旦互换必须把所有x换成y,所有y换成x,最多可以进行一次这样的操作,求字典序最小的字符串。 贪心、简单题 用cnt[i]表示字...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Gym 100886J Sockets(二分+贪心)
举报原因:
原因补充:

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