您的位置:首页 > 其它

CodeForces - 363D Renting Bikes 二分+贪心

2017-02-24 11:06 417 查看
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?

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 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.

Example

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.

思路: 原本以为是一个小贪心,可仔细一想不太对,如果仅仅为了最小花费去买更多的自行车 那买到的自行车可能不是最多的.

所以这里我们要二分所能购买的最多自行车的数量,然后贪心去验证钱能否买到这么多的自行车.并求最小花费.

贪心验证时我们先对自行车的费用和小朋友的前进行升序排序,然后二分买的自行车数量,

验证时因为为了买最多的自行车所以我们肯定要买最便宜的那mid个自行车,而且为了钱能够买到这么多自行车所以我们要用拥有私人钱最多的那mid个小朋友去买,然后判断缺的钱和共有的钱之间的关系。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
int b[maxn],p[maxn];
int n,m,a;
int check(int x)
{
long long sum=0;
for(int i=x;i>=1;i--)//用拥有钱最多的x个小朋友去买最便宜得x辆自行车
{
if(b[n+i-x]<p[i]) //为了买更多的自行车所以钱最多的要买最贵的.
sum+=p[i]-b[n+i-x];//记录少的钱
}
if(sum<=a)
return 1;
else
return 0;
}
int main()
{
scanf("%d%d%d",&n,&m,&a);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
for(int i=1;i<=m;i++)
scanf("%d",&p[i]);
sort(b+1,b+1+n);
sort(p+1,p+1+m);
int l=0,r=min(m,n),mid,ans=-1;//这里的上界要是n,m中的最小值,否则验证时会数组出现越界.
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
{
l=mid+1;
ans=mid;
}
else
r=mid-1;
}
printf("%d ",ans);
long long s=0;
for(int i=1;i<=ans;i++)
{
s+=p[i];
}
s-=a;
if(s<0)
s=0;
printf("%lld\n",s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: