CodeForces 363D - Renting Bikes

6 篇文章 0 订阅
3 篇文章 0 订阅

A group of n schoolboys decided to ride bikes. As nobody ofthem has a bike, the boys need to rent them.

The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bikecosts pj rubles.

In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boyhas bi personal rubles. The shared budget can be spent on any schoolchildrenarbitrarily, but each boy's personal money can be spent on renting only thisboy's bike.

Each boy can rent at most one bike, one cannot give his bike to somebody else.

What maximum number of schoolboys will be able to ride bikes? Whatminimum sum of personal money will they have to spend in total to let as manyschoolchildren ride bikes as possible?

Input

The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence ofintegers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike.

Output

Print two integers r and s, where r is the maximum number of schoolboys that canrent a bike and s is the minimum total personal money needed torent r bikes. If the schoolchildren cannot rent anybikes, then r = s = 0.

Sample test(s)

input

2 2 10
5 5
7 6

output

2 3

input

4 5 2
8 1 1 2
6 3 7 5 2

output

3 8

Note

In the first sample both schoolchildren can rent a bike. For instance,they can split the shared budget in half (5 rubles each). In this case one ofthem will have to pay 1 ruble from the personal money and the other one willhave to pay 2 rubles from the personal money. In total, they spend 3 rubles oftheir personal money. This way of distribution of money minimizes the amount ofspent personal money.

 

思路:

一开始用贪心做,一直到比赛结束都没做出来。比赛途中有想过用二分,但是具体没想好,怕不够时间写。

代码是参考网上acmer的,用n和m中较小的数进行二分,原因是如果能有k个人借到车,那么最有钱的k个人一定能借到最便宜的k辆车,先尽量用他们自己的钱借车,如果公共资金有剩余,则继续二分。最后注意一下是不是借完车公共资金还有剩余就是了。其实这样二分也是有贪心的思想在的。



代码:
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 100005;

int b[N], p[N];

bool cmp(int a, int b)
{
	return a>b;
}

int main()
{
	int n, m, a;
	scanf("%d%d%d", &n, &m, &a);
	int i;

	for (i = 0; i<n; i++)
		scanf("%d", &b[i]);

	for (i = 0; i<m; i++)
		scanf("%d", &p[i]);

	sort(b, b + n, cmp);
	sort(p, p + m);

	int l = 0, r = min(n, m) - 1;
	int mid;
	int temp;

	while (l <= r)
	{
		mid = (l + r) >> 1;
		temp = 0;

		for (i = 0; i <= mid; i++)
		{
			temp += max(0, p[mid - i] - b[i]);
			if (temp>a)
				break;
		}

		if (temp <= a)
			l = mid + 1;
		else
			r = mid - 1;
	}

	for (i = 0; i<l; i++)
	{
		a -= p[i];
	}
	printf("%d %d\n", l, -min(a, 0));


	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值