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 ...
随机推荐
- HDU_2553——n皇后问题,作弊
在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出有多少种合法的放置方法. Inp ...
- 如何将Java Web项目部署到服务器上
转自:(此处更详细)http://blog.csdn.net/gulu_gulu_jp/article/details/50994003 一.前言 前面我们已经尝过了在云服务器上部署代码的甜头了,现在 ...
- 关于bootstrap--导航栏
1.普通导航:class .nav-tabs. <ul class="nav nav-tabs"> <li class="active"> ...
- [Qt] QString 和 char* 转换
(1) QString 转 char* char acResult[10240]; //QByteArray baResult = strResult.toLatin1(); QByteArray b ...
- 限定checkbox最多选中数量
一.概述: checkbox是我们在编写网页的时候经常使用的多选框,但是有些时候我们会限定最多选中的数量,如何限定呢? 下面这例子限定了最多选中两个元素,并且将这两个选中的源依次显示在一个文本框里: ...
- 应用按home键无最近应用
在应用的AndroidManifest里面添加加载模式
- Intel 被 ARM 逼急了
英特尔最近推出基于Silvermont架构Bay Trail系列处理器,相对前一代Bonnell架构的最突出的改进就是支持乱序执行 silvermon架构的处理器将出现在pc,平板等: List of ...
- C# Tips:获得当前登录计算机的用户(本地用户/域用户)
须要using的namespace: using System.Security.Principal; 获得登录计算机的用户: WindowsIdentity windowsIdentity = Wi ...
- IE下判断IE版本语法使用
先摆一下判断IE版本语法 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <![ ...
- jquery之营销系统(会员促销)
var appPath = getAppPath(); var cnt = 0; var loadCnt = 0; $(function() { $("#opreateHtml") ...