USACO1.3.1--Mixing Milk

Mixing Milk

Since milk packaging is such a low margin business, it is important to keep the price of the raw product (milk) as low as possible. Help Merry Milk Makers get the milk they need in the cheapest possible manner.

The Merry Milk Makers company has several farmers from which they may buy milk, and each one has a (potentially) different price at which they sell to the milk packing plant. Moreover, as a cow can only produce so much milk a day, the farmers only have so much milk to sell per day. Each day, Merry Milk Makers can purchase an integral amount of milk from each farmer, less than or equal to the farmer's limit.

Given the Merry Milk Makers' daily requirement of milk, along with the cost per gallon and amount of available milk for each farmer, calculate the minimum amount of money that it takes to fulfill the Merry Milk Makers' requirements.

Note: The total milk produced per day by the farmers will be sufficient to meet the demands of the Merry Milk Makers.

PROGRAM NAME: milk

INPUT FORMAT

Line 1: Two integers, N and M. 
The first value, N, (0 <= N <= 2,000,000) is the amount of milk that Merry Milk Makers wants per day. The second, M, (0 <= M <= 5,000) is the number of farmers that they may buy from. 
Lines 2 through M+1: The next M lines each contain two integers, Pi and Ai
Pi (0 <= Pi <= 1,000) is price in cents that farmer i charges.
Ai (0 <= Ai <= 2,000,000) is the amount of milk that farmer i can sell to Merry Milk Makers per day.

 

SAMPLE INPUT (file milk.in)

100 5

5 20

9 40

3 10

8 80

6 30

OUTPUT FORMAT

A single line with a single integer that is the minimum price that Merry Milk Makers can get their milk at for one day.

SAMPLE OUTPUT (file milk.out)

630
题解:贪心思想,把牛奶价格按升序排序(我用的是快排,其实可以用桶排序把代码压缩,因为价格pi为[0,1000],范围灰常小),从低到高买入,直到买够N即可。
View Code
 1 /*

 2 ID:spcjv51

 3 PROG:milk

 4 LANG:C

 5 */

 6 #include<stdio.h>

 7 void qsort(long a[],long p[],long l,long r)

 8 {

 9     long i,j,mid,temp;

10     i=l;

11     j=r;

12     mid=p[(l+r)/2];

13     while(i<=j)

14     {

15         while(p[i]<mid) i++;

16         while(p[j]>mid) j--;

17         if(i<=j)

18         {

19             temp=p[i];

20             p[i]=p[j];

21             p[j]=temp;

22             temp=a[i];

23             a[i]=a[j];

24             a[j]=temp;

25             i++;

26             j--;

27 

28         }

29     }

30     if(i<r) qsort(a,p,i,r);

31     if(j>l) qsort(a,p,l,j);

32 

33 }

34 int main(void)

35 {

36     freopen("milk.in","r",stdin);

37     freopen("milk.out","w",stdout);

38     long p[5005],a[5005];

39     long i,m,n,ans;

40     scanf("%ld%ld",&n,&m);

41     for(i=0; i<m; i++)

42         scanf("%ld%ld",&p[i],&a[i]);

43     qsort(a,p,0,m-1);

44     i=0;

45     ans=0;

46     while(n&&i<m)

47     {

48         if((n-a[i])>=0)

49         {

50             n-=a[i];

51             ans+=a[i]*p[i];

52             i++;

53 

54         }

55         else

56         {

57             ans+=n*p[i];

58             n=0;

59         }

60     }

61     printf("%ld\n",ans);

62     return 0;

63 }

 

 

你可能感兴趣的:(USACO)