时间限制:1秒
空间限制:32768K
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
#include <bits/stdc++.h>
using namespace std;
struct point{
int x, y;
bool operator < (const point& a) const{
if(x == a.x){
return y < a.y;
}
return x > a.x;
}
}a[500005];
int h[1000002], c[1000002];
int lowbit(int x){
return x & (-x);
}
void add(int x, int ma){
while(x <= ma){
c[x] += 1;
x += lowbit(x);
}
}
int query(int x){
int res = 0;
while(x){
res += c[x];
x -= lowbit(x);
}
return res;
}
vector<pair<int,int> > ans;
int main(){
int n, tot = 0, pos;
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d %d", &a[i].x, &a[i].y);
a[i].x++; a[i].y++;
h[++tot] = a[i].x;
h[++tot] = a[i].y;
}
sort(h + 1, h + 1 + tot);
int num = unique(h + 1, h + 1 + tot) - h - 1;
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; ++i){
pos = lower_bound(h + 1, h + 1 + num, a[i].y) - h;
if(query(pos) == i - 1){
ans.push_back(make_pair(a[i].x - 1, a[i].y - 1));
}
add(pos, num);
}
for(int i = ans.size() - 1; i >= 0; --i){
printf("%d %d\n", ans[i].first, ans[i].second);
}
}
/*
题意:
5e5个点,坐标范围1e9,输出所有点满足没有点在其右上方。
思路:
坐标范围很大,离散一下。然后按x轴排序,然后就扫一遍,对于之前的点,x一定比当前
点大,就看y有没有比它大的就行了,树状数组维护一下即可。
注意处理一下x一样大的情况。
*/

被折叠的 条评论
为什么被折叠?



