Provincial ZJ2017 F Heap Partition [greedy + weight line segment tree]

A sequence S = {s1, s2, …, 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 sj, sj ≤ 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 a1, a2, …, 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 a1, a2, …, 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 Pi1, Pi2, …, 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
Hint

The meaning of the question: Give you a sequence that allows you to build the minimum number of binary trees. The nodes of the binary tree need to satisfy that the child id is less than the parent id and the child weight is greater than the parent weight. The number of output trees, the nodes of each tree are output in ascending order.

Analysis: There was a problem with the scheduling of L2 trains in the ladder competition before, and it was found that the strategies were basically the same. Put it if you can, choose the one with the closest weight, otherwise, create a new tree.

So the question is, when can it be placed and how to get the placement position faster. And the data is less than 10^5, then you can go directly to the segment tree. Pay attention to the preservation of binary trees!

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 1e5+5;
int a[maxn];
int sum[maxn<<3];
vector<int>G[maxn];


int query(int x,int l,int r,int ll,int rr)
{
    int ans = 0;
    if(l==r){
        if(sum[x]>0){
            return l;
        }
        return 0;
    }else{
        int mid = (l+r)>>1;
        if(rr>=mid+1&&sum[rt]>0){
            ans= max(ans,query(rt,mid+1,r,ll,rr));
        }
        if(ans!=0) return ans;
        if(ll<=mid&&sum[lt]>0){
            ans= max(ans,query(lt,l,mid,ll,rr));
        }
        return ans;
    }
}

void add(int x,int l,int r,int p,int o)
{
    if(l==r&&l==p){
        sum[x]+=o;
        return ;
    }else{
        int mid = (l+r)>>1;
        if(p<=mid) add(lt,l,mid,p,o);
        else add(rt,mid+1,r,p,o);
        sum[x]=sum[rt]|sum[lt];
    }
}

vector<int> t[maxn];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int n;scanf("%d",&n);
        for(int i=1;i<=n;i++) t[i].clear(),G[i].clear();
        for(int i=1;i<=n*4;i++) sum[i]=0;

        int ans=0;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n;i++){
            int m = query(1,1,n,1,a[i]);    
            if(m==0){
                ans++;
                G[a[i]].push_back(ans);
                G[a[i]].push_back(ans);
                t[ans].push_back(i);
            }else{
                add(1,1,n,m,-1);
                int tree=G[m][G[m].size()-1];
                G[m].pop_back();
                t[tree].push_back(i);
                G[a[i]].push_back(tree);
                G[a[i]].push_back(tree);
            }
            add(1,1,n,a[i],2);
        }
        printf("%d\n",ans);
        for(int i=1;i<=ans;i++){
            printf("%d",t[i].size());
            sort(t[i].begin(),t[i].end());
            for(int j=0;j<t[i].size();j++){
                printf(" %d",t[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815024&siteId=291194637