会场安排问题(+排序小结)

会场安排问题(最后有排序小结)

时间限制:3000 ms  |  内存限制:65535 KB
难度:3

描述

    学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办。小刘的工作就是安排学校小礼堂的活动,每个时间最多安排一个活动。现在小刘有一些活动计划的时间表,他想尽可能的安排更多的活动,请问他该如何安排。

输入

    第一行是一个整型数m(m<100)表示共有m组测试数据。
    每组测试数据的第一行是一个整数n(1<n<10000)表示该测试数据共有n个活动。
    随后的n行,每行有两个正整数Bi,Ei(0<=Bi,Ei<10000),分别表示第i个活动的起始与结束时间(Bi<=Ei)

输出

    对于每一组输入,输出最多能够安排的活动数量。
    每组的输出占一行

样例输入

    2
    2
    1 10
    10 11
    3
    1 10
    10 11
    11 20

样例输出

    1
    2

提示

注意:如果上一个活动在t时间结束,下一个活动最早应该在t+1时间开始

思路

采用贪心的思想(该方法并不是从整体最优方面来考虑问题,而是从某种意义上的局部最优角度做出选择;选择的过程中,可以依赖之前的操作,但是不影响将来所做的选择)。

每次选取会议时都选择使得剩下时间最长的会议,这样可以保证会议数最多。也就是按结束时间从早到晚排序。


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Plan {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		
		int t = cin.nextInt();
		while(t > 0) {
			t--;
			int n = cin.nextInt();
			Demo[] d = new Demo[n+5];
			for(int i=0; i<n; i++) {
				int s = cin.nextInt();
				int e = cin.nextInt();
				d[i] = new Demo(s, e);
			}
			Comparator<Demo> cmp = new Comparator<Demo>() {
				@Override
				public int compare(Demo o1, Demo o2) {
					if(o1.e == o2.e) {
						if(o1.s == o2.s) 
							return 0;
						if(o1.s > o2.s)
							return 1;
						return -1;
					}else {
						if(o1.e > o2.e)
							return 1;
						return -1;
					}
				}
			};
			Arrays.sort(d, 0, n, cmp);
			
			int cnt = 1;
			int end = d[0].e;
			for(int i=1; i<n; i++) {
				if(d[i].s > end) {
					end = d[i].e;
					cnt ++;
				}
			}
			System.out.println(cnt);
		}
	}
	
	static class Demo {
		int s,e;
		public Demo(int s, int e) {
			this.s = s;
			this.e = e;
		}
	}
}

对于自定义类排序

类内排序采用实现 Comparable 接口的方式(重写 compareTo 方法)

规则:

与本(或第一个)对象相比,若本(或第一个)对象大于另一个对象则返回1,小于则返回-1,相等则返回0;

按此规则排序是升序排序,调换返回值则反向排序。当返回0时按原相对位置排序

import java.util.Arrays;
public class Solution {
    static class P implements Comparable<P>{
    	int n1;
    	int n2;
    	public P(int n1,int n2) {
    		this.n1 = n1;
    		this.n2 = n2;
    	}
    	
    	@Override
    	public int compareTo(P p) {//按 n1 升序、n2 降序排序
    		if(this.n1 == p.n1) {
    			if(this.n2 > p.n2)
    				return -1;
    			if(this.n2 < p.n2)
    				return 1;
    			return 0;
    		}else {
    			if(this.n1 > p.n1)
    				return 1;
    			return -1;
    		}
    	}
    	
    	@Override
    	public String toString() {
    		return this.n1+"  "+this.n2;
    	}
    }
	
    public static void main(String[] args) {
		P[] p = new P[45];
		p[0] = new P(1, 6);
		p[1] = new P(4, 5);
		p[2] = new P(7, 1);
		p[3] = new P(2, 4);
		p[4] = new P(4, 6);
		p[5] = new P(7, 2);
		p[6] = new P(1, 6);
		
		Arrays.sort(p, 0, 7);
		for(int i=0; i<7; i++) {
			System.out.println(p[i]);
		}
		/*
		 * 排序结果:
		 	1  6
			1  6
			2  4
			4  6
			4  5
			7  2
			7  1
		 
		*/
	}
}

类外排序采用定义 Comparator 类型的对象(重写 compare 方法);

import java.util.Arrays;
import java.util.Comparator;
public class Solution {

    static class P {
    	int n1;
    	int n2;
    	public P(int n1,int n2) {
    		this.n1 = n1;
    		this.n2 = n2;
    	}
    	
    	@Override
    	public String toString() {
    		return this.n1+"  "+this.n2;
    	}
    }
	
    public static void main(String[] args) {
    	        P[] p = new P[45];
		p[0] = new P(1, 6);
		p[1] = new P(4, 5);
		p[2] = new P(7, 1);
		p[3] = new P(2, 4);
		p[4] = new P(4, 6);
		p[5] = new P(7, 2);
		p[6] = new P(1, 6);
		
		Comparator<P> cmp = new Comparator<P>() {
			@Override
			public int compare(P p1, P p2) {//按 n1 升序、n2 降序排序
				if(p1.n1 == p2.n1) {
	    			if(p1.n2 > p2.n2)
	    				return -1;
	    			if(p1.n2 < p2.n2)
	    				return 1;
	    			return 0;
	    		}else {
	    			if(p1.n1 > p2.n1)
	    				return 1;
	    			return -1;
	    		}
			}
		};
		
		Arrays.sort(p, 0, 7, cmp);
		for(int i=0; i<7; i++) {
			System.out.println(p[i]);
		}
		/*
		 * 排序结果:
		 	1  6
			1  6
			2  4
			4  6
			4  5
			7  2
			7  1
		 
		*/
	}
}

如有错误或不合理的地方,敬请指正~

加油!!

猜你喜欢

转载自blog.csdn.net/qq_37194492/article/details/89150418
今日推荐