zoj 3963 Heap Partition(并查集,贪心,二分)
Heap Partition
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
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
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(并查集,贪心,二分)的更多相关文章
- ZOJ 3963 Heap Partition set维护。给一个序列,将其划分成尽量少的序列,使每一个序列满足按照顺序构造二叉树,父母的值<=孩子的值。
Heap Partition Time Limit: Seconds Memory Limit: KB Special Judge A sequence S = {s1, s2, ..., sn} i ...
- ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解
题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multi ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- [POJ2054]Color a Tree (并查集+贪心)
POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...
- 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 ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 利用并查集+贪心解决 Hdu1232
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ_1456 Supermarket 【并查集/贪心】
一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
随机推荐
- SQLServer中行列转换Pivot UnPivot
PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...
- adobe flash player升级coredump分析
flash player版本号:14.0.0.125 产品名称:Adobe® Flash® Player Installer/Uninstaller 系统:windows xp sp3 调试器:win ...
- SQL语句中的Having子句与where子句
一.介绍 聚合函数如SUM, COUNT, MAX, AVG等.这些函数和其它函数的根本区别就是它们一般作用在多条记录上.而通过使用GROUP BY 子句,可以让SUM 和 COUNT 这些函数对属于 ...
- Linux服务器维护常用命令
# uname -a # 查看内核/操作系统/CPU信息 # /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算 ...
- windows下python调用c文件流程
1.新建fun.c文件和fun.h文件 #include <stdio.h> #include <stdlib.h> #include <string.h> int ...
- CRC冗余校验码的介绍和实现
from:http://yoyo.play175.com/p/200.html 节选至百度百科: 首先,任何一个由二进制数位串组成的代码,都可以惟一地与一个只含有0和1两个系数的多项式建立一一对应的关 ...
- Ajax+Spring MVC实现跨域请求(JSONP)
背景: AJAX向后台(springmvc)发送请求,报错:已阻止交叉源请求:同源策略不允许读取 http://127.0.0.1:8080/DevInfoWeb/getJsonp 上的远程资源.可 ...
- $《利用Python进行数据分析》学习笔记系列——IPython
本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...
- $.queue() 与 $.dequeue() -- 队列
JQuery 运用队列为动画模块服务,但好像它应该有更多用处,我觉得的,那试试就知道咯. 简单的来讲,它就是形成队列和出列, 也就因此可以进行很有规律的回调和延时了呀(暂停感觉有难度),当然这就是后面 ...
- 用vim写python脚本的自动缩进格式设置