zoukankan      html  css  js  c++  java
  • 贪心算法----区间选点问题(POJ1201)

    题目:

      

      题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量。

      输入:

        第一行输入n,代表n个区间。

        接下来的n行每行的第一个数代表区间起点,第二个数代表区间终点,第三个数代表这个区间必须要选取的点的数量。

      输出:

        输出最少的点的数量,这些最少的点要覆盖全部区间。

      这个题是区间选点问题的一种变体,但是我们对于区间选点问题清楚之后那么这种题目也是一样解决的,只不过需要在某些地方特别处理一下。这道题目跟区间调度的问题非常类似,我们也可以采用区间调度问题的策略运用到这道题目上面,尽量往结束时间的端点来选点因为这样做我们可以使尽量少的点覆盖更多的区间,之后就是选点的问题了,当我们选择了这个点之后需要标记一下,定义一个数轴来记录其中标记过的点,以防下一次在选点的时候重复选择。而且在for循环中依次扫描这些给定的区间,看这个区间需要覆盖的点的数量是多少(有可能这个区间的标记的点出现在另外的区间上那么这个时候我们就选择跳过这个点)

      代码:

     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 public class 区间选点问题1 {
     5     public static void main(String[] args) {
     6         Scanner sc = new Scanner(System.in);
     7         int n = sc.nextInt();
     8         Interval[] intervals = new Interval[n];
     9         for (int i = 0; i < n; i++) {
    10             intervals[i] = new Interval(sc.nextInt(), sc.nextInt(), sc.nextInt());
    11         }
    12         Arrays.sort(intervals);// 按区间右端点排序
    13 
    14         int max = intervals[n - 1].t;// 右端最大值
    15         int[] axis = new int[max + 1];// 标记数轴上的点是否已经被选中
    16         // int[] sums = new int[max + 1];
    17         for (int i = 0; i < n; i++) {
    18             // 1.查阅区间中有多少个点
    19             int s = intervals[i].s;// 起点
    20             int t = intervals[i].t;// 终点
    21             int cnt = sum(axis, s, t);// 找到这个区间已经选点的数量,
    22                                         //sums[t] - sums[s - 1]; 效率低
    23             // 2.如果不够,从区间右端开始标记,遇标记过的就跳过
    24             intervals[i].c -= cnt;// 需要新增的点的数量
    25             while (intervals[i].c > 0) {
    26                 if (axis[t] == 0) {// 从区间终点开始选点
    27                     axis[t] = 1;
    28                     // updateSums(t,sums);//更新前缀和
    29                     intervals[i].c--;// 进一步减少需要新增的点的数量
    30                     t--;
    31                 } else {// 这个点已经被选过了
    32                     t--;
    33                 }
    34             }
    35 
    36         }
    37         System.out.println(sum(axis, 0, max));
    38     }
    39 
    40     /**
    41      * 统计数轴axis上s-t区间已经有多少个点被选中
    42      * 
    43      * @param axis
    44      * @param s
    45      * @param t
    46      * @return
    47      */
    48     private static int sum(int[] axis, int s, int t) {
    49         int sum = 0;
    50         for (int i = s; i <= t; i++) {
    51             sum += axis[i];
    52         }
    53         return sum;
    54     }
    55 
    56     private static void updateSums(int t, int[] sums) {
    57         for (int i = t; i < sums.length; i++) {
    58             sums[i]++;
    59         }
    60     }
    61 
    62     private static class Interval implements Comparable<Interval> {
    63         int s;
    64         int t;
    65         int c;
    66 
    67         public Interval(int s, int t, int c) {
    68             this.s = s;
    69             this.t = t;
    70             this.c = c;
    71         }
    72 
    73         @Override
    74         public int compareTo(Interval other) {
    75             int x = this.t - other.t;
    76             if (x == 0)
    77                 return this.s - other.s;
    78             else
    79                 return x;
    80         }
    81     }
    82 }

      结果:

        

      但是上面这个代码提交到OJ上面会发生超时,代码逻辑本身没有什么问题,关键是在扫描每个区间的时候消耗的时间比较多导致了超时,但是我们可以使用另外的一种数据结构来解决,那就是树状数组,降低扫描区间的时间复杂度。关于树状数组现在不太懂,而且也脱离了贪心算法的范畴,现在把代码记录下来,留作以后再看。

      代码:

     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 public class 区间选点问题2 {
     5     public static void main(String[] args) {
     6         Scanner sc = new Scanner(System.in);
     7         int n = sc.nextInt();
     8         Interval[] intervals = new Interval[n];
     9         for (int i = 0; i < n; i++) {
    10             intervals[i] = new Interval(sc.nextInt(), sc.nextInt(), sc.nextInt());
    11         }
    12         Arrays.sort(intervals);// 按区间右端点排序
    13 
    14         int max = intervals[n - 1].t;// 右端最大值
    15         int[] axis = new int[max + 1];
    16         int[] c = new int[max + 2];
    17         // int[] sums = new int[max + 1];
    18         for (int i = 0; i < n; i++) {
    19             // 1.查阅区间中有多少个点
    20             int s = intervals[i].s;// 起点
    21             int t = intervals[i].t;// 终点
    22             int cnt = sum(t + 1, c, max + 1) - sum(s, c, max + 1);// sum(axis,s,t);//sums[t]
    23                                                                     // - sums[s
    24                                                                     // -
    25                                                                     // 1];//效率低
    26             // 2.如果不够,从区间右端开始标记,遇标记过的就跳过
    27             intervals[i].c -= cnt;
    28             while (intervals[i].c > 0) {
    29                 if (axis[t] == 0) {
    30                     axis[t] = 1;
    31                     update(t + 1, 1, c, max + 1);
    32                     intervals[i].c--;
    33                     t--;
    34                 } else {
    35                     t--;
    36                 }
    37             }
    38 
    39         }
    40         System.out.println(sum(max + 2, c, max + 1));
    41     }
    42 
    43     /**
    44      * 更新树状数组c,注意i是项数,不是下标,而是下标+1
    45      */
    46     private static void update(int i, int delta, int[] c, int n) {
    47         for (; i <= n; i += lowbit(i)) {
    48             c[i] += delta;
    49         }
    50     }
    51 
    52     /**
    53      * 前i项和,注意:i不是下标
    54      * 
    55      * @param i
    56      * @return
    57      */
    58     private static int sum(int i, int[] c, int n) {
    59         int sum = 0;
    60         if (i > n)
    61             i = n;
    62         for (; i > 0; i -= lowbit(i)) {
    63             sum += c[i];
    64         }
    65         return sum;
    66     }
    67 
    68     /**
    69      * 它通过公式来得出k,其中k就是该值从末尾开始1的位置。 然后将其得出的结果加上x自身就可以得出当前节点的父亲节点的位置
    70      * 或者是x减去其结果就可以得出上一个父亲节点的位置。
    71      * 比如当前是6,二进制就是0110,k为2,那么6+2=8,C(8)则是C(6)的父亲节点的位置;
    72      * 相反,6-2=4,则是C(6)的上一个父亲节点的位置。
    73      */
    74     static int lowbit(int x) {
    75         return x - (x & (x - 1));
    76     }
    77 
    78     private static class Interval implements Comparable<Interval> {
    79         int s;
    80         int t;
    81         int c;
    82 
    83         public Interval(int s, int t, int c) {
    84             this.s = s;
    85             this.t = t;
    86             this.c = c;
    87         }
    88 
    89         @Override
    90         public int compareTo(Interval other) {
    91             int x = this.t - other.t;
    92             if (x == 0)
    93                 return this.s - other.s;
    94             else
    95                 return x;
    96         }
    97     }
    98 }
    View Code

     

  • 相关阅读:
    Go入门笔记-1 Unbuntu直接安装go 16.5版本
    EdgexGo2.0学习-12 consul不能按服务名获取数据
    EdgexGo2.0学习-11 测试读取ModBus协议
    win10下VirtualBox开启串口
    Linux 开发板通过笔记本上网
    EdgexGo2.0学习-10 edgex-consul 编译 for "-t, --tag" flag: invalid reference format
    EdgexGo2.0学习-9 ERROR: No matching distribution found for PyYAML<4,>=3.10
    EdgexGo2.0学习-8 pip not found
    EdgexGo2.0学习-7 20.10.6: Pulling from library/docker
    EdgexGo2.0学习-6 修改日志时区显示
  • 原文地址:https://www.cnblogs.com/xiaoyh/p/10364251.html
Copyright © 2011-2022 走看看