1003 Mixing Milk

题目:Mixing Milk

Since milk packaging(牛奶包装) is such a low margin(低利润,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(方法,manners为礼貌).

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.

Input

The first line contains two integers, N and M. The first value, N, (0 <= N <= 2,000,000) is the amount of milk that Merry Milk Makers' want per day. The second, M, (0 <= M <= 5,000) is the number of farmers that they may buy from.

The next M lines (Line 2 through M+1) 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.

Output

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 Input
100 5
5 20
9 40
3 10
8 80
6 30
Sample Output
630
 
思路:将每个农民的牛奶价格和牛奶数量看成一个整体,按照价格从低到高排序,然后先找价格最低的农民买,如果他的牛奶足够满足需求,则向他一个人购买即可,如不能满足全部需求,则先把他的牛奶买光,再找价格第二低的买,以此类推。
 
代码:
 
#include <stdio.h> /*标准IO*/
#include <stdlib.h> /*标准库函数,(void*)malloc(int size)需要用到*/

int main()
{
    int i, j, flag;
    long N, M, temp, money = 0;/*有的编译器int为2字节有的4字节,故用long*/
    long *P = NULL, *A = NULL;
    /*二元运算符除了->和.之外运算符两边空一格,逗号,分号后面空一格*/

    scanf("%ld %ld", &N, &M);

    /*动态申请内存,考虑内存申请失败的情况*/
    P = (long*)malloc(M * sizeof(long));
    A = (long*)malloc(M * sizeof(long));
    if(NULL == P || NULL == A)/*NULL写在==左边,这样不小心把==写成=编译器能报错*/
    {
        printf("Out of memory!\n");
        exit(1);
    }

    /*不同功能块之间空一行*/
    for(i = 0; i < M; i++)
    {
        /*指针加一并不是加一个字节,而是它指向的数据类型的长度*/
        scanf("%ld %ld", P + i, A + i);
    }

    /*冒泡排序外层循环控制循环次数,n个数最多n-1趟排序*/
    for(i = 0; i < M-1; i++)
    {
        flag = 0;/*增加标记位,数组有序之后后面几趟的比较就没必要了*/
        /*内层循环随着趟数增加,比较的数减少*/
        for(j = 0; j < M-1-i; j++)
        {
            if(P[j] > P[j+1])
            {
                temp = P[j];
                P[j] = P[j+1];
                P[j+1] = temp;
                temp = A[j];
                A[j] = A[j+1];
                A[j+1] = temp;
                flag = 1;
            }
        }
        if(0 == flag)
        {
            break;
        }
    }

    for(i = 0; N > A[i]; i++)/*第i个农民存量不能满足需求*/
    {
        money += P[i] * A[i];/*把他的牛奶全买了*/
        N -= A[i];/*需求量减少*/
    }
    money += N * P[i];/*第i个农民能满足需求,则只需购买N的量*/

    printf("%ld\n",money);
    /*malloc 之后一定要free释放内存*/
    free(P);
    free(A);
    /*内存虽然释放了,但P和A依然记录着释放的内存地址,将其置NULL,避免野指针*/
    P = NULL;
    A = NULL;
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值