Mixing Milk

编程入门 行业动态 更新时间:2024-10-24 10:17:58

<a href=https://www.elefans.com/category/jswz/34/1711142.html style=Mixing Milk"/>

Mixing Milk

题目:                                      

Mixing Milk

My Tags  (Edit)
  Source : Unknown
  Time limit : 3 sec   Memory limit : 32 M

Submitted : 8025, Accepted : 3387

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.

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

                                                       题目大意:有工厂需要M个牛奶,共有N个农民来提供,但是没个农民牛奶的产量和牛奶的价格都不一样,问工厂要用怎样的方式用最少的钱去买牛奶!  知道这是一个贪心问题!

                                                    解题思路:

                                                                 1、首先要买牛奶,肯定是先买便宜的,够不够数,以后添上即可,即先对价格进行排序,同时将牛奶的数量排好位置

                                                                  2、这里采用冒泡排序,左右比较,有两次循环

                                                                  3、排好序后当然开始算多少钱的问题了!先买最便宜的,接着买下一个人的,这是有循环的,终止条件是买够了数!

                                                   总结:这里的细节问题在于,使用 malloc 函数申请空间,最后有一个释放空间的过程,这样就节省了空间了

                                                   源代码:

                                                           

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;int swap(int a, int b)
{int tmp = a;a = b;b = tmp;
}
int main()
{long i, j;long N, M;long  produce = 0;long  money = 0;long  *Pi = NULL, *Ai = NULL;scanf("%ld %ld", &N, &M);Pi = (long*)malloc(M*sizeof(long));Ai = (long*)malloc(M*sizeof(long));for(i = 0; i < M; i++){scanf("%ld %ld", &Pi[i], &Ai[i]);}for(i = 0; i < M; i++){for(j = 0; j < M-1; j++){if(Pi[j] >Pi[j+1]){swap(Pi[j], Pi[j+1]);swap(Ai[j], Ai[j+1]);}}}for(i = 0; N > Ai[i] ; i++){N -= Ai[i];         //这个是produce还是小于money += Ai[i] * Pi[i];}money += N * Pi[i];printf("%ld\n", money);free(Pi);free(Ai);return 0;
}

更多推荐

Mixing Milk

本文发布于:2024-02-10 16:44:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1676274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mixing   Milk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!