CodeForces 363D Renting Bikes

本文探讨了一群学生如何在有限的共享预算和个人资金下,以最少的个人支出实现最多人数骑自行车的问题。通过排序和二分查找的方法,找到能租用的最大自行车数量及对应的最小个人花费。

B - Renting Bikes
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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

Sample Input

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

Hint

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.


这个题关键就是稍微有点贪心的思想,去二分找出最大车子数。

剩下的就很简单了,用所有的车子钱减去预算就可以了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=1e5+7;
int n,m,p;
int a[MAXN],b[MAXN];
int main()
{
    int i;
    scanf("%d%d%d",&n,&m,&p);
    for(i=0;i<n;++i)scanf("%d",&a[i]);
    for(i=0;i<m;++i)scanf("%d",&b[i]);
    sort(a,a+n);
    sort(b,b+m);
    int low=0,high=min(n+1,m+1);
    int ans=0,ca=100;
    while(low<=high&&ca--)
    {
        int mid=(low+high)>>1;
        long long sum=0;
        for(i=0;i<mid;++i)
        {
            if(a[n-1-i]<b[mid-1-i])sum+=b[mid-1-i]-a[n-1-i];
        }
        if(sum<=p)
        {
            low=mid;
            ans=mid;
        }
        else high=mid;
    }
    long long sum=0;
    printf("%d ",ans);
    for(i=0;i<ans;++i)
    {
        sum+=b[i];
    }
    sum-=p;
    if(sum<0)sum=0;
    printf("%lld\n",sum);
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值