zoukankan      html  css  js  c++  java
  • rent bike问题(二分+贪心)

    题目描述:

    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个人的私款,以及租每辆车的价格钱数

    需要你做的是能租最多的车,以及花费自己的私款最少,并且每个人的私款只能用来自己租自行车

    思路(贪心+二分)

    既然想要花费自己的私款最少,就让有钱的人去租便宜的自行车,这样下来肯定花费私款(自己的钱)才相对较少,并且租的自行车还相对多,故利用二分把可以租的车的范围确定下来

    具体思路的实现见代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,m,k;
    int a[100005],b[100005]; //用来存储人的私款和租车价格 
    long long int judge(int x)//这个是在模拟比较私款数和租车价钱 
    {
    	long long int sum=0;
    	for(int t=0;t<x;t++)
    	{
    		if(a[n-x+t]<b[t])
    		{
    			sum+=(b[t]-a[n-x+t]);//这是相当于钱不够,需要公款支援 
    		}
    	}
    	return sum;//这是需要公款支援的总数 
    }
    int main()
    {
    	
    	cin>>n>>m>>k;
    	for(int t=0;t<n;t++)
    	{
    		scanf("%d",&a[t]);
    	} 
    	for(int t=0;t<m;t++)
    	{
    		scanf("%d",&b[t]);
    	} 
    	sort(a,a+n);
    	sort(b,b+m);
    	//用二分来确定可以租的车数 
    	int left=0;
    	int right=min(n,m);//二分是由小的一部分决定的,比如人数少,提供的车多,一旦二分很有可能这一部分这一部分没有相对应的另一部分 
    	//以上说法如果不太明白可以去假设几个去试试应该就理解了 
    	int s=0;//用来表示买的车数 
    	while(left<=right)
    	{
    
    		int mid=(left+right)>>1;
    		//判断需要的公款和提供的公款进行比较 
    		//如果小于则说明买完mid辆车还可以买,所以往后继续找可以买的车,否则往前找 
    		if(judge(mid)<=k)
    		{
    			left=mid+1;
    			s=mid;
    		} 
    		else
    		{
    			right=mid-1;
    		}
    	}
    	//找完买了多少辆车后可以把租这些车的价钱求和 
        long long int sum=0; 
    	for(int t=0;t<s;t++)
    	{
    		sum+=b[t];
    	} 
    	//这时候的sum就是需要的私款数 
    	if(sum>k)
    	{
    		sum=sum-k;
    	}
    	//公款够,不需要私款,就不能减,不然出负数,显然不对 
    	else
    	{
    		sum=0;
    	} 
    	printf("%d %lld
    ",s,sum);
    	return 0;
    } 
  • 相关阅读:
    Java编程之委托代理回调、内部类以及匿名内部类回调(闭包回调)
    JavaEE开发之记事本完整案例(SpringBoot + iOS端)
    JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
    JavaEE开发之SpringBoot工程的创建、运行与配置
    JavaEE开发之SpringMVC中的自定义消息转换器与文件上传
    Scala.js v0.1 发布,在浏览器直接运行 Scala
    如何编写 Cloud9 JavaScript IDE 的功能扩展
    在 Cloud 9 中搭建和运行 Go
    MicroPHP 2.2.0 发布
    StrongSwan 5.1.1 发布,Linux 的 IPsec 项目
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10782107.html
Copyright © 2011-2022 走看看