• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java实现蓝桥杯 算法提高 线段和点

java 搞代码 4年前 (2022-01-09) 39次浏览 已收录 0个评论

一、算法提高 线段和点

1、时间限制

1.0s 内存限制:256.0MB

2、问题描述 

有n个点和m个区间,点和区间的端点全部是整数,对于点a和区间[b,c],若a>=b且a<=c,称点a满足区间[b,c]。
  求最小的点的子集,使得所有区间都被满足。

3、输入格式

第一行两个整数n m
  以下n行 每行一个整数,代表点的坐标
  以下m行 每行两个整数,代表区间的范围

4、输出格式 

输出一行,最少的满足所有区间的点数,如无解输出-1。
样例输入:
5 5
2
6
3
8
7
2 5
3 4
3 3
2 7
6 9
样例输出:
2

5、数据规模和约定 

1<=n,m<=10000
  0<=点和区间的坐标<=50000

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;


public class xianduanhedian {
 private static In<mark>本文来源gaodaimacom搞#^代%!码&网(</mark>putStream is = System.in;

 public static int nextInt() {
  try {
   int i;

   while ((i = is.read()) < 45 || i > 57) {
   }

   int mark = 1, temp = 0;

   if (i == 45) {
    mark = -1;
    i = is.read();
   }

   while (i > 47 && i < 58) {
    temp = temp * 10 + i - 48;
    i = is.read();
   }

   return temp * mark;
  } catch (IOException e) {
   e.printStackTrace();
  }

  return -1;
 }

 static class Node {
  public int start;
  public int end;

  public Node(int start, int end) {
   this.start = start;
   this.end = end;
  }

 }

 public static void main(String[] args) {
  int n = nextInt();
  int m = nextInt();
  int point[] = new int[n];
  for (int i = 0; i < n; i++)
   point[i] = nextInt();
  Node node[] = new Node[m];
  for (int i = 0; i < m; i++)
   node[i] = new Node(nextInt(), nextInt());
  Arrays.sort(point);
  Arrays.sort(node, new Comparator<Node>() {
   public int compare(Node o1, Node o2) {
    return o1.end - o2.end;
   }
  });
  int currentPoint = 0;
  int count = 0;
  int j = 1;
  for (int i = 0; i < m; i++) {
   int x = node[i].start;
   int y = node[i].end;
   if (x <= currentPoint)
    continue;
   int temp = -1;
   for (j -= 1; j < n; j++) {
    if (point[j] <= y) {
     temp = point[j];
    } else {
     break;
    }
   }
   if (temp == -1) {
    count = 0;
    break;
   } else {
    currentPoint = temp;
    count++;
   }

  }
  System.out.println(count);
 }

}

到此这篇关于Java实现蓝桥杯 算法提高 线段和点的文章就介绍到这了,更多相关Java内容请搜索搞代码以前的文章或继续浏览下面的相关文章希望大家以后多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java实现蓝桥杯 算法提高 线段和点
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址