POJ 3614 (貪心)

原題連接node

思路:把牛的最小值升序排序,防曬霜的防曬強度升序排序。若是牛最小承受小於防曬霜的,讓牛的最大防曬進隊。而後判斷隊列裏面牛的最大承受大於防曬霜,就答案加1spa

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 1e5+10;
int n, m;
struct node{
    int a, b;
    bool operator < (const node & a1) const{
        return a < a1.a;
    }
}cow[N], bot[N];
int main(){
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int ans  = 0;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++){
        scanf("%d%d", &cow[i].a, &cow[i].b);
    }
    for(int i = 1; i <= m; i++){
        scanf("%d%d", &bot[i].a, &bot[i].b);
    }
    sort(cow + 1, cow + 1 + n);
    sort(bot + 1, bot + 1 + m);
    priority_queue<int, vector<int>, greater<int> > q;
    for(int i = 1, j = 1; i <= m; i++){
        while(j <= n && cow[j].a <= bot[i].a){
            q.push(cow[j].b);
            j++;
        }
        while(!q.empty() && bot[i].b){
            int t = q.top();
            q.pop();
            if(t < bot[i].a) continue;
            ans++;
            bot[i].b--;
        }
    }
    printf("%d\n", ans);
    return 0;
}
相關文章
相關標籤/搜索