Codeforces 363D Renting Bikes

D. Renting Bikes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A group of n schoolboys decided to ride bikes. As nobody of them 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 bike costs pjrubles.

In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bipersonal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy'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? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible?

Input

The first line of the input contains three integers nm and a (1 ≤ n, m ≤ 1050 ≤ 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 of integers 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 can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0.

Examples
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 of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.

题意 : 给定一定数量的人与自行车,在租得最大数量的自行车时候,使个人的花费最少。也就是公共预算花的最多

思路:肯定要进行排序,首先要获得最多的自行车数量,使用二分进行筛选出最大值,此时的花掉的预算一定小于给出预算,在花掉的个人钱减去剩下的预算就是个人的最少花费。

注意: 使用二分法,   数组下标从1开始比较好写

#include <iostream> 
#include <cstdio>
#include <algorithm>

using namespace std; 

const int N = 1e5+10; 
int worth[N],weight[N]; 
int main()
{
	int m,n,a; 
	int ans;
	long long temp; 
	cin>>n>>m>>a; 
	for ( int i=1; i<=n; i++ ) 	 scanf("%d",&worth[i]); 
	for ( int i=1; i<=m; i++ )	 scanf("%d",&weight[i]);
	sort( worth+1,worth+n+1); 
	sort( weight+1,weight+m+1); 
	
	int left,right,mid; 
	left = 0; 
	right = min( n,m ); 
	
	while ( left<=right ) {
		mid = ( left+right ) /2; 
		temp=0; 
		for ( int i=1; i<=mid; i++ ) {
			if ( worth[n-i+1]<weight[mid-i+1] ) {
				temp += weight[mid-i+1]-worth[n-i+1]; 
			}
		}
		if ( temp>a ) {
			right = mid-1; 
		}
		else {
			ans = mid; 
			left = mid+1; 
		}
	}
	printf("%d ",ans); 
	temp = 0;  
	for ( int i=1; i<=ans; i++ ) {
		if ( worth[n+1-i]<weight[ans-i+1] ) {
			temp+= worth[n-i+1] ; 
			a -= -worth[n-i+1]+weight[1+ans-i] ; 
		}
		else {
			temp += weight[ans-i+1]; 
		}
	}
	cout<<max((long long)0,temp-a)<<endl; 
	
	return 0 ; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值