USACO Section 1.3.1 Mixing Milk

简单题,贪心,不多说

/*
ID: zhuihun1
PROG: milk
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include<algorithm>
#define MAXSIZE 20
using namespace std;
struct Farmer
{
    int price;
    int amount;

};
int need,n;
Farmer fa[5005];
bool comp(Farmer a,Farmer b)
{
    return a.price<b.price;
}
int main()
{
    ofstream fout ("milk.out");
    ifstream fin ("milk.in");
    int i;
    fin>>need>>n;
    for(i=0;i<n;i++)
        fin>>fa[i].price>>fa[i].amount;
    sort(fa,fa+n,comp);
    int sum=0,cost=0;
    i=0;
    while(sum<need)
    {
        if(fa[i].amount+sum<need)
        {
            sum+=fa[i].amount;
            cost+=fa[i].amount*fa[i].price;
        }
        else
        {
            cost+=fa[i].price*(need-sum);
            sum=need;
        }
        i++;

    }

    fout<<cost<<endl;

    return 0;
}


你可能感兴趣的:(USACO Section 1.3.1 Mixing Milk)