cf97B
• 给定平面直角坐标系上的n个点,可以添加若干点使得任意两点 至少满足以下三个条件之一
• 1. 两点横坐标相同
• 2. 两点纵坐标相同
• 3. 两点构成的矩形内(包含边界)存在其他点
• 最终点集最多20w个点,输出方案
• 1 ≤ n ≤ 1e4
我们先给所有点按照 x 排序,然后在中间建立一个平行 y 轴的投影轴,把左右所有点都在中间投影,那么中间所有的点与左边或者右边都满足条件 左边与右边的点又包含中间的点
所以开始分治左半部分 和右半部分
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 10025;
struct node
{
int x,y;
bool operator< (const node other) const&
{
return x < other.x;
}
}arr[MAX_N];
set<pair<int ,int > >st;
void dfs(int l,int r)
{
int mid = (l+r)>>1;
for(int i = l;i<=r;++i)
{
st.insert(make_pair(arr[mid].x,arr[i].y));
}
if(l<mid) dfs(l,mid-1);
if(mid<r) dfs(mid+1,r);
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i = 1;i<=n;++i)
scanf("%d%d",&arr[i].x,&arr[i].y),st.insert(make_pair(arr[i].x,arr[i].y));
sort(arr+1,arr+1+n);
dfs(1,n);
printf("%d\n",st.size());
for(set<pair<int ,int > >::iterator it = st.begin();it!=st.end();++it)
{
printf("%d %d\n",it->first,it->second);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}

该博客讨论了如何利用分治算法解决Codeforces上97B题目的挑战。题目要求在给定的n个平面上的点中添加点,使得任意两点满足至少一种特定条件,如横坐标相同、纵坐标相同或构成的矩形包含其他点。通过首先按x坐标排序并投影到y轴的平行线上,可以将问题分解为处理左半部分和右半部分,确保所有点都满足条件。最终解决方案允许最多20w个点,并且适用于n值在1e4以内的范围。
1109

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



