Code forces363D Renting Bikes

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<numeric>//STL数值算法头文件
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
typedef long long LL;
const int N=1e5+10;

LL a[N],b[N],w;

int main()
{
    int n,m;
    while(~scanf("%d%d%lld",&n,&m,&w))
    {
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=m; i++)
            scanf("%lld",&b[i]);
        sort(a+1,a+n+1);
        sort(b+1,b+m+1);
        int ans=0,cas=100;
        int left=0,right=min(n+1,m+1);
        while(left<right&&cas--)
        {
            int mid=(left+right)/2;
            LL res=0;
            for(int i=1; i<=mid; i++)
            {
                if(a[n+1-i]<b[mid+1-i])   //最有钱的几个人去买最便宜的车以至于能买到最多数量的车;
                    res+=(b[mid+1-i]-a[n+1-i]);
            }
            if(res<=w)
            {
                left=mid;
                ans=mid;
            }
            else
                right=mid;
        }
        printf("%d",ans);
        LL sum=0;
        for(int i=1; i<=ans; i++)
        {
            if(a[n+1-i]<=b[ans+1-i])
            {
                sum+=a[n+1-i];
                w-=(b[ans+1-i]-a[n+1-i]);
            }
            else
                sum+=b[ans+1-i];
        }
        printf(" %lld
",max(0LL,sum-w));//0LL表示把0转化为LL型,因为max函数要保持类型一致
    }
    return 0;
}

原文地址:https://www.cnblogs.com/nyist-xsk/p/7264889.html