BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations
Time Limit: 20 Sec Memory Limit: 259 MB
Submit: 391 Solved: 127
[Submit][Status]
Description
Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some interesting features: The tree consists of straight branches, bifurcations and leaves. The trunk stemming from the ground is also a branch. Each branch ends with either a bifurcation or a leaf on its top end. Exactly two branches fork out from a bifurcation at the end of a branch - the left branch and the right branch. Each leaf of the tree is labelled with an integer from the range . The labels of leaves are unique. With some gardening work, a so called rotation can be performed on any bifurcation, swapping the left and right branches that fork out of it. The corona of the tree is the sequence of integers obtained by reading the leaves' labels from left to right. Byteasar is from the old town of Byteburg and, like all true Byteburgers, praises neatness and order. He wonders how neat can his tree become thanks to appropriate rotations. The neatness of a tree is measured by the number of inversions in its corona, i.e. the number of pairs(I,j), (1< = I < j < = N ) such that(Ai>Aj) in the corona(A1,A2,A3…An).
The original tree (on the left) with corona(3,1,2) has two inversions. A single rotation gives a tree (on the right) with corona(1,3,2), which has only one inversion. Each of these two trees has 5 branches. Write a program that determines the minimum number of inversions in the corona of Byteasar's tree that can be obtained by rotations.
现在有一棵二叉树,所有非叶子节点都有两个孩子。在每个叶子节点上有一个权值(有n个叶子节点,满足这些权值为1..n的一个排列)。可以任意交换每个非叶子节点的左右孩子。
要求进行一系列交换,使得最终所有叶子节点的权值按照遍历序写出来,逆序对个数最少。
Input
In the first line of the standard input there is a single integer (2< = N < = 200000) that denotes the number of leaves in Byteasar's tree. Next, the description of the tree follows. The tree is defined recursively: if there is a leaf labelled with ()(1<=P<=N) at the end of the trunk (i.e., the branch from which the tree stems), then the tree's description consists of a single line containing a single integer , if there is a bifurcation at the end of the trunk, then the tree's description consists of three parts: the first line holds a single number , then the description of the left subtree follows (as if the left branch forking out of the bifurcation was its trunk), and finally the description of the right subtree follows (as if the right branch forking out of the bifurcation was its trunk).
第一行n
下面每行,一个数x
如果x==0,表示这个节点非叶子节点,递归地向下读入其左孩子和右孩子的信息,
如果x!=0,表示这个节点是叶子节点,权值为x
1<=n<=200000
Output
In the first and only line of the standard output a single integer is to be printed: the minimum number of inversions in the corona of the input tree that can be obtained by a sequence of rotations.
一行,最少逆序对个数
Sample Input
0
0
3
1
2
Sample Output
HINT
Source
题解:
我的做法是这样的:(虽然被卡成翔。。。)
因为左子树+右子树构成的逆序对=左子树内部的逆序对+右子树内部的逆序对+左子树与右子树之间的逆序对
而前两部分仅通过交换左右子树是不会改变的,所以我们只要贪心的去看 交换后 左右子树之间的逆序对是否会减少就行,
减少了就交换,然后递归下去解决左子树和右子树。
在这个过程中我们用了一种方法,类似于NOI2007货币交换的方法,
先用 x 排序,然后就可以 O(n)算出逆序对,
然后按 y 分为两部分,递归下去
然后按 x 将两部分归并回来,使得仍保持原数组有序。
然后交上去T成翔啊。。。
想了想忽然发现 这是会被卡成o(n^2)的啊!如果这棵树极度不平衡!
先贴上我的代码,然后去膜拜题解。。。
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 1000000+10000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
ll n,tot,cnt=,v[maxn],ls[maxn],rs[maxn],s[maxn],sum[],ans[];
ll ret=;
struct rec{int x,y;}a[maxn],b[maxn];
void dfs(int x)
{
v[x]=read();
if(v[x]){s[x]=;a[++tot].x=v[x];a[tot].y=tot;return;};
ls[x]=++cnt;dfs(ls[x]);
rs[x]=++cnt;dfs(rs[x]);
s[x]=s[ls[x]]+s[rs[x]];
}
void solve(int k,int l,int r)
{
int mid=l+s[ls[k]]-,pl=l-,pr=mid;
if(!s[ls[k]])return;
sum[]=sum[]=ans[]=ans[]=;
for2(i,l,r)
{
int x=a[i].y<=mid?:;
sum[x]++;ans[x]+=sum[-x];
}
ret+=min(ans[],ans[]);
for2(i,l,r)if(a[i].y<=mid)b[++pl]=a[i];else b[++pr]=a[i];
for2(i,l,r)a[i]=b[i];
solve(ls[k],l,mid);
solve(rs[k],mid+,r);
pl=l,pr=mid+;
for2(i,l,r)
if((a[pl].x<a[pr].x||pr>r)&&pl<=mid)b[i]=a[pl++];else b[i]=a[pr++];
for2(i,l,r)a[i]=b[i];
}
inline bool cmp(rec a,rec b)
{
return a.x<b.x;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
dfs();
//for1(i,4*n)cout<<ls[i]<<' '<<rs[i]<<' '<<s[i]<<endl;
sort(a+,a+n+,cmp);
solve(,,n);
printf("%lld\n",ret);
return ;
}
神一样的题解,居然是用线段树启发式合并!
:
注意到对于一个非叶子节点,是否交换它的儿子对它之后的统计没有影响,那么只需要统计每个点左右儿子是否交换,并求出每个节点贡献的逆序对数Sum[i],求∑Sum[i]即可;
求Sum[i]的时候,每个点维护一个线段树表示这个点的子树内的权值情况(有无,权值1~n为下标),可以先解决i的左右儿子;这时候只要考虑左右儿子之间对答案的贡献,若不交换左右儿子,贡献就是∑(i>j)(i属于左儿子,j属于右儿子),否则相反;那么我们想到用启发式合并的思想,每次在线段树中插入一个元素x,并且统计答案(具体是∑(size[j]) | j为线段树的右边节点而且x属于线段树的左边节点),那么时间复杂度是 O(Nlog^2N)
更好的方法:直接两颗线段树合并,并顺便统计答案即可(并且贪心考虑在这个节点是不是要交换左右儿子);
时间复杂度的证明:
由于是动态节点,那么对于要合并的两棵树,复杂度正比与他们的公共节点数目;
又因为元素是1~n的排列,不会重复,那么对于线段树每一层的节点单独考虑经过次数,对于第i层,∑size的大小是n,对于每一个节点经过次数不会超过 size大小,由于线段树有LogN层,那么总的节点经过次数是NLogN,又因为线段树节点的Update操作均是常数时间完成的,那么总的时间复杂度是O(NlogN)
太神了!原来逆序对还能这么求!orz!
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 250000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
struct{ll sum,ls,rs;}t[*maxn];
ll n,ret=,tot,ans[];
int build(int l,int r,int x)
{
int k=++tot,mid=(l+r)>>;
t[k].sum=;
if(l==r)return k;
if(x<=mid)t[k].ls=build(l,mid,x);else t[k].rs=build(mid+,r,x);
return k;
}
int merge(int l,int r)
{
if(l*r==)return l+r;
ans[]+=t[t[l].ls].sum*t[t[r].rs].sum;
ans[]+=t[t[l].rs].sum*t[t[r].ls].sum;
t[l].sum+=t[r].sum;
t[l].ls=merge(t[l].ls,t[r].ls);
t[l].rs=merge(t[l].rs,t[r].rs);
return l;
}
int work()
{
int x=read();
if(x)return build(,n,x);
int l=work(),r=work();
ans[]=ans[]=;
if(t[l].sum<t[r].sum)swap(l,r);
x=merge(l,r);
ret+=min(ans[],ans[]);
return x;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
work();
printf("%lld\n",ret);
return ;
}
BZOJ2212: [Poi2011]Tree Rotations的更多相关文章
- BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对
原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...
- BZOJ2212 [Poi2011]Tree Rotations 【线段树合并】
题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...
- BZOJ2212 [POI2011] Tree Rotations 【treap】
题目分析: 写的无旋treap应该跑不过,但bzoj判断的总时限.把相关实现改成线段树合并就可以了. 代码: #include<bits/stdc++.h> using namespace ...
- bzoj2212[Poi2011]Tree Rotations [线段树合并]
题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- POI2011 Tree Rotations
POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...
- 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并
原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html 题目描述 Byteasar the gardener is growing a rare tr ...
随机推荐
- 基准测试之netperf
使用 server端 [root@jiangyi01.sqa.zmf /home/ahao.mah/ALIOS_QA/tools/netperf] #netserver -p 10001 Starti ...
- JS浏览器对象-window对象
代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- Linux 常用命令记录
1.查看磁盘空间使用情况 df -[a i m] 或更多 df -lh 2.查看目录文件占用大小 du -sh * du --max-depth=1 -lh 3.内存使用qingkuang free ...
- Javascript:数组和字符串的相互转化
中午吃饭的时候,和室友讨论前端的问题,然后一个有趣的问题被抛出来: javascript用什么方法可以把“hello world”位置反转输出,即输出:"dlrow olleh"? ...
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
1.UITapGestureRecognizer 点击/双击手势 代码如下: var tapGesture = UITapGestureRecognizer(target: self, action: ...
- Android获取屏幕分辨率及DisplayMetrics简介
Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题. 手机的分辨率信息是手机的一项重要信息,很好的是,Android ...
- c#中的面向对象基础知识总结
面向对象 1.面向过程----->面向对象 1. 面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作. 面向对象:意在写出一个通用的代码,屏蔽差异. 我们在代码中描述一个对象,通 ...
- 活动指示器UIActivityIndicatorView
活动指示器UIActivityIndicatorView可以告知用户有一个操作正在进行中 1.创建 UIActivityIndicatorView *activityIndicatorView ...
- Popular Cows (POJ No.2186)
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- JQuery原理介绍及学习方法
前言 对于JQuery,想必大家都很熟悉.目前,很多web项目,在实施的过程中,考虑到各浏览器原生JS API的兼容性,大都会选用JQuery或类似于JQuery这样的框架来进行网页效果开发.JQue ...