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 ...
随机推荐
- DAY5-常用模块
一 time与datetime模块 二.random模块 三.os模块 四.sys模块 五 shutil模块 六 json&pickle模块 七 shelve模块 八 xml模块 九 conf ...
- 高性能MySQL笔记-第5章Indexing for High Performance-004怎样用索引才高效
一.怎样用索引才高效 1.隔离索引列 MySQL generally can’t use indexes on columns unless the columns are isolated in t ...
- java过滤器 Fliter
定义:过滤器是一个服务器端组件,他可以截取用户端请求信息与响应信息,并对信息进行过滤 例:当进入csdn要进行文章编辑时,检测用户是否登录,若未登录,跳转到登录界面. 过滤器操作方法: init() ...
- SRA数据转成fastq
Downloading and installing the SRA Toolkit step1: 下载并安装SRAtoolkit (Download the Toolkit from the ...
- 两个进程之间的通讯——pipe 管道
在实际工作中,已经编辑好了NIPT_analysis的软件,该软件一般的输入文件是sam文件,但是为了集成进入测序仪器,需要直接从比对软件的标准输出中读取sam文件,省去了比对软件和NIPT_anal ...
- Json Post到 https的坑 - the underlying connection was closed an unexpected error occurred on a send(远程服务器未知错误导致关闭)
最近做了一个安装包,安装包会弹出dotnet的 窗体,这个安装包会去调用https的一个api.用测试程序测试窗体都是好的.一旦打入安装包后,就报错.研究了半天,原来是https惹的祸 解决方案: . ...
- 更新anaconda及所有包
################################## # 更新Anaconda conda update conda # 更新所有包 conda update --all ###### ...
- labview中的移位寄存器、循环隧道,自动索引隧道的区别
对于循环结构(For 循环.while循环)而言,循环体内的数据域外部数据的传递是通过以下三种方式: 1.移位寄存器2.循环隧道3.自动索引隧道 第一.各自的区别.作用 循环隧道,就是把数据传入传出循 ...
- Djianggo 在windows中安装出现报错的解决方案
Djianggo 在win7下 安装会报错 Traceback (most recent call last):File "setup.py", line 4, in <mo ...
- Xshell连接linux(deepin)时提示ssh服务器拒绝了密码,请再试一次解决方法
用Xshell root连接时显示ssh服务器拒绝了密码,应该是应该是sshd的设置不允许root用户用密码远程登录 修改 /etc/ssh/sshd_config文件,注意,安装了openssh才会 ...