字节跳动2018校招大数据方向(第一批)编程题1

字节跳动2018校招大数据方向(第一批)编程题1

题目描述

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轴。

输入例子1:
5
1 2
5 3
4 6
7 5
9 0

输出例子1:
4 6
7 5
9 0

代码实现

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        Corr[] cor = new Corr[n];
        for(int i=0; i<n; i++){
            int x = cin.nextInt();
            int y = cin.nextInt();
            cor[i] = new Corr(x, y);
        }
        Arrays.sort(cor);//按照横坐标进行排序
        int max = -1;
        List<Corr> list = new ArrayList<>();
        for(int i=0; i<n; i++){
            Corr now = cor[i];
            int x = now.x;
            if(x > max){
                System.out.println(now.x + " " + now.y);
                max = x;
            }
        }
    }
}
class Corr implements Comparable<Corr>{
    int x, y;
    Corr(){}
    Corr(int x, int y){this.x = x; this.y = y;}
    @Override
    public int compareTo(Corr ano){
        return ano.y - y;
    }
}
posted @ 2021-03-13 21:17  Garrett_Wale  阅读(106)  评论(0编辑  收藏  举报