当前位置 : 主页 > 编程语言 > c语言 >

POJ - 3614 贪心

来源:互联网 收集:自由互联 发布时间:2023-09-06
To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they’re at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000

To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they’re at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn’t tan at all…

The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.

What is the maximum number of cows that can protect themselves while tanning given the available lotions?

Input

  • Line 1: Two space-separated integers: C and L
  • Lines 2…C+1: Line i describes cow i’s lotion requires with two integers: minSPFi and maxSPFi
  • Lines C+2…C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri

Output
A single line with an integer that is the maximum number of cows that can be protected while tanning

Sample Input
3 2
3 10
2 5
1 5
6 2
4 1
Sample Output
2

思路:将每个奶牛的minf按降序排序,然后在合法的情况下选择最大spf的防晒霜,
由贪心可知正确性显然

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 600005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-6
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
	ll ans = 1;
	a = a % mod;
	while (b > 0) {
		if (b % 2)ans = ans * a;
		b = b / 2;
		a = a * a;
	}
	return ans;
}

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a%b);
}

struct Cow {
	int minf, maxf;
}cow[maxn];

bool cmp(Cow a, Cow b) {
	return a.minf > b.minf;
}

int spf[maxn], cover[maxn];

int main()
{
	ios::sync_with_stdio(false);
	int c, l;
	cin >> c >> l;
	int i, j;
	for (i = 1; i <= c; i++) {
		cin >> cow[i].minf >> cow[i].maxf;
	}
	sort(cow + 1, cow + 1 + c, cmp);
	int tot = 0;
	for (i = 1; i <= l; i++) {
		cin >> spf[i] >> cover[i];
	}
	int pos;
	for (i = 1; i <= c; i++) {
		int maxx = -1;
		pos = -1;
		for (j = 1; j <= l; j++) {
			if (maxx < spf[j]) {
				if (spf[j] >= cow[i].minf&&spf[j] <= cow[i].maxf&&cover[j] >= 1) {
					maxx = spf[j];
					pos = j;
				}
			}
		}
		if (pos != -1) {
			cover[pos]--;
			tot++;
		}
	}
	cout << tot << endl;
}
上一篇:[Leetcode]返回链表开始入环的第一个节点
下一篇:没有了
网友评论