CodeForces

来源:互联网 发布:freebsd mysql 编辑:程序博客网 时间:2024/06/05 02:51

题目地址:点击打开链接


题意:n个人,每个人自己有一定的钱,还有公共的预算。m辆自行车,每辆车有一定的价格。求最多能租几辆车并且用的私房钱的总数最少。


思路:遇到这种题总是想不到正确的思路方向哭。二分车辆,然后贪心验证。最后用私房钱最少就是需要买价格最低的k辆车花费和-公共的钱,注意下不能是负数。还有注意用long long。


代码:

#include<algorithm>#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 1e5+5;typedef long long ll;ll a[maxn], b[maxn], n, m, s;ll judge(int x){    ll res = 0;    for(int i = 1; i <= x; i++)    {        if(a[n-x+i] < b[i])            res += b[i]-a[n-x+i];    }    return res;}int main(void){    while(cin >> n >> m >> s)    {        for(int i = 1; i <= n; i++)            scanf("%I64d", &a[i]);        for(int i = 1; i <= m; i++)            scanf("%I64d", &b[i]);        sort(a+1, a+n+1);        sort(b+1, b+m+1);        ll l = 0, r = min(m, n), ans = 0, ansval = 0;        while(l <= r)        {            int mid = (l+r)/2;            if(judge(mid) <= s) l = mid+1, ans = mid;            else r = mid-1;        }        ll t = 0;        for(int i = 1; i <= ans; i++)            t += b[i];        ansval = t-s;        printf("%I64d %I64d\n", ans, ansval < 0 ? 0 : ansval);    }    return 0;}


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.

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.



0 0