Java藍(lán)橋杯實(shí)現(xiàn)線段和點(diǎn)
一、算法提高 線段和點(diǎn)
1、時(shí)間限制
1.0s 內(nèi)存限制:256.0MB
2、問題描述
有n個點(diǎn)和m個區(qū)間,點(diǎn)和區(qū)間的端點(diǎn)全部是整數(shù),對于點(diǎn)a和區(qū)間[b,c],若a>=b且a<=c,稱點(diǎn)a滿足區(qū)間[b,c]。
求最小的點(diǎn)的子集,使得所有區(qū)間都被滿足。
3、輸入格式
第一行兩個整數(shù)n m
以下n行 每行一個整數(shù),代表點(diǎn)的坐標(biāo)
以下m行 每行兩個整數(shù),代表區(qū)間的范圍
4、輸出格式
輸出一行,最少的滿足所有區(qū)間的點(diǎn)數(shù),如無解輸出-1。
樣例輸入:
5 5
2
6
3
8
7
2 5
3 4
3 3
2 7
6 9
樣例輸出:
2
5、數(shù)據(jù)規(guī)模和約定
1<=n,m<=10000
0<=點(diǎn)和區(qū)間的坐標(biāo)<=50000
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
public class xianduanhedian {
private static InputStream 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);
}
}
到此這篇關(guān)于Java實(shí)現(xiàn)藍(lán)橋杯 算法提高 線段和點(diǎn)的文章就介紹到這了,更多相關(guān)Java內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時(shí)候報(bào)decompiled.class file byt
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時(shí)候報(bào)decompiled.class file bytecode version:52.0(java 8)錯誤的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Java實(shí)現(xiàn)兩人五子棋游戲(七) 屏幕提示信息
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)兩人五子棋游戲,屏幕提示游戲信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Mybatis不支持batchInsertOrUpdate返顯id問題
這篇文章主要介紹了Mybatis不支持batchInsertOrUpdate返顯id問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring?Cloud?Gateway集成Sentinel流控詳情
這篇文章主要介紹了Spring?Cloud?Gateway集成Sentinel流控詳情,Sentinel支持對Spring?Cloud?Gateway、Zuul等主流的API?Gateway進(jìn)行限流,需要的朋友可以參考一下2022-09-09

