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

3
0
0
3
1
2

Sample Output

1

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的更多相关文章

  1. BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对

    原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...

  2. BZOJ2212 [Poi2011]Tree Rotations 【线段树合并】

    题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...

  3. BZOJ2212 [POI2011] Tree Rotations 【treap】

    题目分析: 写的无旋treap应该跑不过,但bzoj判断的总时限.把相关实现改成线段树合并就可以了. 代码: #include<bits/stdc++.h> using namespace ...

  4. bzoj2212[Poi2011]Tree Rotations [线段树合并]

    题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...

  5. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  6. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

  7. 2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...

  8. POI2011 Tree Rotations

    POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...

  9. 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html 题目描述 Byteasar the gardener is growing a rare tr ...

随机推荐

  1. 【2012长春区域赛】部分题解 hdu4420—4430

    这场比赛特点在于两个简单题太坑,严重影响了心情..导致最后只做出两题....当然也反映出心理素质的重要性 1002: 题意:一个矩阵b[n][n]通过数组 a[n]由以下规则构成,现在已知b[n][n ...

  2. sicily 4433 DAG?

    题意:输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph). 解法:还是深搜 #include<iostream> #include<memory ...

  3. ubuntu下安装pdo扩展

    ubuntu下安装好LAMP后默认情况没有安装mysql_pdo扩展,以下是安装 步聚,在终端输入以下命令 1.pecl search pdo 2.sudo pecl install pdo 当出现E ...

  4. NuGet 问题及小技巧

    在使用NuGet程序包管理时,经常会遇到的一些小问题.比如,联网搜索时间太长,搜索不到常见或想要的程序包,程序包版本兼容问题,想安装制定的版本等. 那么我们可以使用以下的一些小技巧来解决. 1.检查N ...

  5. Redis源代码分析-内存数据结构intset

    这次研究了一下intset.研究的过程中,一度看不下过去,可是还是咬牙挺过来了.看懂了也就是那么回事.静下心来,切莫浮躁 Redis为了追求高效,在存储下做了非常多的优化,像intset就是作者为了节 ...

  6. 【转】asp.net 利用Global.asax 捕获整个解决方案中的异常错误

    之前做项目的时候都是在每个页面中处理这不同的异常信息,一个页面数下来,很多个try{}catch{}语句块,令整个代码结构有些不够美观. 今天看到一篇帖子,是关于利用全局应用程序类来帮忙获取异常信息, ...

  7. html5.边框属性相关知识点

    border-left 定义左边框 border-top 定义上边框 border-right 定义有边框 border-bottom 定义下边框 边框样式: dotted 边框线为点状虚线 dash ...

  8. XML中 添加或修改时 xmlns="" 怎么删除

    //创建节点时 记得加上  ---> xmldoc.DocumentElement.NamespaceURI XmlElement url = xmldoc.CreateElement(&quo ...

  9. 使用Gird++打印出现“Retrieving the COM class factory for component with CLSID”的解决办法

    我们的接口需要返回一个gird++生成PDF文件的二进制数据,在本地测试都很好,发布到服务器上一直出现“Retrieving the COM class factory for component w ...

  10. 找两个string[ ]里不同的元素

    方法 1:string[] strListOne = new string[] {"Type", "Parent Id","Status"} ...