题目
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?
有C头奶牛日光浴,第i头奶牛需要minSPF[i]和maxSPF[i]单位强度之间的阳光。每头奶牛在日光浴之前必须涂防晒霜,防晒霜有L种,涂上第i种之后,身体接收到的阳光强度就会稳定为SPF[i],第i种防晒霜有cover[i]瓶。求最多可以满足多少头奶牛进行日光浴。C,L<=2500
题解
贪心
对minSPF递减排序,排序后对于每头奶牛,找一个它能用的SPF最大的防晒霜给它用。
因为排序后一头奶牛后面的奶牛minSPF一定不比他大,那么给当前奶牛用SPF大的防晒霜对整体问题的影响要比用SPF小的防晒霜要好
代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=2503;
int n,m,ans;
int s[1003];
struct node{
int a,b;
}f[N];
bool comp(node a,node b){
return a.a>b.a;
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d%d",&f[i].a,&f[i].b);
for (int i=1;i<=m;i++){
int c,w;
scanf("%d%d",&c,&w);
s[c]+=w;
}
sort(f+1,f+n+1,comp);
for (int i=1;i<=n;i++){
for (int j=f[i].b;j>=f[i].a;j--)
if (s[j]){
ans++,s[j]--;
break;
}
}
printf("%d",ans);
}