题解 CF576C 【Points on Plane】

题解 CF576C 【Points on Plane】

一道很好的思维题。

传送门

我们看这个曼哈顿距离,显然如果有一边是按顺序排列的,显然是最优的,那另一边怎么办呢?

假如你正在(ioi)赛场上,此时遇到一个(nle 10^6)的题目,你现在发现自己的排列最坏情况是(O(n^2))的,你怎么办?

可以莫队优化!

于是复杂度降到了(O(nsqrt{n}))

那么我们回来看,假设点是按(x)轴为关键字排序的,那么(x)方向产生的贡献最多是(n)的。

那么,算上(y)轴方向上的贡献,最终的答案是

(f(n)=n+nsqrt{n})

(nle10^6)时,

(y=f(x),y_{min}=1001000000<2.5 imes 10^9)

于是这题就解决了。上代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<bitset>
#include<vector>
#include<map>
#include<ctime>
#include<cstdlib>
#include<set>
#include<bitset>
#include<stack>
#include<list>
#include<cmath>
using namespace std;
#define RP(t,a,b) for(register int (t)=(a),edd_=(b);t<=edd_;++t)
#define DRP(t,a,b) for(register int (t)=(a),edd_=(b);t>=edd_;--t)
#define ERP(t,a) for(int t=head[a];t;t=e[t].nx)
#define Max(a,b) ((a)<(b)?(b):(a))
#define Min(a,b) ((a)<(b)?(a):(b))
#define TMP template<class ccf>
#define lef L,R,l,mid,pos<<1
#define rgt L,R,mid+1,r,pos<<1|1
#define midd register int mid=(l+r)>>1
#define chek if(R<l||r<L)return
#define all 1,n,1
#define pushup(x) seg[(x)]=seg[(x)<<1]+seg[(x)<<1|1]
typedef long long ll;
TMP inline ccf qr(ccf k){
    char c=getchar();
    ccf x=0;
    int q=1;
    while(c<48||c>57)
	q=c==45?-1:q,c=getchar();
    while(c>=48&&c<=57)
	x=x*10+c-48,c=getchar();
    if(q==-1)
	x=-x;
    return x;
}
const int maxn=1e6+15;
int be[maxn];
int N;
int n;
struct node{
    int x,y,id;
    inline void scan(int k){
	x=qr(1);
	y=qr(1);
	id=k;
    }
    inline bool operator < (node z){
	int dx=z.x;
	int dy=z.y;
	if(be[dx]==be[x]){
	    if(be[dx]&1)
		return y<dy;
	    else
		return y>dy;
	}
	else
	    return x<dx;
    }
}data[maxn];

    
int main(){
#ifndef ONLINE_JUDGE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
    n=qr(1);
    RP(t,1,n)
	data[t].scan(t);
    N=pow(n,0.5);
    RP(t,1,maxn-15)
	be[t]=(t-1)/N+1;
    sort(data+1,data+n+1);
    RP(t,1,n)
	cout<<data[t].id<<' ';
    cout<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/winlere/p/10311212.html