`
Simone_chou
  • 浏览: 192756 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

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 

   题意:

   要装满M(范围是0到5000)那么大的牛奶,提供N(范围是0到2000000)个店铺。每个店铺最大供应奶量为Ai(范围是0到2000000),其中每一升需要花Pi(范围是0到1000)的钱,购买的量可以等于或者小于最大供应量,输出满足需求量M的最小花费金额。

 

   思路:

   使金额最小,那么则从单价最小的开始购买,在为满足需求量时,应购买该店铺的最大供应量。如果购买该店铺的最大供应量超过了需求量的时候,则按需求量和该店铺的最大供应量的差值来购买,这时候达到的金额就为最小值了。

 

   AC:

 

#include<cstdio>
#include<algorithm>
using namespace std;

struct seller
{
	int per;
	int max;
};
seller people[5005];

int cmp(seller a,seller b)
{
	return a.per<b.per;
}

int main()
{
	int N,M,i,sum=0,price=0,left;
	scanf("%d%d",&N,&M);
	for(i=0;i<M;i++)
	  scanf("%d%d",&people[i].per,&people[i].max);
	sort(people,people+M,cmp);
	
//	printf("\n");
//	for(i=0;i<M;i++)
//		printf("%d %d\n",people[i].per,people[i].max);
//保险起见,还是输出来看看有没有正确存储
   
    for(i=0;i<M;i++)
    {
    	if(sum<N)
    	{
//这时候的已购买量加上这个店铺的最大供应还是小于需求量的话
//就购买该店铺全部的牛奶(该店铺的最大供应量)	
            if(sum+people[i].max<=N)
    	    {
    	    	sum+=people[i].max;
    	    	price+=people[i].per*people[i].max;
    	    }
//如果加上后超过需求量了,则按差值购买
    		else if(sum+people[i].max>N)
    		{
//这里必须用left来保存剩余量
//如果直接在sum进行赋值操作的话
//sum+=(N-sum);prince+=(people[i].per*(N-sum));
//后面乘以的sum已经改变了,已经变成了N,而不是两者的差值了
//写之前还是要动下脑子再写……    			
                        left=N-sum;
			sum=N;
    			price+=(people[i].per*left);
    		}
    	}
//买够了就跳出循环,输出最小金额		
                if(sum==N) break;
    }
    printf("%d\n",price);
}

   总结:

 

   第一次没有Bug没有想错然后一遍AC的,虽然题目简单,但是还是有点小激动。一开始还是犯了个低级错误,所以写每句代码前都要考虑清楚才行,检验下到底是错还是对……

分享到:
评论

相关推荐

    Handbook of industrial mixing - science and practice

    《Handbook of Industrial Mixing - Science and Practice》(工业混合手册:科学与实践)是一本由Edward L. Paul、Victor A. Atiemo-Obeng和Suzanne M. Kresta共同编辑的专业书籍。本书旨在为读者提供工业混合领域...

    USACO2001-2007历年月赛测试数据+题目+题解打包全

    资源包包括USACO 2001-2007年月赛的测试数据;usaco月赛十年题典(2000-2009),usaco月赛2002-2008题解。单独下载需资源分30分以上。为了方便编程爱好者,我这边统一下载打包。欢迎下载。

    Markov chain and Mixing time second edition

    Since the publication of the first edition, the field of mixing times has continued to enjoy rapid expansion. In particular, many of the open problems posed in the first edition have been solved. The ...

    Mixing patterns in network

    Newman撰写的一篇经典论文,该文深入探讨了网络中顶点(节点)之间的连接模式,特别是它们倾向于与其他具有相似或不相似特征的顶点相连接的现象,即所谓的“同质性混合”(assortative mixing)。Newman在这篇文章...

    Video Mixing Renderer(VMR)技术

    ### Video Mixing Renderer(VMR)技术深度解析 #### 引言 Video Mixing Renderer(VMR),即视频混合渲染器,是DirectShow框架中用于处理和渲染视频数据的关键组件。VMR设计之初旨在解决多视频流的混合显示问题,...

    Survey Image Mixing and Deleting for Data Augmentation.zip

    本压缩包文件“Survey Image Mixing and Deleting for Data Augmentation.zip”包含了一份详细探讨这一主题的PDF报告,主要关注了两种数据增强策略:图像混合(Image Mixing)和图像删除(Image Deleting)。...

    Rocky DEM Workshop 16-Mixing Tank Part B_Rocky Coupling Setup

    在本工作坊“Rocky DEM Workshop 16-Mixing Tank Part B_Rocky Coupling Setup”中,我们将深入探讨如何使用Rocky软件与Ansys Fluent进行两相流的耦合模拟,特别是在混合罐(Mixing Tank)情境下,固体颗粒(如粒子...

    USACO官网93题fps格式 OJ题库

    USACO 官网第一到 五章 练习题中文语言官方数据 ...10 [1.3] 混合牛奶 Mixing Milk 11 [1.3] 修理牛棚 Barn Repair 12 [1.3] 牛式 Prime Cryptarithm 13 [1.3] 虫洞 wormhole 14 [1.3] 滑雪课程设计Ski Course Design

    Super-Resolution With Sparse Mixing Estimators

    本文讨论了一种基于稀疏混合估计器(Sparse Mixing Estimators,SME)的图像超分辨重建方法。该方法通过对不同先验知识的线性估计器进行自适应混合,以最小化每个系数块中的l0范数为目标,从而得到稀疏的混合权重。...

    Four-Wave Mixing in an Optical Fiber in the Zero-Dispersion Wavelength Region

    四波混频(Fiber Four-Wave Mixing, FWM)是光纤非线性效应之一,这种现象指的是当三束不同波长的光在同一光纤中传播时,会产生第四束波长的光。该现象不仅可能导致多信道传输系统的性能下降,而且还能用于新频率的...

    HOJ_1003_Java

    【压缩包子文件的文件名称列表】"1003_A_Mixing Milk"表示压缩包内有一个名为"1003_A_Mixing Milk"的文件,这个文件很可能包含了题目1003的具体实现。"Mixing Milk"可能是题目描述的一部分,提示我们这个问题可能...

    Fat-Drum-Mixing

    ### Fat-Drum-Mixing:鼓混音教程详解 #### 标题解析与扩展 “Fat-Drum-Mixing”这一标题直接指出了本文的主题是关于如何通过混音技术来让鼓声更加饱满、有力。在音乐制作中,“fat”通常用来形容声音的厚度与饱和...

    Survey Image Mixing and Deleting for Data Augmentation.pdf

    数据增强在深度神经网络性能提升方面扮演了重要角色。该技术通过各种手段,如dropout、正则化和图像增强,防止过拟合并提高模型的泛化能力。图像混合和删除是数据增强的一个子领域,它包括两种主要操作:一是将两张...

    The Generalization Performance of ERM Algorithm with Strongly Mixing Observations

    混合样本ERM算法的推广性能,邹斌,李落清,The generalization performance is the main purpose of machine learning theoretical research. The previous main bounds describing the generalization ability of ...

    Phai_nonlinear.zip_FOUR WAVE_Four-wave mixing_fiber_four wave mi

    Four wave mixing propagation in fiber

    Rocky DEM Workshop 16-Mixing Tank Part A_Fluent Case Setup.pdf

    水中的搅拌罐问题,我们将探讨如何使用Ansys Fluent设置多相流CFD(计算流体动力学)案例,为后续与RockyDEM的双向耦合模拟做准备。本工作坊分为两部分,第一部分(Part A)专注于Fluent案例的配置。...

    第1章总结1

    1.3节围绕贪心算法展开,"Mixing Milk"和"Barn Repair"要求运用贪心策略解决问题。"Prime Cryptarithm"结合了枚举和构造法,而"Calf Flac"则再次运用枚举。 1.4节探讨更多搜索技巧,如"Packing Rectangles"需要完全...

    msm6295pdf 4-CHANNEL MIXING ADPCM VOICE SYNTEHSIS LSI

    msm6295资料4-CHANNEL MIXING ADPCM VOICE SYNTEHSIS LSI

    Markov Chains and Mixing Times

    ### 马尔科夫链与混合时间 #### 前言 《马尔科夫链与混合时间》是一本2010年出版的研究生级别教材,由David A. Levin、Yuval Peres和Elizabeth L....本书主要介绍了马尔科夫链的基本理论及其在不同领域的应用,特别...

Global site tag (gtag.js) - Google Analytics