您的位置:首页 > 其它

BZOJ 2212([Poi2011]Tree Rotations-启发式合并)

2013-06-23 20:42 363 查看

2212: [Poi2011]Tree Rotations

Time Limit: 20 Sec Memory Limit: 259 MB

Submit: 322 Solved: 93

[Submit][Status][Discuss]

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

第3次做POI的题??

大家可以看到本题较坑,n不是结点数是叶结点数(自己眼残还敢吐槽??)

好吧……我反省

容易发现这题和dispatching看起来差不多

其实做法也差不多,做的时候顺便统计一下有几个结点小于当前结点(从大到小-不重算)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (2*200000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
int n,root=0;
struct node
{
int fa,ch[2],size,c;
node():size(0),c(0){ch[0]=ch[1]=fa=0;}
}a[MAXN];
void update(int x){a[x].size=a[a[x].ch[0]].size+a[a[x].ch[1]].size+(a[x].c>0);}
int tail=0;
void pushdown(int x){a[a[x].ch[0]].fa=a[a[x].ch[1]].fa=x;}
void build(int &x)
{
if (!x) x=++tail;
scanf("%d",&a[x].c);
if (a[x].c==0)
{
build(a[x].ch[0]);
build(a[x].ch[1]);
update(x);pushdown(x);
}else a[x].size=1;
}
void rotate(int x)
{
int y=a[x].fa,z=a[y].fa;
bool p=a[y].ch[0]==x;
if (z)
{
if (a[z].ch[0]==y) a[z].ch[0]=x;
else a[z].ch[1]=x;
}
a[x].fa=z,a[y].fa=x;
if (a[x].ch[p]) a[a[x].ch[p]].fa=y;
a[y].ch[p^1]=a[x].ch[p];
a[x].ch[p]=y;
update(y);
}
void splay(int x)
{
while (a[x].fa)
{
//rotate(x);

int y=a[x].fa,z=a[y].fa;
if (z)
if ((a[y].ch[0]==x)^(a[z].ch[0]==y)) rotate(x);
else rotate(y);
rotate(x);

//update(x);
}
update(x);
}
void ins(long long &tot,int x,int y)
{
a[x].size++;
if (a[y].c<=a[x].c)
{
if (a[x].ch[0]) ins(tot,a[x].ch[0],y);
else a[y].fa=x,splay(a[x].ch[0]=y);
}
else
{
tot+=a[a[x].ch[0]].size+(a[x].c>0);
if (a[x].ch[1]) ins(tot,a[x].ch[1],y);
else a[y].fa=x,splay(a[x].ch[1]=y);
}
}
int q[MAXN],size;
void clac(int x,int y)
{
if (a[y].ch[0]) clac(x,a[y].ch[0]);
if (a[y].c) q[++size]=y;
if (a[y].ch[1]) clac(x,a[y].ch[1]);
}
long long merge(bool &lor,int z)
{
//long long ans=0;
int x=a[z].ch[0],y=a[z].ch[1];
//bool p=0; //0< 1>
if (a[x].size<a[y].size) swap(x,y);//,p=1,lor=1;

a[x].fa=0;a[y].fa=0;q[1]=y;
size=0;clac(x,y);
long long tot=0;
ForD(i,size)
{
int now=q[i];
a[now].ch[0]=a[now].ch[1]=a[now].fa=0;a[now].size=1;
ins(tot,x,now);
//if (!p) ans+=siz-tot;else ans+=tot;
x=now;
}
a[x].fa=z;
a[z].ch[0]=0,a[z].ch[1]=x;
return tot;
}
long long qur(int &x)
{
if (a[x].c) return 0;
else
{
long long lson=a[a[x].ch[0]].size,rson=a[a[x].ch[1]].size,ls=qur(a[x].ch[0]),rs=qur(a[x].ch[1]);
bool lor=0;
long long ms=merge(lor,x);
/*
if (a[x].fa)
if (a[a[x].fa].ch[0]==x) a[a[x].fa].ch[0]=(lor)?a[x].ch[1]:a[x].ch[0];
else a[a[x].fa].ch[1]=(lor)?a[x].ch[1]:a[x].ch[0];
*/
return ls+rs+min(lson*rson-ms,ms);
}

}
int main()
{
//	freopen("bzoj2212.in","r",stdin);
//	freopen(".out","w",stdout);
scanf("%d",&n);
build(root);
cout<<qur(root)<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: