BNUOJ 4156 Chocolate Buying

贪心,每次都找价值最小的巧克力。做的时候很怕爆long long,结果1Y了。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct T
{
    long long p;
    long long c;
};
T f[110000];
bool cmp(T a,T b)
{
    return a.p<b.p;
}
int main()
{
    int n,i;
    long long sum,t,ans;
    scanf("%d%lld",&n,&sum);
    for (i=0; i<n; i++)
    {
        scanf("%lld%lld",&f[i].p,&f[i].c);
    }
    sort(f,f+n,cmp);
    ans=0;
    for (i=0; i<n; i++)
    {
        t=sum/f[i].p;
        if (t >= f[i].c)
        {
            sum-=(f[i].p*f[i].c);
            ans+=f[i].c;
        }
        else
        {
            sum-=(f[i].p*t);
            ans+=t;
        }
        if (t == 0 || sum == 0)
            break;
    }
    printf("%lld\n",ans);
}


你可能感兴趣的:(BNUOJ 4156 Chocolate Buying)