codeforces363D——Renting Bikes(二分,贪心)

来源:互联网 发布:dwg是什么软件 编辑:程序博客网 时间:2024/05/01 14:30

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.

Examples
input
2 2 105 57 6
output
2 3
input
4 5 28 1 1 26 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辆单车,首先一共有a元的公款,每个人还有自己的私款。

私款只能用在自己的单车上,问你一共最多能租下多少辆单车。


首先把单车价格和每个人的钱排序。

最大化可行解,二分找出最多能租的单车数目。

把价格最低的那些单车相加,减去公款数目就是最小花费。



#include <algorithm>#include <cstring>#include <cstdio>#include <iostream>using namespace std;const int MAXN=100010;int b[MAXN];int p[MAXN];bool C(int x,int n,int m,int a){    int i=n-x,j=0;for(;i<n&&j<m;i++,j++){if(b[i]<p[j]){a-=(p[j]-b[i]);}if(a<0)            break;}if(a>=0)return true;elsereturn false;}int main(){int n,m,a;scanf("%d%d%d",&n,&m,&a);for(int i=0;i<n;i++){scanf("%d",b+i);}for(int i=0;i<m;i++){scanf("%d",p+i);}sort(p,p+m);sort(b,b+n);int lb=0,ub=min(n+1,m+1);while(ub-lb>1){int mid=(lb+ub)>>1;if(C(mid,n,m,a))lb=mid;elseub=mid;}int r=lb;if(r==0){printf("0 0\n");return 0;}long long sum=0;for(int i=0;i<r;i++){sum+=p[i];}printf("%d %I64d\n",r,sum>a?sum-a:0);}




0 0
原创粉丝点击