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.
题目大意:
现在有A金钱用作公共财产,现在有N个小孩和M辆车。
每个小孩都想买一辆车,而且只能买一辆车,问最多小孩子们可以买多少辆车,同时问买这些辆车的最小个人总花费是多少。
思路:
①首先我们将每个人拥有的金钱数量和车辆的价格从小到大排序。
②然后我们可以二分购买的车辆数。那么两点法贪心去判定能否通过这N个小孩子的个人财产和公共财产来购买这些车。(显然我们这里要买最便宜的车);
我们希望拥有钱数多的小孩子买贵的车,如果钱不够,用公用财产来补。
③最后我们能够二分出最多购买的车辆数,那么我们知道,这些最便宜的车辆一定是可以购买到的,那么我们再二分最小总个人花费,然后判断一下即可。
判断方法很简单,sum(p【1】~p【最多车脸熟】)-mid即可,如果这个值小于等于A.那么就是可行的。
我们二分出最小个人总花费即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long int
ll b[140000];
ll p[140000];
int n,m;
ll tot;
int num;
int Slove(ll mid)
{
ll sum=0;
for(int i=1;i<=num;i++)
{
sum+=p[i];
}
if(sum-mid<=tot)return 1;
else return 0;
}
int check(int mid)
{
ll tmp=tot;
int j=n;
for(int i=mid;i>=1;i--)
{
if(b[j]>=p[i])j--;
else
{
if(tmp>=0)
{
if(b[j]+tmp>=p[i])
{
tmp-=p[i]-b[j];
j--;
}
else return 0;
}
else return 0;
}
}
return 1;
}
int Getnum()
{
int l=0;
int r=min(n,m);
int num=-1;
while(r-l>=0)
{
int mid=(l+r)/2;
if(check(mid)==1)
{
num=mid;
l=mid+1;
}
else r=mid-1;
}
return num;
}
int main()
{
while(~scanf("%d%d%lld",&n,&m,&tot))
{
num=0;
for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
sort(b+1,b+1+n);
for(int i=1;i<=m;i++)scanf("%lld",&p[i]);
sort(p+1,p+1+m);
num=Getnum();
printf("%d ",num);
if(num==0)
{
printf("0\n");
continue;
}
ll ans=-1;
ll l=0;
ll r=100000000000000000;
while(r-l>=0)
{
ll mid=(l+r)/2;
if(Slove(mid)==1)
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%lld\n",ans);
}
}