牛客算法周周练3/POJ - 3614 E -Sunscreen(贪心,优先队列)
题目描述
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?
输入描述:
* 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
输出描述:
A single line with an integer that is the maximum number of cows that can be protected while tanning
示例1
输入
3 2 3 10 2 5 1 5 6 2 4 1
输出
2
链接:https://ac.nowcoder.com/acm/contest/5338/E
来源:牛客网
题意:
奶牛美容:有头奶牛日光浴,每头奶牛分别需要和单位强度之间的阳光。现有种防晒霜,分别能使阳光强度稳定为,其瓶数为。求最多满足多少头奶牛?
思路:
将奶牛按照为第一指标,为第二指标进行升序排序。
将防晒霜按照指标进行升序排序,
从枚举第 个防晒霜,将满足的奶牛的都加入到一个小根堆中,
然后对于堆顶的元素如果满足:就将一个第个防晒霜给这个奶牛使用。
核心思想:对于满足的奶牛,优先第个防晒霜给较小的使用,
因为较大的有较大的选择空间。
可以思考下面样例,方便理解:
2 2 1 6 4 5 5 1 6 1
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #include <sstream> #include <bitset> #include <unordered_map> // #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define du2(a,b) scanf("%d %d",&(a),&(b)) #define du1(a) scanf("%d",&(a)); using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;} ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;} void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;} inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;} void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}} void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}} const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ #define DEBUG_Switch 0 int n, m; pii a[maxn]; pii b[maxn]; bool cmp2(pii & aa, pii & bb) { return aa.fi < bb.fi; } priority_queue<int, vector<int>, greater<int> > heap; int main() { #if DEBUG_Switch freopen("C:\\code\\input.txt", "r", stdin); #endif //freopen("C:\\code\\output.txt","r",stdin); n = readint(); m = readint(); repd(i, 1, n) { a[i].fi = readint(); a[i].se = readint(); } sort(a + 1, a + 1 + n); repd(i, 1, m) { b[i].fi = readint(); b[i].se = readint(); } sort(b + 1, b + 1 + m, cmp2); int id = 1; int ans = 0; repd(i, 1, m) { while (id <= n && a[id].fi <= b[i].fi) { heap.push(a[id].se); id++; } while (!heap.empty() && b[i].se > 0) { int x = heap.top(); heap.pop(); if (x >= b[i].fi) { b[i].se--; ans++; } else { continue; } } } printf("%d\n", ans ); return 0; }