zoukankan      html  css  js  c++  java
  • D. Renting Bikes 二分

    D. Renting Bikes
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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 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 test(s)
    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.

    二分找最大的车数。。。。。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<stack>
    #include<set>
    #include<iostream>
    #include<vector>
    #include<queue>
    //ios_base::sync_with_stdio(false);
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    #define sz(v) ((int)(v).size())
    #define rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define repf(i, a, b) for (int i = (a); i <= (b); ++i)
    #define repd(i, a, b) for (int i = (a); i >= (b); --i)
    #define clr(x) memset(x,0,sizeof(x))
    #define clrs( x , y ) memset(x,y,sizeof(x))
    #define out(x) printf(#x" %d
    ", x)
    #define sqr(x) ((x) * (x))
    typedef long long LL;
    
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 300000;
    int b[maxn];
    int p[maxn];
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int n,m,a;
        while(cin>>n>>m>>a)
        {
            repf(i,1,n)
                scanf("%d",&b[i]);
            
            repf(i,1,m)
                scanf("%d",&p[i]);
            
            int ans = 0;
            sort(b+1,b+1+n);
            sort(p+1,p+1+m);
            
            int Min = min(n,m);
            int L = 0;
            int R = Min;
            int mid;
            while(L <= R)
            {
                int temp = a;
                mid = (L+R)/2;
                int i;
                for(i = n-mid +1;i<=n;++i)
                {
                    int gap = p[i - (n-mid+1)+1] - b[i];
                    if(gap > 0)
                    {
                        if(temp>=gap)
                        {
                            temp-=gap;
                        }else
                        {
                            break;
                        }
                    }
                }
                if(i == n+1)
                {
                    L = mid + 1;
                }else
                    R = mid - 1;
            }
            
            repf(i,1,R)
                ans+=p[i];
            cout<<R<<" "<<max(0,ans - a)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Ghost博客安装
    PHP变量作用域
    ssh文件传输命令:sz与rz命令
    excel怎么固定第一行
    memcache和redis区别
    Memcache分布式部署方案
    Memcache服务器端参数说明
    Memcache基础教程
    在Windows下安装Memcached
    MySQL体系结构和存储引擎概述
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3418522.html
Copyright © 2011-2022 走看看