zoukankan      html  css  js  c++  java
  • BZOJ2212: [Poi2011]Tree Rotations

    题解:因为是二叉树 所以我们贪心的考虑是否可以交换 然后用线段树合并并维护逆序对O(nlogn)

    /**************************************************************
        Problem: 2212
        User: c20161007
        Language: C++
        Result: Accepted
        Time:8872 ms
        Memory:179536 kb
    ****************************************************************/
     
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <cmath>
    #include <set>
    #include <map>
    #define mp make_pair
    #define pb push_back
    #define pii pair<int,int>
    #define link(x) for(edge *j=h[x];j;j=j->next)
    #define inc(i,l,r) for(int i=l;i<=r;i++)
    #define dec(i,r,l) for(int i=r;i>=l;i--)
    const int MAXN=4e5+10;
    const double eps=1e-8;
    #define ll long long
    using namespace std;
    struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
    void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;}
    ll read(){
        ll x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return x*f;
    }
    int a[MAXN];
    typedef struct Q{
        int ch[2],key,rt;
    }Q;
    Q que[MAXN];
    int cnt,cnt1,n,temp;
    void newnode(int &x,int key){
        x=++cnt;que[x].ch[0]=que[x].ch[1]=que[x].rt=0;que[x].key=key;
    }
    queue<int>q;
    typedef struct node{
        int l,r,sz;
    }node;
    node d[MAXN*32];
    void up(int rt){d[rt].sz=d[d[rt].l].sz+d[d[rt].r].sz;}
    void update(int &x,int l,int r,int t){
        if(!x)x=++cnt1;
        if(l==r){d[x].sz=1;return ;}
        int mid=(l+r)>>1;
        if(t<=mid)update(d[x].l,l,mid,t);
        else update(d[x].r,mid+1,r,t);
        up(x);
    }
    ll ans1,sum;
    void merge(int &x,int y,int l,int r,int t){
        //cout<<x<<":::"<<y<<" "<<l<<" "<<r<<" "<<t<<endl;
        if(!y)return ;
        if(!x){x=y;ans1+=d[y].sz*t;return ;}
        int mid=(l+r)>>1;
        merge(d[x].l,d[y].l,l,mid,t);
        merge(d[x].r,d[y].r,mid+1,r,t-d[d[x].l].sz+d[d[y].l].sz);
        up(x);
        //cout<<l<<" "<<r<<" "<<d[x].sz<<endl;
    }
    void built(int &x){
        newnode(x,a[cnt+1]);
        if(que[x].key)return ;
        built(que[x].ch[0]);
        built(que[x].ch[1]);
    }
    void dfs(int x){
        if(!x)return ;
        //cout<<x<<" "<<que[x].key<<endl;
        dfs(que[x].ch[0]);
        dfs(que[x].ch[1]);
        if(que[x].key){update(que[x].rt,1,n,que[x].key);return ;}
        ans1=0;ll t=1ll*d[que[que[x].ch[0]].rt].sz*d[que[que[x].ch[1]].rt].sz;
        //cout<<x<<"==="<<t<<endl;
        merge(que[que[x].ch[0]].rt,que[que[x].ch[1]].rt,1,n,d[que[que[x].ch[0]].rt].sz);
        sum+=min(ans1,t-ans1);que[x].rt=que[que[x].ch[0]].rt;
    }
    int main(){
        n=read();
        inc(i,1,2*n-1)a[i]=read();
        temp=cnt=0;built(temp);
        dfs(temp);
        printf("%lld
    ",sum);
        return 0;
    }
    

      

    2212: [Poi2011]Tree Rotations

    Time Limit: 20 Sec  Memory Limit: 259 MB
    Submit: 2231  Solved: 919
    [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

     

  • 相关阅读:
    搭建Java环境
    【leetcode】257. 二叉树的所有路径
    【leetcode】563. 二叉树的坡度
    【leetcode】401. 二进制手表
    【leetcode】859. 亲密字符串
    【leetcode】1441. 用栈操作构建数组
    【leetcode】1502. 判断能否形成等差数列
    【leetcode】605. 种花问题
    【leetcode】1252. 奇数值单元格的数目
    【leetcode】1640. 能否连接形成数组
  • 原文地址:https://www.cnblogs.com/wang9897/p/9957316.html
Copyright © 2011-2022 走看看