天天看點

貪心算法-會場安排

描述:假設有足夠多的會場供n個活動使用,找出安排完所有的活動後的最少使用會場數。

分析:對所有活動的開始時間,結束時間進行排序,依次周遊每一個時間,若為開始時間,則count++,結束時間count–;在這期間,count達到的最大值,即為活動所需的最小會場數。

代碼:

package tanxinsuanfa;

public class huichanger {
    public static void main(String[] args) {
        int []start={,,,,};
        int []f={,,,,};
        activity1 a[]=new activity1[start.length+f.length];
        for(int i=;i<start.length;i++){
            a[i]=new activity1();
            a[i].time=start[i];
            a[i].type=true;

        }
        for(int i=start.length;i<a.length;i++){
            a[i]=new activity1();
            a[i].time=f[i-start.length];
            a[i].type=false;
        }
        activity1 t=new activity1();
        for(int i=;i<a.length;i++)
            for(int j=i+;j<a.length;j++){
                if(a[i].time>a[j].time){
                t=a[i];
                a[i]=a[j];
                a[j]=t;}
            }
        int count=;
        int max=;
        for(int i=;i<a.length;i++)
            if(a[i].type==true){
                count++;
                if(count > max)
                    max = count;
            }
            else
                count--;
        System.out.println(max);

}
    }
class activity1{
    public int time;
    boolean type;//true:start false:over
    public activity1(){

    }

}
           

繼續閱讀