首页 > 其他 > 详细

USACO milk

时间:2014-05-13 11:55:51      阅读:432      评论:0      收藏:0      [点我收藏+]
/*
ID:kevin_s1
PROG:milk
LANG:C++
*/

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAXN 5001

using namespace std;

int N,M;
struct farmer{
	int Pi;
	int Ai;
}farmers[MAXN];

int cmp(farmer a,farmer b){
	return a.Pi < b.Pi;
}

int main(){
	freopen("milk.in","r",stdin);
	freopen("milk.out","w",stdout);
	cin>>N>>M;
	for(int i = 1; i <= M; i++){
		cin>>farmers[i].Pi>>farmers[i].Ai;
	}
	sort(farmers + 1, farmers + M + 1, cmp);
	int count = 0;
	int index = 1;
	int money = 0;
	while(count < N){
		if(count + farmers[index].Ai <= N){
			count = count + farmers[index].Ai;
			money += farmers[index].Pi * farmers[index].Ai;
			index++;
		}
		if(count + farmers[index].Ai > N){
			money += farmers[index].Pi * (N - count);
			count = N;
			index ++;
		}
	}
	cout<<money<<endl;

	return 0;
}


//greedy algorithm

USACO milk,布布扣,bubuko.com

USACO milk

原文:http://blog.csdn.net/kevin_samuel/article/details/25662447

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!