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.util.*; import java.io.*; public class Main{ public static void main(String[] args) throws Exception{ // 快速输入输出 StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken(); int n = (int)in.nval; int[][] pts = new int[n][2]; for(int i = 0;i < n;i ++){ in.nextToken(); pts[i][0] = (int)in.nval; in.nextToken(); pts[i][1] = (int)in.nval; } sort(pts); List<Integer> ans = new ArrayList<>(); ans.add(pts.length - 1); int maxs = pts[pts.length-1][1]; for(int i = pts.length - 2;i >= 0;i --){ if(pts[i][1] >= maxs){ ans.add(i); } maxs = Math.max(maxs, pts[i][1]); } for(int i = ans.size() - 1;i >= 0;i --){ out.println(pts[ans.get(i)][0] + " " + pts[ans.get(i)][1]); } out.flush(); } // 桶排序 public static void sort(int[][] pts){ int n = pts.length; int[] count = new int[10]; int tp = 1; int[][] dp = new int[n][2]; for(int i = 0;i < 10;i ++){ for(int j = 0;j < 10;j ++){ count[j] = 0; } for(int j = 0;j < n;j ++){ int y = pts[j][0]; count[y/tp%10] ++; } for(int j = 1;j < 10;j ++){ count[j] += count[j-1]; } for(int j = n - 1;j >= 0;j --){ int pos = pts[j][0] / tp % 10; count[pos] --; dp[count[pos]][0] = pts[j][0]; dp[count[pos]][1] = pts[j][1]; } tp *= 10; for(int j = 0;j < n;j ++){ pts[j][0] = dp[j][0]; pts[j][1] = dp[j][1]; } } } }
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; /** * T18 * 和C++一样的逻辑,时间复杂度为N,竟然运行不通过,赤裸裸的歧视啊 */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<Point> list = new ArrayList<>(500002); for (int i = 0; i < n; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); list.add(new Point(x, y)); } reslove1(list); } public static void reslove1(List<Point> list) { list.sort(new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { int i = o1.x - o2.x; if (i == 0) { i = o1.y - o2.y; } return i; } }); List<Point> result = new ArrayList<>(500002); int maxy = -1; for (int i = list.size(); i > 0; i--) { Point p1 = list.get(i - 1); if (p1.y > maxy) { result.add(p1); maxy = p1.y; } } for (int i = result.size(); i > 0; i--) { Point t = result.get(i - 1); System.out.println(t.x + " " + t.y); } } static class Point { int x; int y; //默认为0,当不为0时,标识已经处理过 int z; Point(int x, int y) { this.x = x; this.y = y; } } }
时间复杂度为O(nlogn) 只通过60%
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = Integer.parseInt(scan.nextLine()); int[][] arr = new int[n][2]; for (int i = 0; i < n; i++) { String[] line = scan.nextLine().split(" "); arr[i][0] = Integer.parseInt(line[0]); arr[i][1] = Integer.parseInt(line[1]); } // 按x的坐标从小到大排序 Arrays.sort(arr, (o1, o2) -> o1[0] - o2[0]); ArrayList<int[]> list = new ArrayList<>(); // 使用动态规划从后往前 int last_max = -1; // 记录当前下标后面的最大值 for (int i = n - 1; i >= 0; i--) { // 如果后面的y值没有大于当前y值的就添加进去 if (arr[i][1] > last_max) list.add(0, arr[i]); last_max = Math.max(last_max, arr[i][1]); // 更新最大值 } for (int[] temp : list) { System.out.println(temp[0] + " " + temp[1]); } } }
/* 说算法的复杂度过大,只通过50%,哪位大佬帮忙看看啊。救救孩子! 筛选用的遍历 排序用的快速排序 */ import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int [][] arr = new int[n][2]; for (int i = 0; i < n; i++) { arr[i][0] = scanner.nextInt(); arr[i][1] = scanner.nextInt(); } List<int[]> answer = new ArrayList<>(); for (int i = 0; i < n ; i++) { boolean flag = true; int right = n-1; int left = 0; while(left<right){ if ((left!=i) &&(arr[left][0]>arr[i][0]&&arr[left][1]>arr[i][1])){ flag = false; break; }else if ((right!=i)&&(arr[right][0]>arr[i][0]&&arr[right][1]>arr[i][1])){ flag = false; break; } left++; right--; } if (flag){ answer.add(arr[i]); } } int size = answer.size()/2; int[][] result = answer.toArray(new int[size][]); //结果集排序 quickSort(result,0,result.length-1); //输出 for (int i = 0; i < result.length; i++) { System.out.println(result[i][0]+" "+result[i][1]); } } //快速排序 public static void quickSort(int[][] arr,int low,int high){ if (low>=high){ return ; } int left = low; int right = high; int[] key = arr[low]; while (left<right){ while (arr[right][0]>=key[0] && left<right){ right--; } while (arr[left][0]<=key[0] && left<right){ left++; } if (left<right){ int[] temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } } arr[low] = arr[left]; arr[left] = key; quickSort(arr,low,left-1); quickSort(arr,left+1,high); } }
import java.util.Scanner; //只能通过70%,求大佬帮忙看看 public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int N = input.nextInt(); int[][] p = new int[N][2]; for(int i = 0; i < N; i++) { p[i][0] = input.nextInt(); p[i][1] = input.nextInt(); } input.close(); Solution(N, p); } public static void Solution(int N, int[][] p){ QuickSort(p, 0, N-1); int[] ymax = new int[N]; ymax[N-1] = p[N-1][1]; for(int i = N-2; i >= 0; i--) { ymax[i] = (p[i][1] > ymax[i+1]) ? p[i][1] : ymax[i+1]; } for(int i = 0; i < N; i++) { if(p[i][1] == ymax[i]) { System.out.println(p[i][0]+" "+p[i][1]); } } } public static void QuickSort(int[][] p, int start, int end) { int i = start; int j = end; int k = p[start][0]; int[] t; while(i < j) { t = p[i]; while(i < j && p[j][0] >= k) { j --; } p[i] = p[j]; p[j] = t; while(i < j && p[i][0] <= k) { i ++; } p[j] = p[i]; p[i] = t; } if(i - 1 > start) QuickSort(p, start, i-1); if(i + 1 < end) QuickSort(p, i+1, end); } }
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Port{ int x; int y; } class cmp implements Comparator<Port>{ @Override public int compare(Port o1, Port o2) { if(o1.y < o2.y) { return 1; }else{ return -1; } } } public class Main{ public static void main(String[] args) { @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Port[] p = new Port[n]; for (int i = 0; i < n; i++) { Port port = new Port(); port.x = scanner.nextInt(); port.y = scanner.nextInt(); p[i] = port; } Arrays.sort(p, 0, n, new cmp()); int max = -1; for (int i = 0; i < p.length; i++) { if (p[i].x >max) { max = p[i].x; System.out.print(p[i].x + " " + p[i].y +"\n"); } } } }