Description
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
样例解释:第一种防晒霜的固定阳光强度6可以用于第一头 [3,10] 的牛,答案加一,第一种防晒霜两瓶变成一瓶;
第二种防晒霜的固定阳光强度4可以用于第二头 [2,5] 的牛或者第三头 [1,5] 的牛,但是这种防晒霜只有一瓶,
所以答案加一,第二种防晒霜没了,结束,所以答案是2。
思路:看懂样例的话,理解题意应该就没问题了。这题应该想到用贪心的思想,对奶牛的阳光承受区间从小到大排序,
对防晒霜的固定阳光强度从小到大排序,然后用优先队列处理就好了......
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<map>
6 #include<set>
7 #include<vector>
8 #include<queue>
9 using namespace std;
10 #define ll long long
11 const int inf=1e9+7;
12 const int mod=1e9+7;
13
14 const int maxn=3e3+10;
15
16 bool cmp(const pair<int,int> &a,const pair<int,int> &b)
17 {
18 if(a.first != b.first)//优先排min_spfi
19 return a.first < b.first;
20 else if(a.first == b.first)//min_spfi相同排max_spfi
21 return a.second < b.second;
22 }
23
24 int main()
25 {
26 ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
27
28 priority_queue<int,vector<int>,greater<int> >q;//优先出队小的元素
29
30 /*q.push(1);q.push(5);q.push(3);//test
31
32 while(!q.empty())
33 {
34 cout<<q.top()<<endl;
35 q.pop();
36 }*/
37
38 pair<int,int> cow[maxn],you[maxn];//定义两个pair数组,比较方便
39
40 int c,l;
41 cin>>c>>l;
42
43 for(int i=0;i<c;i++)//输入每只奶牛的min_spfi和max_spfi
44 cin>>cow[i].first>>cow[i].second;
45
46 for(int i=0;i<l;i++)//输入每瓶油(防晒霜2333)的固定阳光强度和瓶数
47 cin>>you[i].first>>you[i].second;
48
49 sort(cow,cow+c,cmp);//排序
50
51 sort(you,you+l,cmp);//排序
52
53 //for(int i=0;i<c;i++)//test
54 // cout<<cow[i].first<<" "<<cow[i].second<<endl;
55
56 ll int ans=0;//记录答案
57
58 int now=0;
59
60 for(int i=0;i<l;i++)//对每瓶油对奶牛遍历
61 {
62 while(now<c&&cow[now].first<=you[i].first)//固定阳光强度大于奶牛的min_spfi
63 {
64 q.push(cow[now].second);//入队
65 now++;//找下一头奶牛
66 }
67
68 while(!q.empty())//队列不为空
69 {
70 if(you[i].second==0)//油没了直接break
71 break;
72
73 int x=q.top();//找出奶牛的max_spfi
74 q.pop();
75
76 if(x>=you[i].first)//固定阳光强度不超过牛的max_spfi
77 {
78 ans++;//成功
79 you[i].second--;//油减一瓶
80 }
81 else//否则跳过
82 continue;
83
84 }
85
86 }
87
88 cout<<ans<<endl;//输出答案
89
90 return 0;
91 }