zoukankan      html  css  js  c++  java
  • 2015 Multi-University Training Contest 4 hdu 5338 ZZX and Permutations

    ZZX and Permutations

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 771    Accepted Submission(s): 243


    Problem Description
    ZZX likes permutations.

    ZZX knows that a permutation can be decomposed into disjoint cycles(see https://en.wikipedia.org/wiki/Permutation#Cycle_notation). For example:
    145632=(1)(35)(462)=(462)(1)(35)=(35)(1)(462)=(246)(1)(53)=(624)(1)(53)……
    Note that there are many ways to rewrite it, but they are all equivalent.
    A cycle with only one element is also written in the decomposition, like (1) in the example above.

    Now, we remove all the parentheses in the decomposition. So the decomposition of 145632 can be 135462,462135,351462,246153,624153……

    Now you are given the decomposition of a permutation after removing all the parentheses (itself is also a permutation). You should recover the original permutation. There are many ways to recover, so you should find the one with largest lexicographic order.
     
    Input
    First line contains an integer t, the number of test cases.
    Then t testcases follow. In each testcase:
    First line contains an integer n, the size of the permutation.
    Second line contains n space-separated integers, the decomposition after removing parentheses.

    n105. There are 10 testcases satisfying n105, 200 testcases satisfying n1000.
     
    Output
    Output n space-separated numbers in a line for each testcase.
    Don't output space after the last number of a line.
     
    Sample Input
    2
    6
    1 4 5 6 3 2
    2
    1 2
     
    Sample Output
    4 6 2 5 1 3
    2 1
     
    Author
    XJZX
     
    Source
     
    解题:线段树+set
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 300010;
     4 struct node {
     5     int lt,rt,lazy,maxv;
     6 } tree[maxn<<2];
     7 int d[maxn],val2index[maxn],n;
     8 void pushup(int v) {
     9     tree[v].maxv = max(tree[v<<1].maxv,tree[v<<1|1].maxv);
    10 }
    11 void pushdown(int v) {
    12     if(tree[v].lazy > -1) {
    13         tree[v<<1].lazy = tree[v<<1|1].lazy = tree[v].lazy;
    14         tree[v<<1].maxv = tree[v<<1|1].maxv = tree[v].lazy;
    15         tree[v].lazy = -1;
    16     }
    17 }
    18 void build(int lt,int rt,int v) {
    19     tree[v].lt = lt;
    20     tree[v].rt = rt;
    21     tree[v].lazy = -1;
    22     if(lt == rt) {
    23         tree[v].maxv = d[lt];
    24         return;
    25     }
    26     int mid = (lt + rt)>>1;
    27     build(lt,mid,v<<1);
    28     build(mid+1,rt,v<<1|1);
    29     pushup(v);
    30 }
    31 void update(int lt,int rt,int v) {
    32     if(lt <= tree[v].lt && rt >= tree[v].rt) {
    33         tree[v].lazy = tree[v].maxv = 0;
    34         return;
    35     }
    36     pushdown(v);
    37     if(lt <= tree[v<<1].rt) update(lt,rt,v<<1);
    38     if(rt >= tree[v<<1|1].lt) update(lt,rt,v<<1|1);
    39     pushup(v);
    40 }
    41 int query(int lt,int rt,int v) {
    42     if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].maxv;
    43     pushdown(v);
    44     int ret = 0;
    45     if(lt <= tree[v<<1].rt) ret = max(ret,query(lt,rt,v<<1));
    46     if(rt >= tree[v<<1|1].lt) ret = max(ret,query(lt,rt,v<<1|1));
    47     pushup(v);
    48     return ret;
    49 }
    50 set<int>st;
    51 bool used[maxn];
    52 int ret[maxn];
    53 int main() {
    54     int kase;
    55     scanf("%d",&kase);
    56     while(kase--) {
    57         st.clear();
    58         memset(used,false,sizeof used);
    59         memset(ret,0,sizeof ret);
    60         memset(d,0,sizeof d);
    61         scanf("%d",&n);
    62         for(int i = 1; i <= n; ++i) {
    63             scanf("%d",d+i);
    64             val2index[d[i]] = i;
    65         }
    66         build(0,n,1);
    67         st.insert(0);
    68         for(int i = 1; i <= n; ++i) {
    69             if(ret[i]) continue;
    70             int index = val2index[i],mx = 0;
    71             if(!used[d[index+1]]) mx = max(d[index+1],mx);
    72             auto it = st.lower_bound(index);
    73             if(it != st.begin()) --it;
    74             int val = query((*it),index,1);
    75             mx = max(mx,val);
    76             if(mx == d[index+1]) {
    77                 used[d[index+1]] = true;
    78                 ret[i] = d[index+1];
    79                 update(index+1,index+1,1);
    80                 continue;
    81             }
    82             ret[i] = mx;
    83             for(int i = val2index[mx]; i < index; ++i) {
    84                 ret[d[i]] = d[i+1];
    85                 used[d[i]] = true;
    86             }
    87             update(val2index[mx],index,1);
    88             used[d[index]] = true;
    89             for(int i = val2index[mx]; i <= index; ++i)  st.insert(i);
    90         }
    91         for(int i = 1; i <= n; ++i)
    92             printf("%d%c",ret[i],i==n?'
    ':' ');
    93     }
    94     return 0;
    95 }
    View Code
     
  • 相关阅读:
    oracle客户端连接服务器基本教程
    java中字符串处理、串联和转换的几个常用方法,以及如果需要自己编程实现的具体实施步骤。
    面试相关
    java中byte是什么类型,和int有什么区别
    (华为机试大备战)java。多了解了解最常用的那个类库的方法对处理字符串的方法
    (华为)以下代码片段将创建一个仅保存大写字符的字段。
    (华为)以下代码片段将创建一个仅保存大写字符的字段。
    每个程序中只有一个public类,主类?
    我的第一个长程序,虽然是直接抄了书上,可是还是出现了两次拼写错误,最终还是找到异常的答案,改过来了。
    实践证明:当类想实现两个监听接口的时候,必须把两个都设置成内部类,不可能一个是外部类实现,一个是内部类实现。这样容易捕获错误,出现异常。
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4700087.html
Copyright © 2011-2022 走看看