codeforces 363D

12 篇文章 0 订阅
6 篇文章 0 订阅

原题链接


D. Renting Bikes

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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


没想到二分。。。许久没碰二分题了,有点生疏。。。 题意:有n个学生租m辆车子,每人最多租一辆,给出每个学生拥有的money和每辆车子的price,还有一个援助基金a(这个基金可以给任何人用),尽可能让每个人都骑上车子,求可以购买的最大车子数量,以及购买这些车子所花费的最少金钱。。。

二分最大车辆数,求出最大车子,从而得出最少金钱。。



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

int b[100005],p[100005];
int n,m,a;

int check(int x)
{
    int i;
    ll sum=0;
    for(i=0;i<=x;i++)
        if(b[n-1-i]<p[x-1-i])
        sum+=p[x-1-i]-b[n-1-i];
    if(sum>a)
        return 0;
    return 1;
}

int main()
{
    int i,j,ans;
    while(~scanf("%d%d%d",&n,&m,&a))
    {
        for(i=0;i<n;i++)
            scanf("%d",b+i);
        for(i=0;i<m;i++)
            scanf("%d",p+i);
        sort(b,b+n);
        sort(p,p+m);
        int mid,l=0,r=min(n,m);
        while(l<=r)
        {
            mid=(l+r)/2;
            if(check(mid)){
                    ans=mid;
                l=mid+1;
            }
            else
                r=mid-1;
        }
        printf("%d ",ans);
        ll sum=0;
        for(i=0;i<ans;i++)
            sum+=p[i];
        sum-=a;
        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、付费专栏及课程。

余额充值