贪心地从前往后扫,每到一个元素,就查看之前的元素中小于等于其的最大的元素是否存在,如果存在,就将它置为其父亲。如果一个结点已经是两个儿子的父亲了,就不能在set中存在了,就将他删除。如果然后将当前元素插入。

如果不存在,就直接将当前元素插入。

哦,用pb_ds好像有点蠢,貌似set也能查询前驱……QAQ

  1. #include<cstdio>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<ext/pb_ds/assoc_container.hpp>
  6. #include<ext/pb_ds/tree_policy.hpp>
  7. using namespace std;
  8. using namespace __gnu_cxx;
  9. using namespace __gnu_pbds;
  10. #define INF 2147483647
  11. vector<int>G[100010],prin[100010];
  12. int T,n,a[100010];
  13. typedef pair<int,int> Point;
  14. tree<Point,null_type,less<Point>,rb_tree_tag,tree_order_statistics_node_update>S;
  15. typedef tree<Point,null_type,less<Point>,rb_tree_tag,tree_order_statistics_node_update>::iterator ITER;
  16. bool vis[100010];
  17. int sum;
  18. void dfs(int U){
  19. vis[U]=1;
  20. prin[sum].push_back(U);
  21. for(int i=0;i<G[U].size();++i){
  22. dfs(G[U][i]);
  23. }
  24. }
  25. int main(){
  26. // freopen("f.in","r",stdin);
  27. scanf("%d",&T);
  28. for(;T;--T){
  29. sum=0;
  30. memset(vis,0,sizeof(vis));
  31. S.clear();
  32. scanf("%d",&n);
  33. for(int i=1;i<=n;++i){
  34. G[i].clear();
  35. }
  36. for(int i=1;i<=n;++i){
  37. scanf("%d",&a[i]);
  38. ITER it=S.lower_bound(Point(a[i],i));
  39. if(it!=S.begin()){
  40. --it;
  41. G[(*it).second].push_back(i);
  42. if(G[(*it).second].size()==2){
  43. S.erase(it);
  44. }
  45. S.insert(Point(a[i],i));
  46. }
  47. else{
  48. S.insert(Point(a[i],i));
  49. }
  50. }
  51. for(int i=1;i<=n;++i){
  52. if(!vis[i]){
  53. ++sum;
  54. dfs(i);
  55. }
  56. }
  57. printf("%d\n",sum);
  58. for(int i=1;i<=sum;++i){
  59. sort(prin[i].begin(),prin[i].end());
  60. printf("%d ",prin[i].size());
  61. for(int j=0;j<prin[i].size()-1;++j){
  62. printf("%d ",prin[i][j]);
  63. }
  64. printf("%d\n",prin[i][prin[i].size()-1]);
  65. }
  66. for(int i=1;i<=sum;++i){
  67. prin[i].clear();
  68. }
  69. }
  70. return 0;
  71. }

【贪心】【set】zoj3963 Heap Partition的更多相关文章

  1. zoj-3963 Heap Partition(贪心+二分+树状数组)

    题目链接: Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence ...

  2. zoj 3963 Heap Partition(并查集,贪心,二分)

    Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence S = { ...

  3. ZOJ 3963 Heap Partition set维护。给一个序列,将其划分成尽量少的序列,使每一个序列满足按照顺序构造二叉树,父母的值<=孩子的值。

    Heap Partition Time Limit: Seconds Memory Limit: KB Special Judge A sequence S = {s1, s2, ..., sn} i ...

  4. Heap Partition ZOJ - 3963(贪心)

    ZOJ - 3963 贪心做一下就好了 反正别用memset #include <iostream> #include <cstdio> #include <sstrea ...

  5. ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解

    题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multi ...

  6. ZOJ 3963:Heap Partition(贪心+set+并查集)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3963 题意:给出一个n个数的序列,可以在其中找一个子序列建堆,并且堆中的父 ...

  7. 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 = { ...

  8. 优化特性(Attribute)性能

    通过这篇文章,不仅可以了解到Attribute的工作原理,还可以了解到GetcustomeAttribute是的内部执行流程.最后,你会看到,使用缓存机制可以极大的优化反射Attribute的性能. ...

  9. 【python】内存调试

    全文拷贝自:http://blog.csdn.net/BaishanCloud/article/details/76422782 问题定位过程解读 gdb-python:搞清楚python程序在做什么 ...

随机推荐

  1. 【Mysql优化】索引覆盖

    索引覆盖 是指 如果查询的列恰好是索引的一部分,那么查询只需要在索引文件上进行,不需要回行到磁盘再找数据.这种查询速度非常快,称为”索引覆盖”,比平时的查询少一次到磁盘读数据的操作.(索引正好覆盖到查 ...

  2. AspxGridView在cell内显示颜色

    protected void master_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if ...

  3. PHP-5.6.22安装

    查看系统及内核版本 [root@test88 ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@test88 ~]# uname ...

  4. jdbc预编译插入数据操作

    package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...

  5. CF633F The Chocolate Spree

    Description Alice and Bob have a tree (undirected acyclic connected graph). There are \(a_{i}\) choc ...

  6. asp.net的Server.MapPath方法

    Server.MapPath()的功能: 返回与 Web 服务器上的指定虚拟路径相对应的物理文件路径. 命名空间: System.Web 程序集: System.Web(在 System.Web.dl ...

  7. poj 1426(同余搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26926   Accepted: 111 ...

  8. poj 1742(好题,楼天城男人八题,混合背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 33269   Accepted: 11295 Descripti ...

  9. 【hdoj_2152】Fruit(母函数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2152 本题采用母函数模板求解,母函数模板如下: http://blog.csdn.net/ten_sory ...

  10. Redis安装+密码认证

    redis: 安装: $ wget http://download.redis.io/releases/redis-3.2.7.tar.gz $ tar xzf redis-3.2.7.tar.gz ...