hdu6180Schedule(第十场贪心)

Schedule

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 878    Accepted Submission(s): 339


Problem Description
There are N schedules, the i-th schedule has start time  si and end time  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  timeend and  timestart , where time_{end} is time to turn off the machine and  timestart is time to turn on the machine. We assume that the machine cannot be turned off between the  timestart and the  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.
 

Input
The 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  si and  ei  (0<=si<ei<=1e9).
 

Output
For 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
 

Source

给你n个计划,要求用最少的机器解决完计划。机器不能在重叠的计划内工作,求花费的总时间。

思路:将所有的计划按开始时间排序好,用upper_bound函数,如果队列中有大于新加入的开始时间时,更新,如果没有,继续加入新的结束时间

按例中:排好序为(1,3)(2,5)(4,6),新加入3入队,判断第二次的新加入为2,队首大于2所以直接加入5,再加入4,队列中第二个大于4,所以去掉队首3,更新为5,队尾更新为6,最后统计节点为5,6两个

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 1e5+7;
int n;
struct node
{
    int l,r;
} p[N];

int cmp(node a,node b)
{
    return a.l<b.r;
}

multiset<int>S;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        S.clear();
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&p[i].l,&p[i].r);
        }
        sort(p,p+n,cmp);
        LL ans=0;
        for(int i=0;i<n;i++)
        {
            multiset<int>:: iterator it=S.upper_bound(p[i].l);
            if(it==S.begin())
            {
                ans+=p[i].r-p[i].l;
                //cout<<ans<<endl;
                //cout<<p[i].r<<endl;
                S.insert(p[i].r);
            }
            else
            {
                it--;
                ans+=p[i].r-*it;
                 //cout<<ans<<endl;
                S.erase(*it);
                //cout<<p[i].r<<endl;
                S.insert(p[i].r);
            }
        }
        printf("%d %lld\n",S.size(),ans);
        /*for(multiset<int>:: iterator it=S.begin();it!=S.end();it++)
        {
            cout<<*it<<endl;
        }*/
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_32792879/article/details/77618725
今日推荐