zoukankan      html  css  js  c++  java
  • zoj-3963 Heap Partition(贪心+二分+树状数组)

    题目链接:

    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


    题意:给出一个序列,然后要求分成最少多少个子序列,使得每个子序列都满足上面的要求

    思路:贪心,对于a[i],贪心的话就是要在a[1]~a[i-1]中找到一个a[j]做父亲(且a[j]不能超过两个孩子),a[j]<=a[i]&&a[j]>=a[k](1<=任意k<=i-1,k!=j)
       可以离散化,然后二分+树状数组找,线段树会T;
    AC代码:
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    
    int n,a[maxn],vis[maxn],p[maxn],b[maxn],sum[maxn];
    vector<int>ve[maxn];
    struct node
    {
        int a,id;
    }po[maxn];
    int cmp(node x,node y)
    {
        if(x.a==y.a)return x.id<y.id;
        return x.a<y.a;
    }
    inline int lowbit(int x){return x&(-x);}
    inline int query(int x)
    {
        int s=0;
        while(x)
        {
            s+=sum[x];
            x-=lowbit(x);
        }
        return s;
    }
    inline void update(int x,int num)
    {
        while(x<=n)
        {
            sum[x]+=num;
            x+=lowbit(x);
        }
        return ;
    }
    
    
    inline int solve(int x)
    {
        int l=1,r=b[x]-1;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(query(b[x]-1)-query(mid-1)>0)l=mid+1;
            else r=mid-1;
        }
        if(l-1<=0)return -1;
        return p[l-1];
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)read(po[i].a),po[i].id=i,ve[i].clear(),sum[i]=0;
            sort(po+1,po+n+1,cmp);
            for(int i=1;i<=n;i++)b[po[i].id]=i,p[i]=po[i].id;
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                int pos=solve(i);
                if(pos==-1)ans++,vis[i]=ans,ve[ans].push_back(i);
                else vis[i]=vis[pos],ve[vis[i]].push_back(i),update(b[pos],-1);
                update(b[i],2);
            }
            printf("%d
    ",ans);
            for(int i=1;i<=ans;i++)
            {
                int len=ve[i].size();
                printf("%d",len);
                for(int j=0;j<len;j++)printf(" %d",ve[i][j]);puts("");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    Ural 1057. Amount of Degrees
    BZOJ 3517: 翻硬币
    BZOJ 4527: K-D-Sequence
    CTC联结时间分类算法(语音、文本识别)
    我小苏太狼又回来了.
    /*--------------分割线--------------*/
    /*--------------分割线--------------*/
    洛谷 P4149 [IOI2011]Race-树分治(点分治,不容斥版)+读入挂-树上求一条路径,权值和等于 K,且边的数量最小
    Codeforces 161.D. Distance in Tree-树分治(点分治,不容斥版)-树上距离为K的点对数量-蜜汁TLE (VK Cup 2012 Round 1)
    洛谷 P2634 [国家集训队]聪聪可可-树分治(点分治,容斥版) +读入挂+手动O2优化吸点氧才过。。。-树上路径为3的倍数的路径数量
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/6749812.html
Copyright © 2011-2022 走看看