Sunscreen POJ - 3614(貪心)
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
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
題意:有C頭牛,每個牛都有對陽光的承受空間【minspf,maxspf】,有L種防晒霜,每種都有SPF值和瓶數,塗到牛身上可使其只收到SPF值的陽光,每瓶只可塗一頭牛,問最多幾隻牛可以享受陽光?
思路:
此題可以看成在一個數軸上操作。
圖中矩形塊就代表了牛的承受區間,我們從左到右對牛進行塗防晒霜處理,可以從圖中看到,
①【0,1】位置,無牛需要防晒霜
②【1,4】位置,只有一頭牛有需求,那我們直接給橙色塗
④【4,5】位置,紅色牛和橙色牛都有需求,給哪隻牛塗呢?
有人可能說了,前面都給橙色牛塗了,現在肯定給紅色啦,對,但是如果在【4,5】之前沒有防晒霜呢(就一瓶防晒霜在【4,5】位置),也就是說這瓶防晒霜面臨兩個選擇,
我們從圖中看出,那肯定也是給紅色牛啦,因為紅色牛快承受不住啦(該牛承受區間右邊界小),也就是說再往下進行的話,馬上就出了紅牛的承受區間了,然而橙牛卻還沒有,
所以我們先給紅牛,之後再瞅瞅有沒有適合橙牛的。
這樣就可以看出我們的策略:
①將牛按右邊界從小到大排序,防晒霜按spf從小到大排序
②將牛按左邊界從大到小排序,防晒霜按spf從大到小排序
回頭看這問題,對於排好序的牛(假設按①方式),有兩瓶防晒霜x>y,那麼是選x的,因為x,y對於後面的牛來說有三種情況,
①x、y可選
②x不可選,y可選
③x、y不可選(由左邊界影響)
那麼我們選擇小的就對後面的牛來說影響最小
並且因為選擇了對後面牛最小的影響方式,該牛可以選擇防晒霜,就沒有必要留給下頭牛,因為只算牛頭數,哪頭牛不是牛呢
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 using namespace std; 5 6 int C,L; 7 8 struct Node 9 { 10 int l; 11 int r; 12 } cow[3000],sun[3000]; 13 14 bool cmp1(Node a,Node b) 15 { 16 return a.l < b.l; 17 } 18 bool cmp2(Node a,Node b) 19 { 20 return a.r < b.r; 21 } 22 23 int main() 24 { 25 scanf("%d%d",&C,&L); 26 for(int i=1; i<=C; i++) 27 { 28 scanf("%d%d",&cow[i].l,&cow[i].r); 29 } 30 for(int i=1; i<=L; i++) 31 { 32 scanf("%d%d",&sun[i].l,&sun[i].r); 33 } 34 sort(cow+1,cow+1+C,cmp2); 35 sort(sun+1,sun+1+L,cmp1); 36 int ans = 0; 37 for(int i=1; i<=C; i++) 38 { 39 for(int j=1; j<=L; j++) 40 { 41 if(cow[i].l <= sun[j].l && cow[i].r >= sun[j].l && sun[j].r) 42 { 43 ans++; 44 sun[j].r--; 45 break; 46 } 47 } 48 } 49 printf("%d\n",ans); 50 }View Code
優先佇列寫法:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int C,L; 5 struct Node 6 { 7 int l; 8 int r; 9 bool operator<(Node b) 10 { 11 return l < b.l; 12 } 13 } cow[3000],sun[3000]; 14 priority_queue<int,vector<int>,greater<int> >que; 15 int main() 16 { 17 scanf("%d%d",&C,&L); 18 for(int i=1; i<=C; i++) 19 scanf("%d%d",&cow[i].l,&cow[i].r); 20 for(int i=1; i<=L; i++) 21 scanf("%d%d",&sun[i].l,&sun[i].r); 22 sort(cow+1,cow+1+C); 23 sort(sun+1,sun+1+L); 24 int j = 1; 25 int ans = 0; 26 for(int i=1; i<=L; i++) 27 { 28 while(j<=C&&cow[j].l <= sun[i].l) 29 que.push(cow[j].r),j++; 30 while(!que.empty() && sun[i].r) 31 { 32 int tmp = que.top(); 33 que.pop(); 34 if(tmp >= sun[i].l) 35 ans++,sun[i].r--; 36 } 37 } 38 printf("%d\n",ans); 39 }View Code