这题主要对第三个条件的理解,一开始我还以为是只有在两个点构成的矩形中没有任何点时才加入新的点,后来才知道,想多了,不管有没有点,直接加就行了。这样的话,,程序就相对的简单了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
#define maxn 10005
int n,cnt;
struct Node{
int x;
int y;
bool operator < (const Node b)const {
if(x == b.x){
return y < b.y;
}
return x < b.x;
}
};
Node arr[maxn];
set<Node> s;
set<Node>::iterator it;
int cmpX(Node a,Node b){
return a.x < b.x;
}
int cmpY(Node a,Node b){
return a.y < b.y;
}
void divide(int low,int high);
int main()
{
scanf("%d",&n);
cnt = 0;
s.clear();
for(int i = 0; i < n; i++){
scanf("%d%d",&arr[i].x,&arr[i].y);
s.insert(arr[i]);
}
sort(arr,arr+n,cmpX);
divide(0,n-1);
printf("%d\n",s.size());
for(it = s.begin();it != s.end();it++){
printf("%d %d\n",it->x,it->y);
}
return 0;
}
void divide(int low,int high){
if(low == high){
return ;
}
int mid = (low + high) /2;
for(int i = low; i <= high ;i++){
Node tmp;
tmp.x = arr[mid].x;
tmp.y = arr[i].y;
s.insert(tmp);
}
divide(low,mid);
divide(mid+1,high);
return ;
}