HDU

There are N schedules, the i-th schedule has start time i  si and end time i  ei (1 <= i <= N). There are some machines. Each two overlapping schedules cannot be performed in the same machine. For each machine the working time is defined as the difference between timend  timeend and timstart  timestart , where time_{end} is time to turn off the machine and timstart  timestart is time to turn on the machine. We assume that the machine cannot be turned off between the timstart  timestart and the timend  timeend .
Print the minimum number K of the machines for performing all schedules, and when only uses K machines, print the minimum sum of all working times.

InputThe first line contains an integer T (1 <= T <= 100), the number of test cases. Each case begins with a line containing one integer N (0 < N <= 100000). Each of the next N lines contains two integers i  si and i  ei (0<=<<=1e9(0<=si<ei<=1e9) .OutputFor each test case, print the minimum possible number of machines and the minimum sum of all working times.Sample Input

1
3
1 3
4 6
2 5

Sample Output

2 8

题意:有N个任务,每个任务有自己的起始时间和结束时间,问至少多少个机器可以完成这些任务,使其满足每台机器的任务没有时间交集。

思路:直接贪心,每次找完成时间<=当前任务起始时间的最大的一台机器,如果没有,则弄一台新的机器。

主要是要注意set的二分用s.lower_bound(x),优于lower_bound(s.begin(),s.end(),x);

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
multiset<int>s;
multiset<int>::iterator it;
pair<int,int>a[100010];
int main()
{
    int T,N,K; ll ans;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N); K=0; ans=0; s.clear();
        rep(i,1,N) scanf("%d%d",&a[i].first,&a[i].second);
        sort(a+1,a+N+1);
        rep(i,1,N){
            if(s.empty()||(*s.begin())>a[i].first) {
                s.insert(a[i].second); K++; ans-=a[i].first;
            }
            else{
                it=s.upper_bound(a[i].first);
                it--;
                s.erase(it); s.insert(a[i].second);
            }
        }
        for(it=s.begin();it!=s.end();it++) ans+=(*it);
        printf("%d %lld
",K,ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hua-dong/p/9810861.html