会场安排问题

一.贪心算法1

问题描述

假设要在足够多的会场里安排一批活动,并希望使用尽可能少的会场.设计一个有效的贪心算法进行安排(这个问题实际上是著名的图着色问题,若将每一个活动作为图的一个顶点,不相容活动间用边相连,使相邻顶点着有不同颜色的最小着色数,相应于要找的最小会场数) .

算法思想

把所有活动的开始时间和结束时间按从小到大的顺序排列,依次遍历,当遇到开始时间时,count++,当遇到结束时间时,count–,最后,count的最大值就是安排这些活动所需要的最小会场数.
ps(说一下我的理解):当遇到开始时间时,为其分配一个会场举行活动,因为开始的时间一定比结束的时间早,当我遇到一个结束时间时,说明这个活动已经结束,这个会场现在已经空闲,count–,我可以用这个会场去安排别的活动,所以,count的最大值就是我所需的安排这些所有活动所需要的最小的会场数.
代码如下:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Time
{
    int t;   //活动开始的时间或结束的时间
    bool f;   //标记这个活动是开始时间还是结束时间
};
int greedy(vector<Time> x)
{
    int max=0,cur=0,n=x.size();
    sort(x.begin(),x.end(),[](const Time &a,const  Time &b){return a.t < b.t;});  //混排
    for(int i=0;i<n-1;i++)
    {
        if(x[i].f) 
            cur++;
        else
            cur--;
        /*处理x[i]=x[i+1]的情况,当cur>max且x[i]=x[i+1]时,i时间肯定是开始时间,i+1时间可能是开始时间,也可能是结束时间,如果是结束时间,说明某活动开始时间和结束时间相同,不需要会场,故不对max更新,如果是开始时间,那在i+1更新max效果一样,这样就避免了开始时间和结束时间相同时,仍然为其分配了会场. */
        if(x[i].t<x[i+1].t && cur>max){ 
            max=cur;
        }
    }
    return max;     
}
int main()
{
    vector<Time> x;
    int n,i;
    Time temp;
    while(cin>>n,n)
    {
        for(i=0;i<n;i++)
        {
            temp.f=true;
            cin>>temp.t;
            x.push_back(temp);
            temp.f=false;
            cin>>temp.t;
            x.push_back(temp);
        }
        cout<<greedy(x)<<endl;
        x.clear();
    }
    return 0;
}

二.贪心算法2

问题描述如上.
* 首先,选择一个会场,遍历所有的活动,把能放在一个会场的活动放在一个会场里(在一个会场里安排尽可能多的活动).
* .重新选择一个会场,遍历所有没有被安排的活动,把相容的活动放在这个会场中.
* 重复第二步,直到所有的活动都被安排.

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include<vector>
//在一个会场里安排尽可能多的活动
using namespace std;
struct Activity_Info
{
    int s;  //开始时间
    int e;  //结束时间
    bool f;  //标记是否被添加过
};
int ArrangingActivities(vector<Activity_Info> acts)
{
    //按结束时间从小到大排序
    sort(acts.begin(),acts.end(),[](const Activity_Info &a,const Activity_Info &b){return a.e < b.e;});

    int count = 0;
    int currTime = -1;  //当前时间
    int i,flag=acts.size(),t=0;
    while(flag){
        count=0;
        currTime=-1;
    for (i = 0; i < acts.size(); i++){
        if (acts[i].s > currTime && acts[i].f)
        {
            count++;
            currTime = acts[i].e;
            acts[i].f=false;
            flag--;
        }
    }
        t++;
    }
    return t;
}

int main(void)
{
    int ncases;
    cin >> ncases;
    vector<Activity_Info> acts;
    int  i,n;
    Activity_Info temp;
    while (ncases-- != 0)
    {
        cin >> n;
        for (i = 0; i < n; i++)
        {
            cin >> temp.s >> temp.e;
            temp.f=true;
            acts.push_back(temp);
        }
        cout << ArrangingActivities(acts)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值