POJ 3614

是用贪心和优先队列, 十分灵巧的进行求解 : 

  这个贪心是十分的巧妙啊

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;

typedef pair<int, int> P;
int C, L;
const int MAX_C = 2500;
const int MAX_L = 2500;
P a[MAX_L];         //存储防晒霜
P b[MAX_C];         //用来进行存储牛
//qsort 排序函数
int cmp(const void *a, const void *b){
    P *aa = (P *)a;
    P *bb = (P *)b;
    return aa->first > bb->first ? 1 : -1;      //SPF 是排序的首选项
}

int main()
{
    cin>>C>>L;
    P temp;
    priority_queue<int, vector<int>, greater<int> > pque;             //注意是由大到小出现的
    for(int i = 0; i < C; i++){
        scanf("%d%d", &b[i].first, &b[i].second);
    }
    for(int i = 0; i < L; i++){
        scanf("%d%d", &a[i].first, &a[i].second);
    }
    //升序排序, 完成对优先队列的存储
    qsort(a, L, sizeof(a[0]), cmp);
    qsort(b, C, sizeof(b[0]), cmp);
   /* printf("SHOW THE BOTTLE!\n");
    for(int i = 0; i < L; i++)  printf("%d %d\n", a[i].first, a[i].second);   puts("");
    printf("SHOW THE COW : \n");
    for(int i = 0; i < C; i++){
        printf("%d  %d\n", b[i].first, b[i].second);
    }*/
    int res = 0;    int j = 0;
    //一定要注意 a 霜 使用 i  ; b 牛, 使用 j
    for(int i = 0; i < L; i++){     //对防晒霜的数组进行遍历
        while(j < C && a[i].first >= b[j].first){
            pque.push(b[j].second); //将 牛 的最大值, 放在优先队列, 因为他的最小值(区间头部)肯定是满足了, 我们这里关键是看他的最大值(区间尾部)
            j++;
        }
        while(!pque.empty() && a[i].second){    //队列不空, 防晒霜没用完
            int x = pque.top(); pque.pop(); //优先队列当中取出
            if(x < a[i].first)  continue;   //头满足, 但是区间尾部不满足
            res++;  a[i].second--;  //进行答案以及防晒霜个数的更新
        }
    }
    printf("%d\n", res);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lucky-light/p/11431504.html
今日推荐