POJ3416(贪心,优先队列,优先级处理)

Sunscreen
Time Limit: 1000MS

Memory Limit: 65536K
Total Submissions: 11169

Accepted: 3912
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
该题主要应用了贪心,和优先队列,本人是个小菜鸟,第一次接触更改优先级的题目,sort()方法用于排序,但是结构体排序需要重新规定优先级,需要自己写一个返回值为bool型的方法,详见代码。

//贪心求解
//防晒因子需求越少的奶牛对于防晒液来说限制就越强
//先试试限制性强的,再试试限制性弱的。
//这样对于我们来说情况就达到了最优
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#define MAXN 2505
using namespace std;



struct sp{
    int num;
    int many;
}spf[MAXN];

struct nod{
    int maxspf;
    int minspf;
}node[MAXN];
int n,m;
priority_queue<int,vector<int>,greater<int> >que;

//重新规定nod结构体的优先级,若两个结构体
//的minspf相同,maxspf小的优先
//级要高,优先级高的在前面,minspf小的在前面。
//下同
bool cmp(nod a,nod b){
    if(a.minspf == b.minspf){
        return a.maxspf<b.maxspf;
    }
    return a.minspf<b.minspf;
}
bool cmp2(sp a,sp b){
    return a.num<b.num;
}


int main()
{
    while(~scanf("%d%d",&n,&m)){
        for(int i=0;i<n;i++){
            scanf("%d%d",&node[i].minspf,&node[i].maxspf);
        }
        for(int i=0;i<m;i++){
            scanf("%d%d",&spf[i].num,&spf[i].many);
        }

        sort(node,node+n,cmp);
        sort(spf,spf+m,cmp2);
        int summ=0;
        int j = 0;
        for(int i=0;i<m;i++){
            for(;j<n;){
                if(node[j].minspf>spf[i].num)
                    break;
                que.push(node[j].maxspf);
                j++;
            }
            while(!que.empty()&&spf[i].many!=0){
                int tmp = que.top();
                que.pop();
                if(tmp>=spf[i].num){
                    spf[i].many--;
                    summ++;
                }
            }
        }
        printf("%d\n",summ);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40488730/article/details/81560128