活动安排问题

题意:

假设有 n 个活动,每个活动的开始和结束时间分别为 si,ti,我们需要将它们安排到一些教室,任意活动都可以在任意教室进行,同一间教室的活动的时间不能重叠,求最少的教室数。

思路:

实际上我们并不关心每一间教室具体的情况,我们只关心,某个时刻一间教室被占用,某个时刻一间教室变为空,而这与活动的开始结束一一对应,我们只要从小到大遍历这些时刻,就能知道哪个时刻所需占用的教室数最多了。

#include <iostream>
#include <algorithm>
using namespace std;

struct actv{
    int t;
    bool flag;//开始为true,结束为false
};

int cmp(actv a,actv b){
    if(a.t<b.t)
        return 1;
    return 0;
}

int main(){
    int n;
    cin>>n;
    actv s[2*n];
    for(int i=0;i<2*n;i+=2){
        cin>>s[i].t>>s[i+1].t;
        s[i].flag=true;
        s[i+1].flag=false;
    }
    sort(s,s+2*n,cmp);
    int k=0;
    int ans=0;
    for(int i=0;i<2*n;++i){
        if(s[i].flag){
            ++k;
            ans=max(ans,k);
        }else {
            --k;
        }
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/Maxx-el/p/13417840.html