USACO 1.3 Mixing Milk

直接排序贪心,没啥好说的。 可以桶排优化,但是省事直接SORT了


/*
TASK:milk
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int max_m = 5000;
pair<int, int>a[max_m];
int n,ans(0), m;
int main()
{
	freopen("milk.in", "r", stdin);
	freopen("milk.out", "w", stdout);
	std::ios::sync_with_stdio(false);
	cin >> n >> m;	
#define f a[i].first
#define s a[i].second
	for (int i = 0; i != m; ++ i)	cin>> f >> s;
	sort(a, a + m);
	for (int i = 0; n != 0; ++ i)
	{
		if (s < n)
		{
			n -= s;
			ans += f * s;	
		}else
			{
				ans += n * f;
				n = 0;
			}	
	}
	cout<<ans<<endl;
	return 0;
}


你可能感兴趣的:(USACO 1.3 Mixing Milk)