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 SPFrating 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
Sample Input
3 2 3 10 2 5 1 5 6 2 4 1
Sample Output
2
代码:
#include<iostream> //cin和cout函数
#include<queue> //优先队列
#include<algorithm> //sort函数
#include<cstdio> //printf和scanf函数
using namespace std;
struct node { int min,max; //定义结构体
bool operator<(const node &oth)const //声明优先队列排序方法
{
return min < oth.min; //每次输出最大元素,针对结构体中的min排序
}
} cow[2500];
struct nod1{ int spf,num;}lot[2500]; //结构体
bool cmp(nod1 x,nod1 y){ //排序声明函数
return x.spf>y.spf; //从大到小
}
int main(){
int m,n,i,j,ans=0,min[2500],max[2500],spf[2500],num[2500];
priority_queue < node > dui; //定义优先队列,node为排序方法
cin>>m>>n;
for(i=0;i<m;i++){
cin>>cow[i].min>>cow[i].max;
node tmp; //定义结构体,以用来向优先队列中加入一组元素
tmp.min=cow[i].min; //获得数值
tmp.max=cow[i].max;
dui.push(tmp); //放入
}
for(i=0;i<n;i++)
cin>>lot[i].spf >>lot[i].num;
sort(lot,lot+n,cmp); //排序
for(i=0;i<m;i++){
node c= dui.top(); //定义结构体,用来取出数据
for(j=0;j<n;j++){
if(lot[j].spf>=c.min&&lot[j].spf<=c.max&&lot[j].num!=0){
ans++;
lot[j].num--;
break;
}
}
dui.pop(); //删除数据
}
printf("%d",ans);
}
先对所有的奶牛按照最低级从高到低进行优先排序,对
防晒霜按照级别从高到低排序,然后取出第一头奶牛,
用循环在防晒霜中找到最先合适的一瓶,直到循环完所
有奶牛,此题本质还是贪心,只是用到了优先队列的知
识而已,并没有完全发挥其本应有的功能