• bzoj 2212: [Poi2011]Tree Rotations


    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的一个排列)。可以任意交换每个非叶子节点的左右孩子。
    要求进行一系列交换,使得最终所有叶子节点的权值按照遍历序写出来,逆序对个数最少。

    Solution

    实现非常巧妙,在线段树合并时可以直接统计
    具体思想:
    x子树内的逆序对数=左儿子内部的逆序对数+右儿子内部的逆序对数+左右儿子组合的逆序对数
    前两项与左右儿子的顺序无关,只需要决策最后一项即可,一路推到根节点即为答案
    (t1+=s[ls[x]]*s[rs[y]])
    (t2+=s[rs[x]]*s[ls[y]])
    在合并时顺便统计两种决策,最后答案加上 (Min(t1,t2))

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    typedef long long ll;
    const int N=400005;
    int root[N],val[N],totnode=0,cnt=1,ls[N*20],rs[N*20],n;
    ll ans=0,t1=0,t2=0,s[N*20];int L[N],R[N];
    void dfs(int x){
       scanf("%d",&val[x]);
       if(!val[x]){
          L[x]=++cnt;dfs(L[x]);
          R[x]=++cnt;dfs(R[x]);
       }
    }
    void ins(int &rt,int l,int r,int sa){
       if(!rt)rt=++totnode;
       if(l==r){s[rt]=1;return ;}
       int mid=(l+r)>>1;
       if(sa<=mid)ins(ls[rt],l,mid,sa);
       else ins(rs[rt],mid+1,r,sa);
       s[rt]=s[ls[rt]]+s[rs[rt]];
    }
    int merge(int x,int y){
       if(!x)return y;if(!y)return x;
       t1+=s[ls[x]]*s[rs[y]];
       t2+=s[rs[x]]*s[ls[y]];
       ls[x]=merge(ls[x],ls[y]);
       rs[x]=merge(rs[x],rs[y]);
       s[x]=s[ls[x]]+s[rs[x]];
       return x;
    }
    void solve(int x){
       if(!x)return ;
       solve(L[x]);solve(R[x]);
       if(!val[x]){
          t1=0;t2=0;
          root[x]=merge(root[L[x]],root[R[x]]);
          ans+=Min(t1,t2);
       }
    }
    void work()
    {
       scanf("%d",&n);
       dfs(1);
       for(int i=1;i<=cnt;i++)
          if(val[i])ins(root[i],1,n,val[i]);
       solve(1);
       printf("%lld
    ",ans);
    }
    
    int main(){work();return 0;}
    
    
  • 相关阅读:
    Webpack4不求人(5) ——编写自定义插件
    Webpack4不求人(4)——编写自定义Loader
    Webpack4不求人(3) ——性能优化
    Webpack4不求人(2) ——手把手搭建TypeScript+React16+ReactRouter5同构应用脚手架
    Shell脚本快速入门(1)
    kafka二进制协议分析与PHP客户端开发
    深入浅出ES6的标准内置对象Proxy
    ES6的Set类型
    深入浅出ES6的迭代器
    Javascript事件系统
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7662818.html
Copyright © 2020-2023  润新知