bzoj2014 [Usaco2010 Feb]Chocolate Buying

来源:互联网 发布:stc51单片机的优点 编辑:程序博客网 时间:2024/03/29 22:26

Description

    贝西和其他奶牛们都喜欢巧克力,所以约翰准备买一些送给她们。奶牛巧克力专卖店里
有N种巧克力,每种巧克力的数量都是无限多的。每头奶牛只喜欢一种巧克力,调查显示,
有Ci头奶牛喜欢第i种巧克力,这种巧克力的售价是P。
    约翰手上有B元预算,怎样用这些钱让尽量多的奶牛高兴呢?下面举个例子:假设约翰
有50元钱,商店里有S种巧克力:
  巧克力品种    单价高兴的奶牛数量    1
    2
    3
    4
    5
    5
    1
    10
    7
    60
    3
    1
    4
    2
    1
   
    显然约翰不会去买第五种巧克力,因为他钱不够,不过即使它降价到50,也不该选择
它,因为这样只会让一头奶牛高兴,实在太浪费了。最好的买法是:第二种买1块,第一种
买3块,第四种买2块,第三种买2块,正好用完50元,可以让1+3+2+2=8头牛高兴。

Input

  第一行:两个用空格分开的整数:N和B,1<=N≤100000,1≤B≤10^18
  第二行到N+1行:第i+l行,是两个用空格分开的整数:Pj和Ci,1≤Pi,Ci≤10^18

Output

  第一行:一个整数,表示最多可以让几头奶牛高兴

Sample Input

5 50
53
11
10 4
7 2
60 1

Sample Output

8

就是限制价值为1背包dp

那这样就可以用贪心搞了

#include<cstdio>#include<algorithm>#define LL long longusing namespace std;struct item{LL c,p;}a[100010];int n;LL m,ans,rest;inline bool cmp(const item &a,const item &b){return a.c<b.c;}inline LL min(LL a,LL b){return a<b?a:b;}inline LL read(){    LL x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int main(){n=read();m=read();for (int i=1;i<=n;i++)  {a[i].c=read();a[i].p=read();}sort(a+1,a+n+1,cmp);for (int i=1;i<=n;i++)  {  LL num=min(m/a[i].c,a[i].p);  ans+=num;m-=num*a[i].c;  }printf("%lld",ans);}


0 0