P为给定的二维平面整数点集。定义 P 中某点x,如果x满足 P 中任意点都不在 x 的右上方区域内(横纵坐标都大于x),则称其为“最大的”。求出所有“最大的”点的集合。(所有点的横坐标和纵坐标都不重复, 坐标轴范围在[0, 1e9) 内)
如下图:实心点为满足条件的点的集合。请实现代码找到集合 P 中的所有 ”最大“ 点的集合并输出。
P为给定的二维平面整数点集。定义 P 中某点x,如果x满足 P 中任意点都不在 x 的右上方区域内(横纵坐标都大于x),则称其为“最大的”。求出所有“最大的”点的集合。(所有点的横坐标和纵坐标都不重复, 坐标轴范围在[0, 1e9) 内)
如下图:实心点为满足条件的点的集合。请实现代码找到集合 P 中的所有 ”最大“ 点的集合并输出。
第一行输入点集的个数 N, 接下来 N 行,每行两个数字代表点的 X 轴和 Y 轴。 对于 50%的数据, 1 <= N <= 10000; 对于 100%的数据, 1 <= N <= 500000;
输出“最大的” 点集合, 按照 X 轴从小到大的方式输出,每行两个数字分别代表点的 X 轴和 Y轴。
5 1 2 5 3 4 6 7 5 9 0
4 6 7 5 9 0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
int num=Reader.nextInt();
PriorityQueue<int[]> points=new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[1]-o1[1];
}
});
for (int i = 0; i < num; i++) {
int x=Reader.nextInt();
int y=Reader.nextInt();
points.offer(new int[]{x,y});
}
int[] p= points.poll();
int x=p[0];
int y=p[1];
PrintWriter out=new PrintWriter(System.out);
out.println(x+" "+y);
while (!points.isEmpty()){
p= points.poll();
if (p[0]>x){
x=p[0];
out.println(p[0]+" "+p[1]);
}
}
out.flush();
}
static class Reader {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()){
tokenizer=new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
} 60%,求大佬看看哪地方需要改进
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
public class NO1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int num = s.nextInt();
HashMap point = new HashMap();
int[] point_x = new int[num];
for(int i = 0; i<num; i++) {
int x = s.nextInt();
int y = s.nextInt();
point.put(x, y);
point_x[i] = x;
}
Arrays.sort(point_x);
int key = -1;
int[] result = new int[num];
int index=0;
for(int i=num-1; i>=0; i--) {
if(point.get(point_x[i]) > key) {
key = point.get(point_x[i]);
result[index] = point_x[i];
index++;
}
}
for(int j=index-1; j>=0; j--) {
// String fs = String.format("%d %d", result[j], point.get(result[j]));
System.out.print(result[j]);
System.out.print(" ");
System.out.println(point.get(result[j]));
}
}
}
70%超时。。求大佬们提意见~~啥意见都行
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); int num = Integer.valueOf(scanner.nextLine());
Point[] points = new Point[num];
List maxPoints = new ArrayList();
String[] line; for (int i = 0; i < num; i++) {
line = scanner.nextLine().trim().split(" ");
points[i] = new Point(Integer.valueOf(line[0]), Integer.valueOf(line[1]));
}
Arrays.sort(points);
maxPoints.add(points[num - 1]);
int maxY = points[num - 1].y; for (int i = num - 2; i >= 0; i--) { if (points[i].y >= maxY) {
maxPoints.add(points[i]);
maxY = points[i].y;
}
}
Collections.reverse(maxPoints);
maxPoints.forEach(System.out::println);
}
static class Point implements Comparator<Point> {
int x;
int y;
public Point(int x, int y) {
this.x = x; this.y = y;
}
[@Override](/profile/992988)
public int compareTo(Point o) {
return Integer.compare(this.x, o.x);
}
[@Override](/profile/992988)
public String toString() {
return x + " " + y;
}
}
}