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 pj rubles.
In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal 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?
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 of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike.
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.
2 2 10 5 5 7 6
2 3
4 5 2 8 1 1 2 6 3 7 5 2
3 8
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.
题意:一堆人租自行车,有共有的资金和私人资金。私人资金只能给自己用,共有资金可以通用。列出n个人的钱,然后列出m辆车的租费,求最大租车数以及个人最少花的钱 。
思路:二分最多租车数,每个mid 都贪心一下,让有最多钱的k个人去买最便宜的k个车
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, presum, person[maxn], pri[maxn], temp;
int check(int k)
{
temp = presum;
for(int i = 1; i <= k; i++)
{
if(person[n-k+i]+temp >= pri[i])
{
if(person[n-k+i] - pri[i] < 0)
temp += (person[n-k+i]-pri[i]);
}
else
return 0;
}
return 1;
}
int main()
{
while(cin >> n >> m >> presum)
{
for(int i = 1; i <= n; i++)
{
scanf("%d", &person[i]);
}
for(int i = 1; i <= m; i++)
{
scanf("%d", &pri[i]);
}
sort(person+1, person+1+n);
sort(pri+1, pri+1+m);
int l = 0, r = min(n,m), ans = 0, ansl = 0;
while(l <= r)
{
int mid = (l+r)/2;
if(check(mid))
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
for(int i = 1; i <= ans; i++)
{
ansl += pri[i];
}
ansl -= presum;
printf("%d %d\n", ans, max(ansl, 0));
}
return 0;
}