# 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;
}


wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis

12-03 1675

11-25 1015

12-30 738

05-01

02-19 45万+

#### Java基础知识面试题（2020最新版）

©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie

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