Codeforces Round #211 (Div. 2)D. Renting Bikes(二分,想法题,好题)
2016-12-20 00:06
423 查看
题目链接
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 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
output
input
output
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。下面一行n个数是每个人的私有钱数,下一行m个数是每辆自行车的价格,。现在题目要求你求出在可以得到最多自行车的条件下,使得每个人私有钱数花费最少。要求是输出最多自行车数,每个人花费的最少私人的钱之和。
题解:
对于最大自行车数,可以二分,对于k辆自行车,肯定是找钱最多的k个人去买钱最少的那k辆车,然后对每个人,如果自己的钱能买就自己买,不够就用公的钱,最后判断剩下的公共的钱还有没有。
确定完最大的自行车数目后,计算钱数最少的k辆车的总花费s,如果s小于a,那么答案就是0,否则为a-s。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=10000+100;
ll b[maxn],p[maxn];
bool cmp(ll a,ll b)
{
return a>b;
}
int n,m;
ll a;
bool check(int x)
{
ll ans=0;
for(int i=1;i<=x;i++)
{
if(b[x-i+1]<p[i]) ans+=p[i]-b[x-i+1];
}
return ans<=a;
}
int main()
{
scanf("%d%d%I64d",&n,&m,&a);
rep(i,1,n+1) scanf("%I64d",&b[i]);
rep(i,1,m+1) scanf("%I64d",&p[i]);
sort(p+1,p+m+1);
sort(b+1,b+n+1,cmp);
int l=0,r=min(n,m);
int ans=0;
while(l<=r)
{
int m=(l+r)/2;
if(check(m))
{
ans=m;
l=m+1;
}
else r=m-1;
}
ll res=0;
for(int i=1;i<=ans;i++)
res+=p[i];
if(res<a) res=0;
else res=res-a;
printf("%d %I64d\n",ans,res);
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 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,以及可以公用的钱数a。下面一行n个数是每个人的私有钱数,下一行m个数是每辆自行车的价格,。现在题目要求你求出在可以得到最多自行车的条件下,使得每个人私有钱数花费最少。要求是输出最多自行车数,每个人花费的最少私人的钱之和。
题解:
对于最大自行车数,可以二分,对于k辆自行车,肯定是找钱最多的k个人去买钱最少的那k辆车,然后对每个人,如果自己的钱能买就自己买,不够就用公的钱,最后判断剩下的公共的钱还有没有。
确定完最大的自行车数目后,计算钱数最少的k辆车的总花费s,如果s小于a,那么答案就是0,否则为a-s。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=10000+100;
ll b[maxn],p[maxn];
bool cmp(ll a,ll b)
{
return a>b;
}
int n,m;
ll a;
bool check(int x)
{
ll ans=0;
for(int i=1;i<=x;i++)
{
if(b[x-i+1]<p[i]) ans+=p[i]-b[x-i+1];
}
return ans<=a;
}
int main()
{
scanf("%d%d%I64d",&n,&m,&a);
rep(i,1,n+1) scanf("%I64d",&b[i]);
rep(i,1,m+1) scanf("%I64d",&p[i]);
sort(p+1,p+m+1);
sort(b+1,b+n+1,cmp);
int l=0,r=min(n,m);
int ans=0;
while(l<=r)
{
int m=(l+r)/2;
if(check(m))
{
ans=m;
l=m+1;
}
else r=m-1;
}
ll res=0;
for(int i=1;i<=ans;i++)
res+=p[i];
if(res<a) res=0;
else res=res-a;
printf("%d %I64d\n",ans,res);
return 0;
}
相关文章推荐
- Codeforces Round #211 (Div. 2)-D. Renting Bikes,二分!感谢队友出思路!
- Codeforces Round #211 (Div. 2) D题(二分,贪心)解题报告
- Codeforces Round #211 (Div. 2)(二分贪心)
- Codeforces Round #211 (Div. 2) D题Renting Bikes
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
- Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
- Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
- Codeforces Round #388 (Div. 2)D. Leaving Auction(二分+思维)
- Codeforces Round #367 (Div. 2) B Interesting drink【二分】
- Codeforces Round #256 (Div. 2) D. Multiplication Table(二分查找)
- Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
- Codeforces Round #402 (Div. 2) D. String Game(二分)
- Codeforces Round #283 (Div. 2) D Tennis Game ——枚举,二分
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(想法题,构造题)
- Codeforces Round #370 (Div. 2) C. Memory and De-Evolution【dfs+想法】
- Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分