题目地址:http://codeforces.com/contest/363/problem/D
思路:贪心+二分。
对于可购买的最多的单车数量s,可以对其进行二分查找求出s,首先将学生个人钱a[n]进行由大到小排序,单车价格d[m]由小到大排序,当查找数为h时,按照贪心思想,应由学生钱多的前h个去买 单车价格小的前h辆,这样即可判断是否能够买h辆车,
对于买s辆车学生个人钱花费最小值 w,可以由大到小一一匹配,最后w减去剩下的公共金钱即可
Code:
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAX_N=100005;
const int MAX_M=100005;
int n,m,p;
int a[MAX_N],d[MAX_M];
bool cmp(const int &p,const int &q){
return p>q;
}
bool Find(int t);
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m>>p;
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<m;++i)
cin>>d[i];
sort(a,a+n,cmp);
sort(d,d+m);
int l=0,r=min(n,m);
while(l<=r){
int h=(l+r)/2;
if(Find(h)) l=h+1;
else r=h-1;
}
LL ans=0;
for(int i=0,j=r-1;i<r;--j,++i)
{
if(a[i]<d[j]){
ans+=a[i];
p-=d[j]-a[i];
}else ans+=d[j];
}
ans-=p;
if(ans<0) ans=0;
cout<<r<<" "<<ans<<endl;
return 0;
}
bool Find(int t)
{
int pp=p;
bool boo=true;
for(int i=0,j=t-1;i<t&&boo;--j,++i)
{
if(a[i]<d[j]) pp-=d[j]-a[i];
if(pp<0) boo=false;
}
return boo;
}