HDU 6180 Schedule

来源:互联网 发布:ci 数据库配置 编辑:程序博客网 时间:2024/06/05 09:21

题目地址
题意:有m个工程,一台机器在同一时间只能运行一个工程,告诉你每个工程的起始时间和结束时间,求出最少要多少个机器以及最小的机器总运行时间(机器开始了就不能停了,知道用完该台机器才停止)。
思路:先按起始时间排序,再用两个优先队列去维护(一个大的优先级更高用于空闲序列,一个小的优先级更高用于运行序列),然后直接模拟就好了,每次都先把运行序列中比当前开始时间小的弹出到空闲序列,再从空闲序列中找到一个时间最晚的加入运行序列(因为要总运行时间最小)。

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#include <functional>#define N 100010#define LL __int64#define inf 0x3f3f3f3f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const LL mod = 1e9 + 7;struct node {    LL st, en;}thing[N];bool cmp1(node a, node b) {    if (a.st == b.st) {        return a.en < b.en;    }    return a.st < b.st;}int main() {    cin.sync_with_stdio(false);    int T;    int n;    cin >> T;    while (T--) {        cin >> n;        priority_queue<LL>p;//空闲集合        priority_queue<LL, vector<LL>, greater<LL> >q;//工作集合        for (int i = 0; i < n; i++) {            cin >> thing[i].st >> thing[i].en;        }        LL ans = 0, sum = 0;        sort(thing, thing + n, cmp1);        for (int i = 0; i < n; i++) {            node now = thing[i];            while ((!q.empty()) && q.top() <= now.st) {                p.push(q.top());                q.pop();            }            if (!p.empty()) {                int num = p.top();                p.pop();                sum += now.en - num;                q.push(now.en);            }            else {                ans++;                sum += now.en - now.st;                q.push(now.en);            }        }        cout << ans << " " << sum << endl;    }    return 0;}