Schedule

来源:互联网 发布:淘宝产品为什么下架了 编辑:程序博客网 时间:2024/05/18 01:31

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;}
原创粉丝点击