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

Hint

d.构造尽可能少的二叉树结构,孩子节点要大于父节点,

比如样例2中,最少可构造2个,分别是2 4 3和1

输出的是数字在原序列中的位置

s.前面的数字从小到大排序,贪心选择尽可能大的构造

如果找不到比当前数字小的,则当前数字作为根,添加一个堆

样例较大,用set来写,二分查找比较快

 #include <bits/stdc++.h>
using namespace std; const int MAXN = 1e5 + ; struct Node {
int id;
int val;
} a[MAXN]; int fa[MAXN];
int childNum[MAXN];// struct NodeCmp {
bool operator()(const Node &a, const Node &b)
{
if (a.val != b.val) return a.val < b.val;
return a.id < b.id;
}
}; set<Node, NodeCmp> st;//按val排序
vector<int> vt[MAXN];//保存儿子节点
vector<int> vt2;//保存父节点 int setFind(int d)
{
if (fa[d] < ) {
return d;
}
return fa[d] = setFind(fa[d]);
} void setJoin(int x, int y)
{
x = setFind(x);
y = setFind(y);
if (x != y) fa[x] = y;
} int main()
{
int T;
int n;
int i, j;
Node tmp;
set<Node>::iterator it;
int tmp2;// scanf("%d", &T); while (T--) {
//这样初始化超时
//memset(fa, -1, sizeof(fa));
//memset(childNum, 0, sizeof(childNum));
scanf("%d", &n);
memset(fa, -, sizeof(int) * (n + ));
memset(childNum, , sizeof(int) * (n + ));
st.clear();
vt2.clear();
for (i = ; i < n; ++i) {
scanf("%d", &a[i].val);
a[i].id = i + ;
it = st.upper_bound(a[i]);
if (it == st.begin()) {//
st.insert(a[i]);
vt2.push_back(a[i].id);
vt[a[i].id].push_back(a[i].id);
} else {
tmp = *(--it);
setJoin(a[i].id, tmp.id);
++childNum[tmp.id];
if (childNum[tmp.id] >= ) {
st.erase(tmp);
} vt[setFind(tmp.id)].push_back(a[i].id);//加到根节点孩子列表
st.insert(a[i]);
}
} printf("%d\n", vt2.size());
for (i = ; i < vt2.size(); ++i) {
tmp2 = vt2[i];//根节点
printf("%d", vt[tmp2].size());
printf(" %d", vt[tmp2][]);//根节点
for (j = ; j < vt[tmp2].size(); ++j) {//孩子节点
printf(" %d", vt[tmp2][j]);
}
printf("\n");
vt[tmp2].clear();//在这里清空比较好
}
} return ;
}

下面这个树状数组的没看懂,

思路:贪心,对于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;
 #include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} 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=;
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=,r=b[x]-;
while(l<=r)
{
int mid=(l+r)>>;
if(query(b[x]-)-query(mid-)>)l=mid+;
else r=mid-;
}
if(l-<=)return -;
return p[l-];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)read(po[i].a),po[i].id=i,ve[i].clear(),sum[i]=;
sort(po+,po+n+,cmp);
for(int i=;i<=n;i++)b[po[i].id]=i,p[i]=po[i].id;
int ans=;
for(int i=;i<=n;i++)
{
int pos=solve(i);
if(pos==-)ans++,vis[i]=ans,ve[ans].push_back(i);
else vis[i]=vis[pos],ve[vis[i]].push_back(i),update(b[pos],-);
update(b[i],);
}
printf("%d\n",ans);
for(int i=;i<=ans;i++)
{
int len=ve[i].size();
printf("%d",len);
for(int j=;j<len;j++)printf(" %d",ve[i][j]);puts("");
}
}
return ;
}

zoj 3963 Heap Partition(并查集,贪心,二分)的更多相关文章

  1. ZOJ 3963 Heap Partition set维护。给一个序列,将其划分成尽量少的序列,使每一个序列满足按照顺序构造二叉树,父母的值<=孩子的值。

    Heap Partition Time Limit: Seconds Memory Limit: KB Special Judge A sequence S = {s1, s2, ..., sn} i ...

  2. ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解

    题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multi ...

  3. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  4. [POJ2054]Color a Tree (并查集+贪心)

    POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...

  5. hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)

    Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  6. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  7. 利用并查集+贪心解决 Hdu1232

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. POJ_1456 Supermarket 【并查集/贪心】

    一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...

  9. POJ1456:Supermarket(并查集+贪心)

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接 ...

随机推荐

  1. PHP多线程pthreads

    Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | EmailPHP 高级 ...

  2. python函数回顾:abs()

    函数:abs() 官方英文文档解释 abs(x) Return the absolute value of a number. The argument may be a plain or long ...

  3. 纯手写wcf代码,wcf入门,wcf基础教程

    1.定义服务协定     =>定义接口 using System.ServiceModel; namespace WcfConsole { /// <summary> /// 定义服 ...

  4. F110的几个功能

    1.F-59, 没有找到函数, 使用BDC BAPI_ACC_DOCUMENT_POST 必须创建有借贷2 line 的凭证,需求要参考原始的SA类型凭证, 创建一个单条的 科目 = 供应商 的凭证, ...

  5. 解决网络 下载 句柄无效。 (异常来自 HRESULT:0x80070006 (E_HANDLE))

    首先要共享该文件 其次 在安全里加IISSHARED 是IIS账号 下载代码 #region 下载        /// <summary>        /// 下载        // ...

  6. 爬虫五 Beautifulsoup模块

    一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...

  7. PL/SQL连接ORACLE失败,ORA-12154: TNS: could not resolve the connect identifier specified

    项目需要使用ORACLE,安装了oracle之后,使用PL/SQL连接,先是提示NOT logger  ,后续不知道改了什么提示解析服务器id失败,重新装了之后更狠的直接来了个空白提示 一.安装PLS ...

  8. 【转】Python爬虫_示例2

    爬虫项目:爬取并筛选拉钩网职位信息自动提交简历   一 目标站点分析 #一:实验前准备: 浏览器用Chrome 用Ctrl+Shift+Delete清除浏览器缓存的Cookie 打开network准备 ...

  9. Python基础(9)_生成器(yield表达式形式)、面向过程编程

    一.yield表达式形式 1 #装饰器,初始化含yield表达式的生成器 def init(func): def wrapper(*args,**kwargs): g=func(*args,**kwa ...

  10. JDK1.8(JRE)和eclipse-jee不匹配解决放

    想要用eclipse-jee的话,需要jdk1.8一下版本才能用. 1.需要下载jdk1.7 2.把jdk1.7安装(不需要设置环境变量). 3.在项目上右击选择properties 4.选择Java ...