YbtOj练习:二分1 喂养宠物

http://noip.ybtoj.com.cn/contest/15/problem/1

二分要养几只兔兔即可。

#include<bits/stdc++.h>
using namespace std;
const int N=55;
int n,totfood;
struct rabbit{
    int hunger,greed,cost;
    bool operator < (const rabbit &G) const
    {
        return cost<G.cost;
    }
}a[N];
bool check(int x)
{
    for(int i=1;i<=n;i++) a[i].cost=a[i].hunger+(x-1)*a[i].greed;
    sort(a+1,a+n+1);
    int res=0;
    for(int i=1;i<=x;i++) res+=a[i].cost;
    return res>totfood;
}
int main()
{
    scanf("%d%d",&n,&totfood);
    for(int i=1;i<=n;i++) scanf("%d",&a[i].hunger);
    for(int i=1;i<=n;i++) scanf("%d",&a[i].greed);
    int l=0,r=n;
    while(l<r)
    {
        int mid=l+r+1>>1;
        if(check(mid)) r=mid-1;
        else l=mid;
    }
    cout<<l;
    return 0;
}

 

posted @ 2020-08-10 08:33  Gold_stein  阅读(248)  评论(0编辑  收藏  举报