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 ...
- CF85D Sum of Medians
CF85D Sum of Medians 题意翻译 一个集合,初始为空.现有三个操作: 1.add:向集合里加入数x,保证加入前集合中没有数x: 2.del:从集合中删除数x,保证删除前集合中有x: ...
- MySQL中行锁的算法
行锁的3中算法 Record Lock:单个行记录上的锁 Gap Lock:间隙锁,锁定一个范围,但不包含记录本身 Next-key Lock:Gap Lock+Record Lock锁定一个范围,并 ...
- vb.net 正則表達式 取 固定格式的字符
vb.net 正則表達式 取 固定格式的字符: 原始字符串:strSqlTmp="select * from A_TEST where a_data = '@1@' and b_link = ...
- 《Python机器学习》笔记(五)
通过降维压缩数据 在前面已经介绍了几种不同的特征选择技术对数据集进行降维的方法.另一种常用于降维的特征选择方法就是特征抽取.数据压缩也是机器学习领域中的一个重要内容.数据压缩技术可以帮助我们对数据及逆 ...
- boost之定时器和io_service
1.定时器的使用,sleep是等待线程,asio封装了操作系统的异步系统调用select,epoll. io_servie 实现了一个任务队列,这里的任务就是void(void)的函数.Io_serv ...
- BAPI: TRANSACTION_BEGIN的作用
大概知道是启动一个新会话, CALL FUNCTION 'TRANSACTION_BEGIN' 业务数据处理, CALL FUNCTION 'TRANSACTION_END' 详细功能不清楚. CLE ...
- JavaScript:学习笔记(2)——基本概念与数据类型
JavaScript:学习笔记(2)——基本概念与数据类型 语法 1.区分大小写.Test 和 test 是完全不同的两个变量. 2.语句最好以分号结束,也就是说不以分号结束也可以. 变量 1.JS的 ...
- SOA 面向服务架构 阅读笔记(五)
14 SOA 服务管理器 契约:契约中必须明确定义双方的责任,否则就会产生混乱. SOA可以管理端到端的流程. IT技术一直是与业务对齐的. 14.1.1 分解IT层 业务服务层 管道层 硬件层 管道 ...
- 六、golang中的结构体和方法、接口
结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...