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

思路:贪心。我们往multiset加还能加子节点的节点,二分查找一个个大于等于当前插入节点的节点,然后插入,若找不到则重新建一棵树。

没想到set自带lower_bound(),第一次迭代器遍历TLE就想着手动写二分...然后发现自带二分...

代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cmath>
  4. #include<string>
  5. #include<queue>
  6. #include<set>
  7. #include<vector>
  8. #include<string.h>
  9. #include<algorithm>
  10. typedef long long int ll;
  11. using namespace std;
  12. const int maxn = 1e5 + ;
  13. const int inf = 0x3f3f3f3f;
  14. const ll mod = 1e9 + ;
  15. vector<int> G[maxn];
  16. struct node{
  17. int id, sz, num;
  18. };
  19. struct compare{
  20. bool operator () (node a, node b){
  21. return a.sz > b.sz;
  22. }
  23. };
  24. node add(int id, int sz){
  25. node a;
  26. a.id = id , a.sz = sz, a.num = ;
  27. return a;
  28. }
  29. multiset<node, compare> q;
  30. int main(){
  31. int T, a;
  32. scanf("%d", &T);
  33. while(T--){
  34. q.clear();
  35. int n;
  36. scanf("%d", &n);
  37. scanf("%d", &a);
  38. int cnt = ;
  39. q.insert(add(, a));
  40. G[].clear();
  41. G[].push_back();
  42. node p;
  43. for(int i = ; i <= n; i++){
  44. scanf("%d" ,&a);
  45. multiset<node>::iterator it;
  46. p.sz = a;
  47. it = q.lower_bound(p);
  48. if(it == q.end()){
  49. q.insert(add(cnt, a));
  50. G[cnt].clear();
  51. G[cnt].push_back(i);
  52. cnt++;
  53. }
  54. else{
  55. p = *it;
  56. q.erase(it);
  57. p.num++;
  58. if(p.num < )
  59. q.insert(p);
  60. q.insert(add(p.id, a));
  61. G[p.id].push_back(i);
  62. }
  63. }
  64. printf("%d\n", cnt - );
  65. for(int i = ; i < cnt; i++){
  66. int len = G[i].size();
  67. printf("%d", len);
  68. for(int j = ; j < len; j++)
  69. printf(" %d", G[i][j]);
  70. printf("\n");
  71. }
  72. }
  73. return ;
  74. }

ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解的更多相关文章

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

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

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

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

  3. zoj 3963 heap partion

    https://vjudge.net/problem/ZOJ-3963 题意: 给出一个数列,可以用这个数列构造一种二叉树,这个二叉树满足数的下标 i <= j,并且 si <= sj,s ...

  4. Heap Partition ZOJ - 3963(贪心)

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

  5. SPOJ ADAFIELD Ada and Field(STL的使用:set,multiset,map的迭代器)题解

    题意:n*m的方格,“0 x”表示x轴在x位置切一刀,“0 y”表示y轴在y位置切一刀,每次操作后输出当前面积最大矩形. 思路:用set分别储存x轴y轴分割的点,用multiset(可重复)储存x轴y ...

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

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

  7. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  8. STL中的二分查找

    本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...

  9. STL中的二分查找——lower_bound 、upper_bound 、binary_search

    STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...

随机推荐

  1. 准备MyBatis

    MyBatis下载:https://github.com/mybatis/mybatis-3/releases MyBatis文件目录: 中文参考文档:http://www.mybatis.org/m ...

  2. MySql 查询表中字段的数据类型

    [1]MySQL中查询某表中字段的数据类型 (1)DESC 表名: (2)DESCRIBE 表名: (3)SHOW COLUMNS FROM 表名: 应用示例: DESC cfg_acct_free_ ...

  3. python os.path.join()

    >>> import os >>> path = '/Users/beazley/Data/data.csv' >>> # Get the las ...

  4. Dirichlet分布深入理解

    Dirichlet分布 我们把Beta分布推广到高维的场景,就是Dirichlet分布.Dirichlet分布定义如下 Dirichlet分布与多项式分布共轭.多项式分布定义如下 共轭关系表示如下 D ...

  5. django2.0 路由规则

    Django2.0中URL的路由机制 路由是关联url及其处理函数关系的过程.Django的url路由配置在settings.py文件中ROOT_URLCONF变量指定全局路由文件名称. Django ...

  6. Kylin, Mondrian, Saiku系统的整合

    本文主要介绍有赞数据团队为了满足在不同维度查看.分析重点指标的需求而搭建的OLAP分析工具.这个工具对Kylin.Mondrian以及Saiku做了一个整合,主要工作包括一些定制化的修改以及环境的配置 ...

  7. Linux基础命令---显示路由表route

    route route指令用于显示或者修改IP路由表.它的主要用途是在使用ifconfig(8)程序配置接口后,通过接口设置到特定主机或网络的静态路由.当使用add或del选项时,路由将修改路由表.如 ...

  8. vue对比其他框架详细介绍

    vue对比其他框架详细介绍 对比其他框架 — Vue.jshttps://cn.vuejs.org/v2/guide/comparison.html React React 和 Vue 有许多相似之处 ...

  9. JustOj 2043: N!

    题目描述 输出N的阶乘.(注意时间限制150ms&&注意不能打表后输出,赛后我们会检查代码,如有发现,该位同学总分记0分处理) 打表的定义:在本地主机预先计算出了每个值对应的答案,并把 ...

  10. Codeforce 287A - IQ Test (模拟)

    In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the ...