【贪心】【set】zoj3963 Heap Partition
贪心地从前往后扫,每到一个元素,就查看之前的元素中小于等于其的最大的元素是否存在,如果存在,就将它置为其父亲。如果一个结点已经是两个儿子的父亲了,就不能在set中存在了,就将他删除。如果然后将当前元素插入。
如果不存在,就直接将当前元素插入。
哦,用pb_ds好像有点蠢,貌似set也能查询前驱……QAQ
- #include<cstdio>
- #include<vector>
- #include<algorithm>
- #include<cstring>
- #include<ext/pb_ds/assoc_container.hpp>
- #include<ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_cxx;
- using namespace __gnu_pbds;
- #define INF 2147483647
- vector<int>G[100010],prin[100010];
- int T,n,a[100010];
- typedef pair<int,int> Point;
- tree<Point,null_type,less<Point>,rb_tree_tag,tree_order_statistics_node_update>S;
- typedef tree<Point,null_type,less<Point>,rb_tree_tag,tree_order_statistics_node_update>::iterator ITER;
- bool vis[100010];
- int sum;
- void dfs(int U){
- vis[U]=1;
- prin[sum].push_back(U);
- for(int i=0;i<G[U].size();++i){
- dfs(G[U][i]);
- }
- }
- int main(){
- // freopen("f.in","r",stdin);
- scanf("%d",&T);
- for(;T;--T){
- sum=0;
- memset(vis,0,sizeof(vis));
- S.clear();
- scanf("%d",&n);
- for(int i=1;i<=n;++i){
- G[i].clear();
- }
- for(int i=1;i<=n;++i){
- scanf("%d",&a[i]);
- ITER it=S.lower_bound(Point(a[i],i));
- if(it!=S.begin()){
- --it;
- G[(*it).second].push_back(i);
- if(G[(*it).second].size()==2){
- S.erase(it);
- }
- S.insert(Point(a[i],i));
- }
- else{
- S.insert(Point(a[i],i));
- }
- }
- for(int i=1;i<=n;++i){
- if(!vis[i]){
- ++sum;
- dfs(i);
- }
- }
- printf("%d\n",sum);
- for(int i=1;i<=sum;++i){
- sort(prin[i].begin(),prin[i].end());
- printf("%d ",prin[i].size());
- for(int j=0;j<prin[i].size()-1;++j){
- printf("%d ",prin[i][j]);
- }
- printf("%d\n",prin[i][prin[i].size()-1]);
- }
- for(int i=1;i<=sum;++i){
- prin[i].clear();
- }
- }
- return 0;
- }
【贪心】【set】zoj3963 Heap Partition的更多相关文章
- zoj-3963 Heap Partition(贪心+二分+树状数组)
题目链接: Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence ...
- zoj 3963 Heap Partition(并查集,贪心,二分)
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- ZOJ 3963 Heap Partition set维护。给一个序列,将其划分成尽量少的序列,使每一个序列满足按照顺序构造二叉树,父母的值<=孩子的值。
Heap Partition Time Limit: Seconds Memory Limit: KB Special Judge A sequence S = {s1, s2, ..., sn} i ...
- Heap Partition ZOJ - 3963(贪心)
ZOJ - 3963 贪心做一下就好了 反正别用memset #include <iostream> #include <cstdio> #include <sstrea ...
- ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解
题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multi ...
- ZOJ 3963:Heap Partition(贪心+set+并查集)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3963 题意:给出一个n个数的序列,可以在其中找一个子序列建堆,并且堆中的父 ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- 优化特性(Attribute)性能
通过这篇文章,不仅可以了解到Attribute的工作原理,还可以了解到GetcustomeAttribute是的内部执行流程.最后,你会看到,使用缓存机制可以极大的优化反射Attribute的性能. ...
- 【python】内存调试
全文拷贝自:http://blog.csdn.net/BaishanCloud/article/details/76422782 问题定位过程解读 gdb-python:搞清楚python程序在做什么 ...
随机推荐
- 【Mysql优化】索引覆盖
索引覆盖 是指 如果查询的列恰好是索引的一部分,那么查询只需要在索引文件上进行,不需要回行到磁盘再找数据.这种查询速度非常快,称为”索引覆盖”,比平时的查询少一次到磁盘读数据的操作.(索引正好覆盖到查 ...
- AspxGridView在cell内显示颜色
protected void master_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if ...
- PHP-5.6.22安装
查看系统及内核版本 [root@test88 ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@test88 ~]# uname ...
- jdbc预编译插入数据操作
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...
- CF633F The Chocolate Spree
Description Alice and Bob have a tree (undirected acyclic connected graph). There are \(a_{i}\) choc ...
- asp.net的Server.MapPath方法
Server.MapPath()的功能: 返回与 Web 服务器上的指定虚拟路径相对应的物理文件路径. 命名空间: System.Web 程序集: System.Web(在 System.Web.dl ...
- poj 1426(同余搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26926 Accepted: 111 ...
- poj 1742(好题,楼天城男人八题,混合背包)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 33269 Accepted: 11295 Descripti ...
- 【hdoj_2152】Fruit(母函数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2152 本题采用母函数模板求解,母函数模板如下: http://blog.csdn.net/ten_sory ...
- Redis安装+密码认证
redis: 安装: $ wget http://download.redis.io/releases/redis-3.2.7.tar.gz $ tar xzf redis-3.2.7.tar.gz ...