链接:编程题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
输出:
4 6 7 5 9 0
题解:
注意:如果 scanf 换成 cin,测试用例会卡在80%。显示超时。
难怪打竞赛的大佬 都是用scanf 和 printf 。
#include <iostream>
#include <algorithm>
#include <vector>
//1 2
//4 6 *
//5 3
//7 5 *
//9 0 *
//9 0 *
//1 2
//5 3
//7 5 *
//4 6 *
using namespace std;
typedef pair<int,int> PII;
const int N = 500010;
PII p[N];
int main(){
int num;
scanf("%d\n",&num);
//cin>>num;
for(int i = 0;i < num; i++){
scanf("%d %d\n",&p[i].first,&p[i].second);
//cin>>p[i].x>>p[i].y;
}
std::sort(p,p+num,[](PII p1,PII p2){
return p1.second > p2.second;
});
int curmaxx = p[0].first;
for(int i = 0;i < num ;i++){
if(p[i].first >= curmaxx){
//cout<<p[j].x<<" "<<p[j].y<<endl;
printf("%d %d\n",p[i].first,p[i].second);
curmaxx = p[i].first;
}
}
return 0;
}