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\n",s,sum);
return 0;
}
rent bike问题(二分+贪心)的更多相关文章
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...
- Codeforces_732D_(二分贪心)
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- CF732D Exams 二分 贪心
思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...
- $CF949D\ Curfew$ 二分/贪心
正解:二分/贪心 解题报告: 传送门$QwQ$ 首先这里是二分还是蛮显然的?考虑二分那个最大值,然后先保证一个老师是合法的再看另一个老师那里是否合法就成$QwQ$. 发现不太会搞这个合不合法的所以咕了 ...
- $bzoj2067\ szn$ 二分+贪心
正解:二分+贪心 解题报告: 传送门$QwQ$ 题目大意就说有一棵树,然后要用若干条线覆盖所有边且不能重叠.问最少要用几条线,在用线最少的前提下最长的线最短是多长. 昂首先最少用多少条线这个还是蛮$e ...
- leetcode1552题解【二分+贪心】
leetcode1552.两球之间的磁力 题目链接 算法 二分+贪心 时间复杂度O(nlogn + nlogm) 1.根据题意描述,我们需要将m个球放入到n个篮子中,根据题目中数据范围描述发现m &l ...
- CF2.C(二分贪心)
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
随机推荐
- IntelliJ IDEA 导入Project
一.方式一 File---->Close Project 这样的户每次需要import,都要close一次,非常不方便,如果能在File下面岂不是更好? 二.方式二 File---->Se ...
- 第一天:tomcat相关知识和浏览器的访问机制
1.tomcat的目录结构 1)bin目录:启动和关闭tomcat以及其他的脚本命令 2)conf目录:存放各种配置文件 a.server.xml配置文件的配置: *<host/>标签: ...
- Android中SQLite查询date类型字段出现有返回但是为错误值的情况
出现该情况的原因是因为查询精度与数据库中存储精度不相同造成的,例如,查询精度为 YYYY-MM-DD 但是存储精度为 YYYY-MM-DD HH:MM:SS,就会出现该错误. 更改查询精度为YYYY- ...
- day70-oracle PLSQL_01基本语法
PLSQL是一种程序,和java一样都是一种程序. sql developer是基于java的jdbc连接数据库.根据java的jdbc,只要有数据库的驱动,就可以连接这个数据库.这个工具默认不需要任 ...
- SQl Server 与数据库的第一次相遇
数据库就是 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,简单说就是存储在硬盘上的文件. 市面上常见数据库有<关系数据库系统>: ORACLE(甲骨文).DB2.S ...
- utf8转unicode
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
- 【Boost】boost库获取格式化时间
获取时间方式 格式一:YYYYMMDD #include<iostream> #include<string> #include<boost/date_time/greg ...
- 40、inner mate distance for paired reads in a bam file
参考:https://www.biostars.org/p/106291/#106344 1.inner mate distance : the distance from the right mos ...
- 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(四)
ViewPagerIndicator+ViewPager 要想使用ViewPagerIndicator,要使用到viewPagerlibrary开源库 top.xml <?xml version ...
- ObjectARX环境搭建之vs2010+objectArx2012+AutoCAD2012
---------------------------------------------------------------------------------------------------- ...