zoukankan      html  css  js  c++  java
  • 8.6贪心策略例题:区间覆盖问题

    /**
    Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn.
    He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000),
    the first being shift 1 and the last being shift T.
    Each cow is only available at some interval of times during the day for work on cleaning.
    Any cow that is selected for cleaning duty will work for the entirety of her interval.
    Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it,
    and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

    Input
    Line 1: Two space-separated integers: N and T
    Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work.
    A cow starts work at the start time and finishes after the end time.

    Output
    Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

    Sample Input
    3 10
    1 7
    3 6
    6 10
    Sample Output
    2

    */
    //POJ2376

    找每一个子问题的最优解

     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 public class Eight_6贪心策略例题_区间覆盖问题 {
     5     private static class Job implements Comparable<Job> {
     6         int s;
     7         int t;
     8 
     9         public Job(int s, int t) {
    10             super();
    11             this.s = s;
    12             this.t = t;
    13         }
    14 
    15         @Override
    16         public int compareTo(Job Other) {
    17             int x = this.s-Other.s;
    18             if(x == 0)
    19                 return this.t-Other.t;
    20             else
    21                 return x;
    22         }
    23 
    24         @Override
    25         public String toString() {
    26             return "Job [s=" + s + ", t=" + t + "]";
    27         }    
    28     }
    29     
    30     public static void main(String[] args) {
    31         Scanner in = new Scanner(System.in);
    32         int N = in.nextInt();
    33         int T = in.nextInt();
    34         Job[] jobs = new Job[N];
    35         for(int i = 0; i < N; i++){
    36             jobs[i] = new Job(in.nextInt(), in.nextInt());
    37         }
    38         
    39         Arrays.sort(jobs);
    40         int start = 1; //要覆盖的目标点,end覆盖该点的所有区间中右端点最右
    41         int end = 1;
    42         int ans = 1;
    43         for(int i = 0; i < T; i++){
    44             int s = jobs[i].s;
    45             int t = jobs[i].t;
    46             
    47             if(i == 0 && s > 1)
    48                 break;
    49             if(s <= start){
    50                 end = Math.max(t, end);
    51             }else{ //开始下一个区间
    52                 ans++;
    53                 start = end+1; //更新起点
    54                 if(s <= start){
    55                     end = Math.max(t, end);
    56                 }else{
    57                     break;
    58                 }
    59             }
    60             if(end >= T) //区间右端点大于输入段退出循环
    61                 break;
    62         }
    63         
    64         if(end < T)
    65             System.out.println("-1");
    66         else
    67             System.out.println(ans);
    68     }
    69 }
  • 相关阅读:
    P3990 [SHOI2013]超级跳马
    P4279 [SHOI2008]小约翰的游戏
    zabbix、agent端服务器图形化展示
    zabbix_agent代理端监控FTP服务
    hadoop SecondNamenode详解
    HADOOP_SECURE_DN_USER has been replaced by HDFS_DATANODE_SECURE_USER
    Linux netstat命令详解(检验本机各端口的网络连接情况)
    查看监听端口命令
    Hadoop服务的端口50070无法访问的问题
    Linux-centos6.8下关闭防火墙
  • 原文地址:https://www.cnblogs.com/z1110/p/12761926.html
Copyright © 2011-2022 走看看