简介
CDQ分治主要用于处理三维偏序的问题,例如:
已知 ,求 且 的对数。
对于这种问题,我们可以先将 进行排序,然后利用归并的思想,让 的 先加入树状数组,利用前缀树状数组找到所有 的就可以了。
时间复杂度:
洛谷-P3810
模板题,没问题。
#include<bits/stdc++.h>
using namespace std;
const int NA=2e5+5;
struct node{
int a,b,c,s,res;
void read(){
cin>>a>>b>>c;
}
}a[NA],tmp[NA];
bool cmp(node x,node y){
if(x.a!=y.a) return x.a<y.a;
if(x.b!=y.b) return x.b<y.b;
return x.c<y.c;
}
int n,k,cnt,tr[NA],ans[NA];
int lowbit(int x){
return x&-x;
}
void modify(int x,int v){
while(x<=k){
tr[x]+=v;
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
void CDQ(int l,int r){
if(l==r) return ;
int mid=(l+r)>>1;
CDQ(l,mid);
CDQ(mid+1,r);
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r){
if(a[p].b<=a[q].b){
modify(a[p].c,a[p].s);
tmp[tot++]=a[p++];
}
else{
a[q].res+=query(a[q].c);
tmp[tot++]=a[q++];
}
}
while(p<=mid){
modify(a[p].c,a[p].s);
tmp[tot++]=a[p++];
}
while(q<=r){
a[q].res+=query(a[q].c);
tmp[tot++]=a[q++];
}
for(int i=l;i<=mid;i++){
modify(a[i].c,-a[i].s);
}
for(int i=l;i<=r;i++){
a[i]=tmp[i];
}
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
a[i].read();
a[i].s=1;
}
sort(a+1,a+n+1,cmp);
cnt=1;
for(int i=2;i<=n;i++){
if(a[i].a==a[i-1].a&&a[i].b==a[i-1].b&&a[i].c==a[i-1].c){
a[cnt].s++;
}
else{
a[++cnt]=a[i];
}
}
CDQ(1,cnt);
for(int i=1;i<=cnt;i++){
ans[a[i].res+a[i].s-1]+=a[i].s;
}
for(int i=0;i<n;i++){
cout<<ans[i]<<"\n";
}
return 0;
} 洛谷-P3157
很显然地,我们可以将时间看作一个维度,就假设按加入时间,我们不难发现有两个情况都要处理:
两个情况处理两次就完事。
#include<bits/stdc++.h>
using namespace std;
const int NA=1e5+5;
struct node{
int a,b,c,s,res;
}a[NA],tmp[NA];
bool cmp(node x,node y){
return x.a<y.a;
}
int n,m,cnt,tr[NA],id[NA];
long long ans[NA];
int lowbit(int x){
return x&-x;
}
void modify(int x,int v){
while(x<=n){
tr[x]+=v;
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
void CDQ1(int l,int r){
if(l==r) return ;
int mid=(l+r)>>1;
CDQ1(l,mid);
CDQ1(mid+1,r);
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r){
if(a[p].b<a[q].b){
modify(a[p].c,1);
tmp[tot++]=a[p++];
}
else{
ans[a[q].a]+=query(n)-query(a[q].c);
tmp[tot++]=a[q++];
}
}
while(p<=mid){
modify(a[p].c,1);
tmp[tot++]=a[p++];
}
while(q<=r){
ans[a[q].a]+=query(n)-query(a[q].c);
tmp[tot++]=a[q++];
}
for(int i=l;i<=mid;i++){
modify(a[i].c,-1);
}
for(int i=l;i<=r;i++){
a[i]=tmp[i];
}
}
void CDQ2(int l,int r){
if(l==r) return ;
int mid=(l+r)>>1;
CDQ2(l,mid);
CDQ2(mid+1,r);
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r){
if(a[p].b>a[q].b){
modify(a[p].c,1);
tmp[tot++]=a[p++];
}
else{
ans[a[q].a]+=query(a[q].c);
tmp[tot++]=a[q++];
}
}
while(p<=mid){
modify(a[p].c,1);
tmp[tot++]=a[p++];
}
while(q<=r){
ans[a[q].a]+=query(a[q].c);
tmp[tot++]=a[q++];
}
for(int i=l;i<=mid;i++){
modify(a[i].c,-1);
}
for(int i=l;i<=r;i++){
a[i]=tmp[i];
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[i].a=0;
a[i].b=i;
a[i].c=x;
id[x]=i;
}
for(int i=1;i<=m;i++){
int x;
cin>>x;
a[id[x]].a=m-i+1;
}
sort(a+1,a+n+1,cmp);
CDQ1(1,n);
sort(a+1,a+n+1,cmp);
CDQ2(1,n);
for(int i=1;i<=m;i++){
ans[i]=ans[i]+ans[i-1];
}
for(int i=m;i>=1;i--){
cout<<ans[i]<<"\n";
}
return 0;
} 洛谷-P4169
我们显然可以发现,我们可以给所有坐标映射到四个象限,显然可以发现,我们四个象限处理是,正好对应了四种情况,使这四种情况转化为了同一种计算方式。于是这道题就异常简单了。
我们的三维便是时间, 和 。
#include<bits/stdc++.h>
using namespace std;
const int NA=6e5+5,MA=1e6+5;
struct node{
int a,b,c,id;
}a[NA],tmp[NA],re[NA];
int n,m,cnt,tr[MA];
int ans[NA];
int mx=0,my=0;
int lowbit(int x){
return x&-x;
}
void modify(int x,int v){
while(x<=my){
tr[x]=max(tr[x],v);
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans=max(ans,tr[x]);
x-=lowbit(x);
}
return ans?ans:-1e9;
}
void del(int x){
while(x<=my){
tr[x]=0;
x+=lowbit(x);
}
}
void CDQ(int l,int r){
if(l==r) return ;
int mid=(l+r)>>1;
CDQ(l,mid);
CDQ(mid+1,r);
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r){
if(a[p].b<=a[q].b){
if(a[p].id==-1) modify(a[p].c,a[p].b+a[p].c);
tmp[tot++]=a[p++];
}
else{
if(a[q].id!=-1) ans[a[q].id]=min(ans[a[q].id],a[q].b+a[q].c-query(a[q].c));
tmp[tot++]=a[q++];
}
}
while(p<=mid){
if(a[p].id==-1) modify(a[p].c,a[p].b+a[p].c);
tmp[tot++]=a[p++];
}
while(q<=r){
if(a[q].id!=-1) ans[a[q].id]=min(ans[a[q].id],a[q].b+a[q].c-query(a[q].c));
tmp[tot++]=a[q++];
}
for(int i=l;i<=mid;i++){
if(a[i].id==-1) del(a[i].c);
}
for(int i=l;i<=r;i++){
a[i]=tmp[i];
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
x++,y++;
mx=max(mx,x);
my=max(my,y);
a[i].a=0;
a[i].b=x;
a[i].c=y;
a[i].id=-1;
}
for(int i=n+1;i<=n+m;i++){
int op,x,y;
cin>>op>>x>>y;
x++,y++;
mx=max(mx,x);
my=max(my,y);
if(op==1){
a[i].a=i;
a[i].b=x;
a[i].c=y;
a[i].id=-1;
}
else{
a[i].a=i;
a[i].b=x;
a[i].c=y;
a[i].id=++cnt;
ans[cnt]=1e9;
}
}
mx++,my++;
for(int i=1;i<=n+m;i++){
re[i]=a[i];
}
CDQ(1,n+m);
for(int i=1;i<=n+m;i++){
a[i]=re[i];
}
for(int i=1;i<=n+m;i++){
a[i].b=mx-a[i].b;
}
CDQ(1,n+m);
for(int i=1;i<=n+m;i++){
a[i]=re[i];
}
for(int i=1;i<=n+m;i++){
a[i].c=my-a[i].c;
}
CDQ(1,n+m);
for(int i=1;i<=n+m;i++){
a[i]=re[i];
}
for(int i=1;i<=n+m;i++){
a[i].b=mx-a[i].b;
a[i].c=my-a[i].c;
}
CDQ(1,n+m);
for(int i=1;i<=cnt;i++){
cout<<ans[i]<<"\n";
}
return 0;
} 洛谷-P4093
我们看到最长上升子序列不难想到动态规划,转移公式就是 ,然而这里,它要满足:
其中 表示 可变化的最大值, 表示 可变化的最小值。
哦,三位偏序!!!
但显然,这里的三位偏序会有所不同。
我们显然是先进行 ,然后计算,再 。这样我们可以顺利的使动态规划合理进行。
同时,在计算过程中,我们显然对于 与 的两个区间需要排序, 显然是按 从小到大排序, 是按 从小到大排序,以便于比较。剩下一维显然就是留给最后一个式子了。
最后要清空加按输入顺序排序。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=2e6+5;
struct node{
int a,b,c,s;
}a[NA],tmp[NA],re[NA];
int op,n,m,cnt,tr[NA],ans[NA];
int lowbit(int x){
return x&-x;
}
void modify(int x,int v){
while(x<=n){
tr[x]=max(v,tr[x]);
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans=max(ans,tr[x]);
x-=lowbit(x);
}
return ans==0?-1e9:ans;
}
void del(int x){
while(x<=n){
tr[x]=0;
x+=lowbit(x);
}
}
void CDQ(int l,int r){
if(l==r){
return ;
}
int mid=(l+r)>>1;
CDQ(l,mid);
sort(a+l,a+mid+1,[](node x,node y){return x.b<y.b;});
sort(a+mid+1,a+r+1,[](node x,node y){return x.s<y.s;});
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r){
if(a[p].b<=a[q].s){
modify(a[p].c,ans[a[p].a]);
tmp[tot++]=a[p++];
}
else{
ans[a[q].a]=max(query(a[q].c)+1,ans[a[q].a]);
tmp[tot++]=a[q++];
}
}
while(p<=mid){
modify(a[p].c,ans[a[p].a]);
tmp[tot++]=a[p++];
}
while(q<=r){
ans[a[q].a]=max(query(a[q].c)+1,ans[a[q].a]);
tmp[tot++]=a[q++];
}
for(int i=l;i<=mid;i++){
del(a[i].c);
}
sort(a+l,a+r+1,[](node x,node y){return x.a<y.a;});
CDQ(mid+1,r);
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[i].a=i;
a[i].b=x;
a[i].c=x;
a[i].s=x;
ans[i]=1;
}
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
a[x].b=max(a[x].b,y);
a[x].c=min(a[x].c,y);
}
CDQ(1,n);
int res=0;
for(int i=1;i<=n;i++) res=max(res,ans[i]);
cout<<res<<"\n";
return 0;
}洛谷-P4755
题意简化:
已知数列 ,求有多少组 满足 。
显然我们可以划分两个区间,对于左边和右边区间里的组数,我们留给递归写。我们只需要考虑跨区间的。
维护 表示以 为开头的后缀最大值, 表示以 为开头的前缀最大值,显然有单调性。
考虑到最大值无非就是左边或者右边,于是我们可以分情况讨论:
面对这两种情况,我们显然会有两个处理方式:
- 在当 满足情况一时加入树状数组
- 在当 满足情况二时加入树状数组
我们由于要处理值域树状数组,所以需要离散化,其他的,显然就是查询时是拿对应情况最大值除以当前值来找 ,再用树状数组拿下。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e5+5;
int a[NA],v[NA];
int op,n,cnt,tr[NA],ans;
int mxl[NA],mxr[NA];
int lowbit(int x){
return x&-x;
}
void modify(int x,int v){
while(x<=n){
tr[x]+=v;
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
void CDQ(int l,int r){
if(l==r){
if(a[l]*a[l]<=a[l]) ans++;
return ;
}
int mid=(l+r)>>1;
CDQ(l,mid);
CDQ(mid+1,r);
mxl[mid]=a[mid];
for(int i=mid-1;i>=l;i--){
mxl[i]=max(mxl[i+1],a[i]);
}
mxr[mid+1]=a[mid+1];
for(int i=mid+2;i<=r;i++){
mxr[i]=max(mxr[i-1],a[i]);
}
int p=mid,q=mid+1;
while(p>=l){
if(q<=r&&mxl[p]>=mxr[q]){
int rk=lower_bound(v+1,v+cnt+1,a[q])-v;
modify(rk,1);
q++;
}
else{
int t=mxl[p]/a[p];
int rk=upper_bound(v+1,v+cnt+1,t)-v-1;
if(rk>0) ans+=query(rk);
p--;
}
}
for(int i=mid+1;i<q;i++){
int rk=lower_bound(v+1,v+cnt+1,a[i])-v;
modify(rk,-1);
}
p=mid,q=mid+1;
while(q<=r){
if(p>=l&&mxl[p]<mxr[q]){
int rk=lower_bound(v+1,v+cnt+1,a[p])-v;
modify(rk,1);
p--;
}
else{
int t=mxr[q]/a[q];
int rk=upper_bound(v+1,v+cnt+1,t)-v-1;
if(rk>0) ans+=query(rk);
q++;
}
}
for(int i=mid;i>p;i--){
int rk=lower_bound(v+1,v+cnt+1,a[i])-v;
modify(rk,-1);
}
return ;
}
signed main(){
cin>>n;
int maxx=0;
for(int i=1;i<=n;i++){
cin>>a[i];
v[i]=a[i];
}
sort(v+1,v+n+1);
cnt=unique(v+1,v+n+1)-v-1;
CDQ(1,n);
cout<<ans<<"\n";
return 0;
}洛谷-P14436
看到这种题目,第一时间就能想到 可作为两维。
根据题目可以得出,我们需要找到两个点组成一个矩形,且其中不包含别的点。
____________
| | · |
| · | |
| | · |
|___·__|______|
如图,很显然我们可以将矩形分为两半。那么我们假设右半边存在一个点,显然,在左半边低于右半边的某点,一定不可以与比右半边点高且右的点。同时我们发现:在左边的点若想与同一个点连接,那么必然满足 逐渐向下且逐渐向右的特性。于是我们就可以利用 cdq 分治来找点的顺序,用单调栈来找到点的数量,当然愿意线段树的也可以用线段树写。
计算答案就用二分就好了,这个显然不难。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=2e5+5;
struct node{
int a,b;
}a[NA],tmp[NA];
int n,ans,st1[NA],st2[NA];
bool cmp(node a,node b){
return a.a<b.a;
}
int search(int v,int t){
int l=0,r=t+1;
while(l+1<r){
int mid=(l+r)>>1;
if(a[st1[mid]].b>=v) r=mid;
else l=mid;
}
return t-r+1;
}
void CDQ(int l,int r){
if(l==r) return ;
int mid=(l+r)>>1;
int M=a[mid].a,p=l,q=mid+1,tot=l;
CDQ(l,mid);
CDQ(mid+1,r);
while(p<=mid&&q<=r){
if(a[p].b<=a[q].b){
tmp[tot++]=a[p++];
}
else{
tmp[tot++]=a[q++];
}
}
while(p<=mid) tmp[tot++]=a[p++];
while(q<=r) tmp[tot++]=a[q++];
int t1=0,t2=0;
for(int i=l;i<=r;i++){
a[i]=tmp[i];
if(a[i].a<=M){
while(t1&&a[i].a>a[st1[t1]].a) t1--;
st1[++t1]=i;
}
else{
while(t2&&a[i].a<a[st2[t2]].a) t2--;
st2[++t2]=i;
ans+=search(a[st2[t2-1]].b,t1);
}
}
return ;
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].a>>a[i].b;
}
sort(a+1,a+n+1,cmp);
CDQ(1,n);
cout<<ans<<"\n";
return 0;
}洛谷-P3364
思路类似于 P4093,我们显然可以 DP,连转移公式都一样,补充几个错误点:
- 一定要排序
- 最后一定要还原排序
- cdq-dp-cdq 顺序处理
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e6+5;
struct node{
int a,b,c,d;
}a[NA],tmp[NA];
bool cmp(node a,node b){
return a.a<b.a;
}
bool cmp1(node a,node b){
return a.d<b.d;
}
bool cmp2(node a,node b){
return a.b<b.b;
}
int op,n,cnt,tr[NA],ans[NA],v[NA];
int lowbit(int x){
return x&-x;
}
void modify(int x,int v){
while(x<=cnt){
tr[x]=max(tr[x],v);
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans=max(ans,tr[x]);
x-=lowbit(x);
}
return ans==0?-1e9:ans;
}
void del(int x){
while(x<=cnt){
tr[x]=0;
x+=lowbit(x);
}
}
void CDQ(int l,int r){
if(l==r){
return ;
}
int mid=(l+r)>>1;
CDQ(l,mid);
sort(a+l,a+mid+1,cmp1);
sort(a+mid+1,a+r+1,cmp2);
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r){
if(a[p].d<=a[q].b){
modify(a[p].c,ans[a[p].a]);
tmp[tot++]=a[p++];
}
else{
ans[a[q].a]=max(ans[a[q].a],query(a[q].d)+1);
tmp[tot++]=a[q++];
}
}
while(p<=mid){
modify(a[p].c,ans[a[p].a]);
tmp[tot++]=a[p++];
}
while(q<=r){
ans[a[q].a]=max(ans[a[q].a],query(a[q].d)+1);
tmp[tot++]=a[q++];
}
for(int i=l;i<=mid;i++){
del(a[i].c);
}
for(int i=l;i<=r;i++){
a[i]=tmp[i];
}
sort(a+l,a+r+1,cmp);
CDQ(mid+1,r);
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].a>>a[i].b>>a[i].c>>a[i].d;
v[i*3-2]=a[i].b;
v[i*3-1]=a[i].c;
v[i*3]=a[i].d;
ans[i]=1;
}
sort(v+1,v+n*3+1);
cnt=unique(v+1,v+n*3+1)-v-1;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
a[i].a=i;
a[i].b=lower_bound(v+1,v+cnt+1,a[i].b)-v;
a[i].c=lower_bound(v+1,v+cnt+1,a[i].c)-v;
a[i].d=lower_bound(v+1,v+cnt+1,a[i].d)-v;
}
CDQ(1,n);
int res=0;
for(int i=1;i<=n;i++) res=max(res,ans[i]);
cout<<res<<"\n";
return 0;
}洛谷-P11197
cdq 分治板题,显然序列 T 不重要,因为如果后三个满足,那么 T 满足,可以,不满足,后面的 x,y 换一个顺序就可以了。
#include<bits/stdc++.h>
using namespace std;
const int NA=5e5+5;
struct node{
int a,b,c;
}a[NA],tmp[NA];
bool cmp(node x,node y){
if(x.a!=y.a) return x.a<y.a;
if(x.b!=y.b) return x.b<y.b;
return x.c<y.c;
}
int n,k,cnt,tr[NA];
long long ans;
int lowbit(int x){
return x&-x;
}
void modify(int x,int v){
while(x<=n){
tr[x]+=v;
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x){
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
void CDQ(int l,int r){
if(l==r) return ;
int mid=(l+r)>>1;
CDQ(l,mid);
CDQ(mid+1,r);
int p=l,q=mid+1,tot=l;
while(p<=mid&&q<=r){
if(a[p].b<=a[q].b){
modify(a[p].c,1);
tmp[tot++]=a[p++];
}
else{
ans+=query(a[q].c);
tmp[tot++]=a[q++];
}
}
while(p<=mid){
modify(a[p].c,1);
tmp[tot++]=a[p++];
}
while(q<=r){
ans+=query(a[q].c);
tmp[tot++]=a[q++];
}
for(int i=l;i<=mid;i++){
modify(a[i].c,-1);
}
for(int i=l;i<=r;i++){
a[i]=tmp[i];
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[x].a=i;
}
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[x].a=i;
}
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[x].b=i;
}
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[x].c=i;
}
sort(a+1,a+n+1,cmp);
CDQ(1,n);
cout<<ans;
return 0;
} AtCoder-ARC159F
洛谷-P3120
我们定义 记为到点 时的总方案数。所以我们有:
显然不能暴力求 ,考虑 分治。我们显然可以将横纵坐标看作一二维,颜色看作第三维。
然而颜色的表达式不是平常的形态。
我们显然知道应当先递归左区间,求值,然后递归右半区间。
的值显然是左上区间的和减去左上区间的颜色相同的和,那么可以维护这两个值。
我们依旧是划为左右两个区间,左区间由于处理过了,所以直接加,右区间处理后再加就好了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=562505;
const int MA=755;
const int mod=1e9+7;
struct node{
int x,y,c,dp;
}a[NA],tmp[NA];
int n,cnt,sum[NA];
int r,c,k;
void CDQ(int l,int r){
int rl=a[l].x;
int rr=a[r].x;
if(rl==rr) return ;
int midr=(rl+rr)>>1;
int mid=midr*c;
CDQ(l,mid);
for(int i=l;i<=r;i++){
tmp[i]=a[i];
}
sort(tmp+l,tmp+r+1,[midr](const node &a,const node &b){
if(a.y!=b.y) return a.y<b.y;
return a.x>b.x;
});
int sa=0;
for(int i=l;i<=r;i++){
if(tmp[i].x<=midr){
sa+=tmp[i].dp;
if(sa>=mod) sa-=mod;
sum[tmp[i].c]+=tmp[i].dp;
if(sum[tmp[i].c]>=mod) sum[tmp[i].c]-=mod;
}
else{
int sam=sum[tmp[i].c];
int v=(sa-sam+mod)%mod;
tmp[i].dp=(tmp[i].dp+v)%mod;
}
}
for(int i=l;i<=r;i++){
int id=(tmp[i].x-1)*c+tmp[i].y;
a[id].dp=tmp[i].dp;
}
for(int i=l;i<=r;i++){
if(tmp[i].x<=midr){
sum[tmp[i].c]=0;
}
}
CDQ(mid+1,r);
}
signed main(){
cin>>r>>c>>k;
n=r*c;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
int x;
cin>>x;
cnt=(i-1)*c+j;
a[cnt].x=i;
a[cnt].y=j;
a[cnt].c=x;
a[cnt].dp=0;
}
}
a[1].dp=1;
CDQ(1,n);
cout<<a[n].dp;
return 0;
}洛谷-P2487
我们显然是计算个数除以总个数。
#include<bits/stdc++.h>
using namespace std;
const int NA=5e4+5;
struct node{
int h,v,id,f;
double g;
}a[NA],b[NA],t[NA];
int n,m,cnt,trb[NA];
double trs[NA];
int v[NA];
int lowbit(int x){
return x&-x;
}
void modify(int x,int v,double V){
while(x<=m){
if(v>trb[x]){
trb[x]=v;
trs[x]=V;
}
else if(v==trb[x]){
trs[x]+=V;
}
x+=lowbit(x);
}
}
pair<int,double> query(int x){
int ans=0;
double res=0;
while(x){
if(trb[x]>ans){
ans=trb[x];
res=trs[x];
}
else if(trb[x]==ans){
res+=trs[x];
}
x-=lowbit(x);
}
return {ans,res};
}
void del(int x){
while(x<=m){
trb[x]=0;
trs[x]=0.0;
x+=lowbit(x);
}
}
void CDQ(node a[],int l,int r,bool rev){
if(l==r){
return ;
}
int mid=(l+r)>>1;
CDQ(a,l,mid,rev);
for(int i=l;i<=r;i++){
t[i]=a[i];
}
sort(t+l,t+r+1,[&](const node &x,const node &y){
if(x.h!=y.h) return rev?x.h<y.h:x.h>y.h;
return x.id<y.id;
});
for(int i=l;i<=r;i++){
int rk=t[i].v;
if(!rev) rk=m+1-rk;
if(t[i].id<=mid) modify(rk,t[i].f,t[i].g);
else{
int best;
double sum;
pair<int,double> qw=query(rk);
best=qw.first;
sum=qw.second;
int idx=t[i].id;
if(best+1>a[idx].f){
a[idx].f=best+1;
a[idx].g=sum;
}
else if(best+1==a[idx].f){
a[idx].g+=sum;
}
}
}
for(int i=l;i<=r;i++){
if(t[i].id<=mid){
int rk=t[i].v;
if(!rev) rk=m+1-rk;
del(rk);
}
}
CDQ(a,mid+1,r,rev);
return ;
}
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].h>>a[i].v;
a[i].id=i;
a[i].f=1;
a[i].g=1.0;
v[i]=a[i].v;
}
sort(v+1,v+n+1);
m=unique(v+1,v+n+1)-v-1;
for(int i=1;i<=n;i++){
a[i].v=lower_bound(v+1,v+m+1,a[i].v)-v;
}
CDQ(a,1,n,0);
for(int i=1;i<=n;i++){
b[i].h=a[n-i+1].h;
b[i].v=a[n-i+1].v;
b[i].id=i;
b[i].f=1;
b[i].g=1.0;
}
CDQ(b,1,n,1);
int L=0;
double tot=0.0;
for(int i=1;i<=n;i++){
L=max(L,a[i].f);
}
for(int i=1;i<=n;i++){
if(a[i].f==L){
tot+=a[i].g;
}
}
cout<<L<<"\n";
for(int i=1;i<=n;i++){
if(a[i].f+b[n-i+1].f-1==L){
double p=a[i].g*b[n-i+1].g/tot;
printf("%.5lf ",p);
}
else{
cout<<"0.00000 ";
}
}
return 0;
}