Schedule

HDU - 6180
这里写图片描述

显然是先排序,然后按左端点从小到大放进去,每次在剩余的机子中找一个符合条件而且尽量大的。
但是一开始用 multiset 和 lowerbound 来进行处理会超时,就想到另一种写法。用一个优先队列和一个栈,对于当前任务,先将优先队列的所有合法情况按顺序压到栈中,直到不存在合法的机器,然后从栈找是否有满足条件的,如果没有就要增加机器

#include <bits/stdc++.h>
using namespace std;
int n;
typedef long long ll;
priority_queue<int, vector<int>, greater<int> > q;
stack<int> s;

const int ARS = 100010;
void readnum(int &x) {
    x = 0;
    char c;
    for(c=getchar();c!=EOF && !isdigit(c);c=getchar());
    for(;isdigit(c);c=getchar()) {
        x = 10*x + c-'0';
    }
}
struct term{
    int x, y;
    bool operator<(const term &rhs) const {
        return x < rhs.x;
    }
} a[ARS];

int main() {
    int _t;
    readnum(_t);
    while (_t--) {
        readnum(n);
        while (q.size()) q.pop();
        while (s.size()) s.pop();

        for (int i = 1; i <= n; i++) {
            readnum(a[i].x);
            readnum(a[i].y);
        }
        sort(a+1, a+n+1);
        ll bg = 0;
        ll ed = 0;
        int num = 0;

        for(int i = 1; i <= n; i++) {
            while(q.size() && q.top() <= a[i].x ) {
                s.push(q.top());
                q.pop();
            }
            if(s.empty()) {
                num++;
                q.push(a[i].y);
                bg += a[i].x;
            } else {
                s.pop();
                q.push(a[i].y);
            }
        }
        while(q.size()) {
            ed += q.top(); q.pop();
        }
        while(s.size()) {
            ed += s.top();
            s.pop();
        }

        printf("%d %lld\n", num, ed-bg);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值