[Usaco 07 Dec] Gourmet Grazers

Description

Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his \(N (1 \leq N \leq 100,000)\) cows.

Each cow \(i\) demands grass of price at least \(A_i (1 \leq A_i \leq 1,000,000,000)\) and with a greenness score at least \(B_i (1 \leq B_i \leq 1,000,000,000)\). The GGG store has \(M (1 \leq M \leq 100,000)\) different types of grass available, each with a price \(C_i (1 \leq C_i \leq 1,000,000,000)\) and a greenness score of \(D_i (1 \leq D_i \leq 1,000,000,000)\). Of course, no cow would sacrifice her individuality, so no two cows can have the same kind of grass.

Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as is necessary.

Please output the minimum cost to satisfy all the cows. If that is not possible, output -1.

Solution

Preface

This is the first article I have ever written, because I'd like to practice my English. If you cannot read it clearly, please use your translation machine to translate this article. I'm very sorry for making so many difficulties to you. Thank you for your toleration.

Steps

When you see this problem, you might have no idea how to deal with it. But don't worry, just follow your sense. Remember our purpose, 'as little money as is necessary'!

Come back to the problem. How can we solve the problem and which way can we approach? Think about this idea: we find that the greenness score only need at least \(B_i\) and you must have found the more greenness scores a cow needs the less grass they can choose, aren't you? It is the point that we need!

According to that important point, One thing we can do is to sort the greenness score and use the minimum money to buy grass. It is important to concentrate on another thing that you should buy grass from the biggest greenness score they need to the minimum one. Because the minimum one will be chosen immediately, if you don't follow this sequence, those who need more greenness score will only have more expensive grass to choose, only to make you cost more.

For those reasons, we should sort at first, and choose the grass that needs the least money from bigger greenness score the cows need to smaller one.

Now, I'd also like to talk some details to you. Pay attention to the function, sort. You should change its way to sort from bigger one to smaller one. You can use a tip like adding negative numbers to change it. What's more, you also should use 'multiset' by including 'set' file to simplify your program.

Easily, the code shows up:

Code

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

pair<int, int> a[100010], b[100010];
priority_queue<int> q;
multiset<int> s;
long long ans;

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for(register int i = 0; i < n; i += 1)
	{
		scanf("%d%d", &a[i].second, &a[i].first);
		a[i].first = -a[i].first;//Negative numbers can change the way to sort
	}
	for(register int i = 0; i < m; i += 1)
	{
		scanf("%d%d", &b[i].second, &b[i].first);
		b[i].first = -b[i].first;
	}
	sort(a, a + n);
	sort(b, b + m);
    //Don't forget to sort greenness scores of grass
    //We need to sort it for the later use
	int p = 0;
	for(register int i = 0; i < n; i += 1)
	{
		while(p < m && b[p].first <= a[i].first)
		{
			s.insert(b[p++].second);
		}
		multiset<int>::iterator it = s.lower_bound(a[i].second);
		if(it == s.end())
		{
			ans = -1;//Can't satisfy all cows
			break;
		}
		else
		{
			ans += *it;
			s.erase(it);
		}
        //We don't need to empty the set
        //Because later cows' greenness scores is smaller than this one
	}
	printf("%lld", ans);
	return 0;
}

Write in the Last

  • Thanks for the free video from 51Nod.
  • Thanks for the video author, Bi Ke. It is he who made that free video elaborately for us.
  • Thanks for correcting mistakes by my schoolmates.
  • This article is written by myself. Reproduction in whole or part without written permission is prohibited.
posted @ 2018-09-19 20:52  孤独·粲泽  阅读(224)  评论(0编辑  收藏  举报