Codeforces 363D - Renting Bikes(贪心二分)
2017-02-24 14:47
495 查看
D. Renting Bikes
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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个人要租m个自行车,每个人有若干钱,有共同财产若干钱.
个人财产只能用在自己的花费上,不能给别人.
求能够租到的最大数量和花费的公共财产数量.
解题思路:
贪心二分,用财产最多的人租最贵的车,然后二分逼近.
AC代码:
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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个人要租m个自行车,每个人有若干钱,有共同财产若干钱.
个人财产只能用在自己的花费上,不能给别人.
求能够租到的最大数量和花费的公共财产数量.
解题思路:
贪心二分,用财产最多的人租最贵的车,然后二分逼近.
AC代码:
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 1e5+5; int b[maxn]; int p[maxn]; int n, m, a; bool check(int mid) { int sum = 0; for(int i = 0; i < mid; i++) if(p[mid-1-i] > b[n-1-i]) sum += p[mid-1-i] - b[n-1-i]; if(sum > a) return 0; return 1; } main() { ios::sync_with_stdio(0); cin >> n >> m >> a; for(int i = 0; i < n; i++) cin >> b[i]; for(int i = 0; i < m; i++) cin >> p[i]; sort(b,b+n); sort(p,p+m); int l = 0; int r = min(n,m); while(l <= r) { int mid = (l+r) >> 1; if(check(mid)) l = mid+1; else r = mid-1; } int sum = 0; for(int i = 0;i < r; i++) sum += p[r-1-i]; sum = max(0LL, sum-a); cout << r << " " << sum; return 0; }
相关文章推荐
- CodeForces 363D Renting Bikes (二分+贪心)
- CodeForces - 363D Renting Bikes (二分+贪心)
- CodeForces 363D.Renting Bikes(二分+贪心)
- CodeForces 363D 【二分+贪心】
- Codeforces 760B Frodo and pillows【贪心+二分】
- CodeForces 201 E.Thoroughly Bureaucratic Organization(贪心+二分+鸽巢原理)
- Codeforces 479D Long Jumps(贪心+二分)
- Codeforces 551C - GukiZ hates Boxes(二分加贪心)
- Codeforces 609D Gadgets for dollars and pounds【二分+贪心】
- Codeforces 363D Renting Bikes【二分+贪心】
- codeforces 527D D. Clique Problem(二分+线段树+贪心+dp)
- CodeForces 609 D.Gadgets for dollars and pounds(二分+贪心)
- Codeforces 825D 二分贪心
- codeforces 803D Magazine Ad(二分+贪心)
- codeforces - 732D Exams 【二分 + 贪心】
- Codeforces 732D Exams【贪心+二分】
- codeforces 377D exam贪心判定+二分
- CodeForces - 363D Renting Bikes(二分,贪心)
- CodeForces 343C Read Time 二分+贪心
- CodeForces 614D 二分+贪心