zoukankan      html  css  js  c++  java
  • 活动安排问题

    题目大概是这样的:在有限的时间里,安排更多的活动。

    策略一:先安排开始时间早的活动。

    策略二:先安排活动持续时间短的活动。

    策略三:先安排活动时间结束早的活动。

    解题思想:很明显,正确的答案是策略三,先排序再选活动。

    代码实现如下:

    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Scanner;
    
    class Party
    {
        int begin;
        int end;
    }
    class Cmp implements Comparator<Party>
    {
        @Override
        public int compare(Party A, Party B)
        {
            if(A.end > B.end)
            {
                return 1;
            }
            else if(A.end == B.end)
            {
                if(A.begin > B.begin)
                {
                    return 1;
                }
                else if(A.begin == B.begin)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }    
    }
    public class Main
    {
        static final int MAX = 10005;
        static Party num[] = new Party[MAX];
        public static void main(String []args)
        {
            Scanner cin = new Scanner(System.in);
            int N = cin.nextInt();
            Init(N);
            for(int i = 0; i < N; i++)
            {
                num[i].begin = cin.nextInt();
                num[i].end = cin.nextInt();
            }
            Arrays.sort(num, 0,N,new Cmp());
            int cnt = 1;
            int End = num[0].end;
            for(int i = 1; i < N; i++)
            {
                if(End < num[i].begin)
                {
                    End = num[i].end;
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
        static void Init(int N)
        {
            for(int i = 0; i < N; i++)
            {
                num[i] = new Party();
            }
        }
    }
  • 相关阅读:
    Https的请求过程
    计算机网络知识
    数据结构之图
    Python3线程池进程池
    数据结构之堆heapq
    EffectivePython并发及并行
    EffectivePython类与继承
    EffectivePython并发及并行
    5.19完全数
    5.18数字全排列
  • 原文地址:https://www.cnblogs.com/674001396long/p/10049902.html
Copyright © 2011-2022 走看看