zoukankan      html  css  js  c++  java
  • 区间调度问题(贪心)

    关于区间问题也可以看我的另一篇博客安排会议https://blog.csdn.net/qq_34115899/article/details/79731172


    /*区间调度问题
     * 有n项工作,每项工作分别在si时间开始,在ti时间结束。对于每项工作,你都可以选择参加与否。
     * 如果选择了参加,那么自始至终都必须全程参加。此外,参加工作的时间段不能重叠(即使是开始的瞬间
     * 和结束的瞬间的重叠也是不允许的)。
     * 你的目标是参与尽可能多的工作,那么最多能参与多少项工作呢?
     * 限制条件:
     * 1≤N≤100000
     * 1≤si≤ti≤10的9次方
     * */
    
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    import java.util.Vector;
    
    public class IntervalScheduling
    {
    	public static int[] E = new int[100000];
    	public static int[] S = new int[100000];
    	public static int[] route = new int[100000];
    	public static class Point
    	{
    		public int start, end;
    		Point(int start, int end)
    		{
    			this.start = start;
    			this.end = end;
    		}
    	}
    	public static void main(String[] args)
    	{
    		Vector<Point> v = new Vector<Point>();
    		Scanner cin  = new Scanner(System.in);
    		int n = cin.nextInt();
    		for (int i = 0; i < n; ++i)
    		{
    			S[i] = cin.nextInt();
    		}
    		for (int i = 0; i < n; ++i)
    		{
    			E[i] = cin.nextInt();
    		}
    		cin.close();
    		for (int i = 0; i < n; ++i)
    		{
    			v.add(new Point(S[i], E[i]));
    		}
    		Collections.sort(v, new Comparator<Point>(){
    					public int compare(Point o1, Point o2)
    					{
    						return o1.end - o2.end;
    					}
    				});//升序排列,若降序交换即可(默认升序)
    		int ans = 0, time = 0, j = 0;
    		for (int i = 0; i < n; ++i)
    		{
    			Point p = v.get(i);
    			if (time < p.start)
    			{
    				++ans;
    				route[++j] = i + 1;//记录第几件工作
    				time = p.end;
    			}
    		}
    		System.out.println(ans + "件");
    		System.out.print("选取工作 ");
    		for (int i = 1; i <= j; ++i)
    		{
    			System.out.print(route[i]+ " ");
    		}
    		System.out.println();
    	}
    }

    ========================================Talk is cheap, show me the code=======================================
    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 ——Link-Cut Tree
    BZOJ 2049 [Sdoi2008]Cave 洞穴勘测 ——Link-Cut Tree
    hdu
    hdu
    hdu
    hdu
    hdu
    hdu
    hdu
    hdu
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179832.html
Copyright © 2011-2022 走看看