POJ 3622 Gourmet Grazers(set)

74 篇文章 0 订阅
39 篇文章 0 订阅

题目链接:点击打开链接

思路:

我们可以先排序, 先按照分数排序再按照价钱排序。  这样从前向后动态处理, 到了当前这头牛, 把商店里分数大于等于它需求的商品都加进去, 这样, 集合中所有的元素的分数都一定满足后面的, 所以分数这个因素就已经保证了。   然后set中按照价钱排序, 可以用二分快速找到价钱大于等于牛的需求的第一个产品。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1e5 + 10;
int T,n,m,x,y;
struct node {
    int x, y;
    node(int x=0, int y=0):x(x), y(y) {}
    bool operator < (const node& rhs) const {
        if(y != rhs.y) return y < rhs.y;
        else return x < rhs.x;
    }
}a[maxn], b[maxn];
multiset<int> g;
multiset<int> :: iterator it;
int main() {
    while(~scanf("%d%d",&n, &m)) {
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &a[i].x, &a[i].y);
        }
        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &b[i].x, &b[i].y);
        }
        sort(a+1, a+n+1);
        sort(b+1, b+m+1);
        bool ok = true;
        ll ans = 0;
        int cnt = m;
        for(int i = n; i >= 1; i--) {
            while(cnt >= 1 && b[cnt].y >= a[i].y) {
                g.insert(b[cnt--].x);
            }
            it = g.lower_bound(a[i].x);
            if(it == g.end()) {
                ok = false; break;
            }
            else {
                ans += *it; g.erase(it);
            }
        }
        if(ok) printf("%I64d\n", ans);
        else printf("-1\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值