思路:
贪心,对于某个防晒霜,把它分给它能满足的所有的牛中要求最严格的一个(若把所有能满足的区间按照左端点从小到大排序的话,则分给右端点最小(区间最短)的一个)。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <functional> 6 using namespace std; 7 8 struct cow 9 { 10 int minn, maxn; 11 }; 12 13 struct lotion 14 { 15 int s, c; 16 }; 17 18 cow a[2505]; 19 lotion t[2505]; 20 int C, L; 21 22 bool cmp(const cow & c1, const cow & c2) 23 { 24 if (c1.minn != c2.minn) 25 { 26 return c1.minn < c2.minn; 27 } 28 return c1.maxn < c2.maxn; 29 } 30 31 bool cmp1(const lotion & l1, const lotion & l2) 32 { 33 return l1.s < l2.s; 34 } 35 36 int main() 37 { 38 cin >> C >> L; 39 priority_queue<int, vector<int>, greater<int> > q; 40 for (int i = 0; i < C; i++) 41 { 42 cin >> a[i].minn >> a[i].maxn; 43 } 44 for (int i = 0; i < L; i++) 45 { 46 cin >> t[i].s >> t[i].c; 47 } 48 sort(a, a + C, cmp); 49 sort(t, t + L, cmp1); 50 int cnt = 0, cur = 0; 51 for (int i = 0; i < L; i++) 52 { 53 while (cur < C && a[cur].minn <= t[i].s) 54 { 55 q.push(a[cur].maxn); 56 cur++; 57 } 58 while (!q.empty() && t[i].c > 0) 59 { 60 int tmp = q.top(); 61 q.pop(); 62 if (t[i].s <= tmp) 63 { 64 cnt++; 65 t[i].c--; 66 } 67 } 68 } 69 cout << cnt << endl; 70 return 0; 71 }