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 ...
随机推荐
- Android学习【Android内核编译流程和错误笔记】
博客:http://blog.csdn.net/muyang_ren Ubuntu14.04 LTS(要求是64位长期支持版LTS) Jdk1.8 内核:android4.0 一:jdk 1.解压jd ...
- MySQL 加密/压缩函数
这些问题可能导致数据值的改变.一般而言,上述问题可能在你使用非二进制串数据类型(如char,varchar,text等数据类型)的情况下发生. AES_ENCRYPT()和AES_DECRYPT() ...
- SPOJ 416 - Divisibility by 15(贪心)
糟烂的代码啊... 这个题目思路很简单——末位只可能为0和5,所有数字的和肯定被3整除 没有0和5的肯定不行 否则,把所有数字求和 如果被3整除,则从大到小输出 如果除3余1,则按以下顺序——删1: ...
- Java基础知识强化53:经典排序之选择排序(SelectionSort)
1.选择排序的原理图: 2. 选择排序代码实现: package cn.itcast_02; /* * 数组排序之选择排序: * 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在 ...
- iptables简述
一.linux防火墙基础防火墙分为硬件防火墙和软件防火墙. 1.概述linux 防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙. 包过滤机制:ne ...
- python:字符串取值
某个字符串为stmp="abcdef54321" 取前面5个stmp[:5] #abcde 取后面5个stmp[-5:] #54321 从前面开始取,不包括最后两个stmp[:-2 ...
- jquery之遍历展示title
//遍历展示title {field:'couponsList',title:'优惠劵类型',width:250,align:'center',sortable:true, formatter:fun ...
- DataGrid( 数据表格) 组件[5]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- (六)《Java编程思想》——初始化及类的加载顺序
package chapter7; /** * 初始化及类的加载顺序:顺序如下 * 1.基类的static变量 * 2.导出类的static变量 * 3.基类的变量 * 4.基类的构造函数 * 5.导 ...
- abstract修饰符,具体类与抽象类的区别
abstract修饰符 abstract可以修饰类,表示一个抽象类,注意:抽象类和具体类唯一的区别:类不能创建对象,而具体类是可以创建对象的 1.具体类有构造方法,抽象类也有构造方法 2.具体类可以有 ...