字节跳动笔试:求出点集合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轴。


输入例子1:

> 5 1 2 5 3 4 6 7 5 9 0

输出例子1:

> 4 6 7 5 9 0




解法:按照y从大到小排序,然后x从小到大选择,x值比前一个大的y值肯定比前一个大(y从大到小排列),从而是“最大的”点```


#include
#include
#include
#include
using namespace std;
struct node
{
int x;
int y;
};
bool cmp(node n1,node n2)
{
return n1.y>n2.y;
}
node nod[1000000];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&nod[i].x,&nod[i].y);
}
sort(nod,nod+n,cmp);
int min=-1;
for(int i=0;i<n;i++)
{
if(nod[i].x>min)
{
min=nod[i].x;
printf("%d %d\n",nod[i].x,nod[i].y);
}
}
}
return 0;
}


public class Main {
//二维数组,每一维想要输入任意个数的字符用空格隔开时可用以下代码
public static void main(String[] args) {

	Scanner sc=new Scanner(System.in);
	 int n=sc.nextInt();//获取点数
                             int a[][]=new int[n][1];
	//跳过这行换行符  
	sc.nextLine();	
	for(int i=0;i<n;i++){
	String str = sc.nextLine();   
	Scanner sc1 = new Scanner(str); 
	int j=0;
	while(sc1.hasNextInt()){
		a[i][j++]=sc1.nextInt();		
			}
	}

                           //已经获得数组,行值为x,列值为y


                          //对数组进行排序

   
                          //比较,如果该数x不小于前一个的x,则比较y,y较大为最大的
	for( int i=0;i<n-1;i++){


                                     if(a[i][0]<a[i+1][0]){
                                          if(a[i][1]<=a[i+1][1])
                                          System.out.println(a[i][1]);
                                          }else{ 
                                             if(a[i][0]=a[i+1][0]){

                                           if()
		while(a[i][j]!=0)
		System.out.print(a[i][j++]+"  ");
		System.out.println();
	}

}

}

发布了254 篇原创文章 · 获赞 23 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_30242987/article/details/104713948