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 题目链接 ...
随机推荐
- python——列表&字符串互相转换方法小结
字符串(str)转列表(list) 转换方法:str.split() str = 'zhu gao chao' print(str.split(' ')) # 用split进行转换 str——> ...
- 随机生成六位验证码函数版(python)
import random def code(n=6,alpha=True): s = '' # 创建字符串变量,存储生成的验证码 for i in range(n): # 通过for循环控制验证码位 ...
- Debussy的安装与使用
1.概述 Debussy是NOVAS Software, Inc ( 思源科技 )发展的HDL Debug & Analysis tool,这套软体主要不是用来跑模拟或看波形,它最强大的功能是 ...
- corethink功能模块探索开发(六)让这个模块在前台显示
效果图:(注意右上角) 实现模块的前台显示只需要在模块目录中的Controller目录建立IndexController.class.php,实现index方法.继承HomeController.就能 ...
- (转) FLASH吸血鬼的工作原理
FLASH吸血鬼是众多网友用来从exe可执行文件中提取swf的利器,其直接读取内存,从内存中取出swf文件.经过分析,发现其原理还是比较简单的.第一步.通过GetWindowThreadProcess ...
- marquee标记
页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...
- range开始节点和结束节点
sc = range.startContainer, so = range.startOffsest ec = range.endContainer, eo=range.endOffset sta ...
- Loadrunder之脚本篇——参数化取值策略
参数取值选项 Select next row Update value on 以上两个选项是改变参数化取值的关键选项. Select next row包含如下选项: Sequential:顺序选择 R ...
- JPEGView——专业、免费、开源的图像浏览器
虽叫JPEGView,它不仅支持jpeg图像格式,主流的图像格式它都支持. 试一试你就知道它有多强大了.
- nodejs数据接收body-parser中间件
给大家翻译一下npm上body-parser的资料 nodejs 的body数据解析中间件 插件作用:对于req.body属性,在操作数据前分析进来的请求体的插件 首先学习解析一个http处理 这篇文 ...