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 题目链接 ...
 
随机推荐
- Linux学习笔记(2)linux系统信息与进程相关命令
			
man 获得命令的帮助手册,如man cp:按q键退出 su 切换用户,如su - root; '-'表示改变用户的环境变量 who 显示系统中登录的用户 w 显示登录用户的详细信息 last 查看最 ...
 - WebService中WSDL和WADL(转)
			
转自https://blog.csdn.net/liuxiao723846/article/details/51611183#commentBox 自己加了修改批注方便自己理解. 1.Java开发We ...
 - 从1到N中1的个数
			
示例1,2...9,10,11中有四个1 int getNumber(int n) { int count = 0; int factor = 1; int low = 0; int cur = 0; ...
 - 3.3 使用STC89C52控制MC20通过GPRS远程发送数据
			
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
 - eval in Shell
			
语法:eval cmdLine eval会对后面的cmdLine进行两遍扫描,如果第一遍扫描后,cmdLine是个普通命令,则执行此命令: 如果cmdLine中含有变量的间接引用,则保证间接引用的语义 ...
 - iOS代码瘦身实践
			
1 分析当前ipa的组成 一般一个ipa会包含: 1) 资源文件 本地文件:数据.配置.数据库等等 字体文件 图片资源 2) 源代码 通过生成linkmap文件,分析源代码生成的编译文件的大小.在B ...
 - 每天一个Linux命令(57)rpm命令
			
rpm是一个功能十分强大的软件包管理系统. (1)用法: 用法: rpm [参数] [包名] (2)功能: 功能: 使得在Linux下安装.升级和删除软 ...
 - 使用Shell脚本查找程序对应的进程ID,并杀死进程
			
#!/bin/sh NAME='shell.php' echo $NAME ID=`ps -ef | grep "$NAME" | grep -v "$0" | ...
 - 出现GC overhead limit exceeded 的解决方案
			
当我在使用MyEclispe IDE创建Maven项目的时候出现 "An internal error occurred during: “Build Project”. GC overh ...
 - Windows batch: call more than one command in a FOR loop?
			
https://stackoverflow.com/questions/2252979/windows-batch-call-more-than-one-command-in-a-for-loop U ...