码迷,mamicode.com
首页 > 其他好文 > 详细

USACO milk

时间:2014-05-13 11:56:20      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   c   int   2014   

/*
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

标签:blog   class   code   c   int   2014   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!