Hoj 1003 Mixing Milk

简单的贪心题。

链接:http://acm.hit.edu.cn/hoj/problem/view?id=1003

每次购买最大量的最便宜的牛奶即可。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

struct Milk
{
    int p;
    int a;
};

Milk milk[5002];

bool cmp(Milk a,Milk b)
{
    return a.p<b.p;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n,m;
    int asum = 0;
    int psum = 0;
    int stopDot;

    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&milk[i].p,&milk[i].a);
    }
    sort(milk,milk+m,cmp);

    for(int i=0;i<m;i++)
    {
        if(asum + milk[i].a<=n)
        {
            asum += milk[i].a;
            psum += milk[i].p * milk[i].a;
        }
        else
        {
            stopDot = i;
            break;
        }
    }

    psum += milk[stopDot].p * (n - asum);

    printf("%d\n",psum);
    return 0;
}




你可能感兴趣的:(Hoj 1003 Mixing Milk)