zoukankan      html  css  js  c++  java
  • The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

    Heap Partition

    Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

    A sequence S = {s1s2, ..., sn} is called heapable if there exists a binary tree T with n nodes such that every node is labelled with exactly one element from the sequence S, and for every non-root node si and its parent sjsj ≤ si and j < i hold. Each element in sequence S can be used to label a node in tree T only once.

    Chiaki has a sequence a1a2, ..., an, she would like to decompose it into a minimum number of heapable subsequences.

    Note that a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

    Input

    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

    The first line contain an integer n (1 ≤ n ≤ 105) — the length of the sequence.

    The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ n).

    It is guaranteed that the sum of all n does not exceed 2 × 106.

    Output

    For each test case, output an integer m denoting the minimum number of heapable subsequences in the first line. For the next m lines, first output an integer Ci, indicating the length of the subsequence. Then output Ci integers Pi1Pi2, ..., PiCi in increasing order on the same line, where Pij means the index of the j-th element of the i-th subsequence in the original sequence.

    Sample Input

    4
    4
    1 2 3 4
    4
    2 4 3 1
    4
    1 1 1 1
    5
    3 2 1 4 1
    

    Sample Output

    1
    4 1 2 3 4
    2
    3 1 2 3
    1 4
    1
    4 1 2 3 4
    3
    2 1 4
    1 2
    2 3 5

    题意:序列 S={s1,s2,...,sn} 被称为 heapable ,当且仅当存在一个二叉树 T , n 个点均作为 T 的一个节点,且满足任意非根节点 si 和其父节点 sj , sjsij<i

    现在有一个序列 a1,a2,...,an ,相应将其分成若干个 heapable 的子序列,问使得子序列个数最少的策略


    题解:参考自别人的代码 贪心+二分 用memset会TLE 直接for一遍清零。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define ll __int64
     4 #pragma comment(linker, "/STACK:102400000,102400000")
     5 int a[100005];
     6 struct node
     7 {
     8     int f,s;
     9 }p;
    10 bool operator <(node x,node y)
    11 {
    12     if(a[x.s]==a[y.s]) return x.s<y.s;
    13     return a[x.s]<a[y.s];
    14 }
    15 set<node> st;
    16 set<node> :: iterator it;
    17 vector<int> v[100005];
    18 int dis[100005];
    19 int main()
    20 {
    21     int t;
    22     scanf("%d",&t);
    23     for(int i=1;i<=t;i++)
    24     {
    25         st.clear();
    26         int cnt=0;
    27         int n;
    28         scanf("%d",&n);
    29         for(int k=0;k<=n;k++)
    30             dis[k]=0;
    31         for(int j=1;j<=n;j++)
    32         {
    33             scanf("%d",&a[j]);
    34             p.s=j;
    35             it=st.upper_bound(p);
    36             if(it==st.begin())
    37             {
    38                 v[cnt].push_back(j);
    39                 p.f=cnt;
    40                 p.s=j;
    41                 st.insert(p);
    42                 cnt++;
    43             }
    44             else
    45             {
    46                 it--;
    47                 p=*it;
    48                 dis[p.s]++;
    49                 if(dis[p.s]==2)
    50                     st.erase(it);
    51                 p.s=j;
    52                 st.insert(p);
    53                 v[p.f].push_back(j);
    54             }
    55         }
    56         printf("%d
    ",cnt);
    57         for(int j=0;j<cnt;j++)
    58         {
    59            printf("%d",v[j].size());
    60            for(int k=0;k<v[j].size();k++)
    61            printf(" %d",v[j][k]);
    62            printf("
    ");
    63            v[j].clear();
    64         }
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    Python定时任务sched(一)
    Python使用selenium进行爬虫(一)
    有关在python中使用Redis(二)
    有关JSOUP学习分享(一)
    jsoup爬虫,项目实战,欢迎收看
    有关在python中使用Redis(一)
    俄罗斯方块代码
    Android !No Launcher activity found!错误
    让jar程序在linux上一直执行(转)
    MyEclipse打包jar 并加入第三方包
  • 原文地址:https://www.cnblogs.com/hsd-/p/6754815.html
Copyright © 2011-2022 走看看