Heap Partition ZOJ - 3963(贪心)

ZOJ - 3963

 

贪心做一下就好了

反正别用memset

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 110000, INF = 0x7fffffff;


struct node
{
    int w, id;
    node(int w, int id) : w(w), id(id) {}
};
bool operator < (node a, node b)
{
    if(a.w == b.w) return a.id < b.id;
    return a.w < b.w;
}


set<node> s;
vector<int> G[maxn];
int b[maxn];
int ans[maxn];


int main()
{
    int T;
    rd(T);
    while(T--)
    {
        s.clear();
        int n;
        rd(n);
        int a, cnt = 0;
        for(int i = 0; i <= n; i++) G[i].clear(), ans[i] = 0;
        set<node>::iterator it;
        for(int i = 1; i <= n; i++)
        {
            rd(a);
            it = s.lower_bound(node(-a, -i));
            if(it == s.end())
            {
                G[cnt].push_back(i);
                b[i] = cnt++;
            }
            else
            {
                node x = *it;
                G[b[-x.id]].push_back(i);
                b[i] = b[-x.id];
                if(++ans[-x.id] == 2)
                    s.erase(it);
            }
            s.insert(node(-a, -i));

        }
        pd(cnt);
        for(int i = 0; i < cnt; i++)
        {
            printf("%d", G[i].size());
            for(int j = 0; j < G[i].size(); j++)
            {

                printf(" %d", G[i][j]);
            }
            printf("\n");
        }



    }


    return 0;
}

 

posted @ 2019-04-29 16:10  WTSRUVF  阅读(192)  评论(0编辑  收藏  举报