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

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

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

代码:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<string.h>
#include<algorithm>
typedef long long int ll;
using namespace std;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + ;
vector<int> G[maxn];
struct node{
int id, sz, num;
};
struct compare{
bool operator () (node a, node b){
return a.sz > b.sz;
}
};
node add(int id, int sz){
node a;
a.id = id , a.sz = sz, a.num = ;
return a;
}
multiset<node, compare> q;
int main(){
int T, a;
scanf("%d", &T);
while(T--){
q.clear();
int n;
scanf("%d", &n);
scanf("%d", &a);
int cnt = ;
q.insert(add(, a));
G[].clear();
G[].push_back();
node p;
for(int i = ; i <= n; i++){
scanf("%d" ,&a);
multiset<node>::iterator it;
p.sz = a;
it = q.lower_bound(p);
if(it == q.end()){
q.insert(add(cnt, a));
G[cnt].clear();
G[cnt].push_back(i);
cnt++;
}
else{
p = *it;
q.erase(it);
p.num++;
if(p.num < )
q.insert(p);
q.insert(add(p.id, a));
G[p.id].push_back(i);
}
}
printf("%d\n", cnt - );
for(int i = ; i < cnt; i++){
int len = G[i].size();
printf("%d", len);
for(int j = ; j < len; j++)
printf(" %d", G[i][j]);
printf("\n");
}
}
return ;
}

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. 【2017-2-26】C#String类、Math类、DateTime类

    String类 黑色小扳手:属性      后面不带括号 紫色小箱子:方法      后面带小括号 1.字符串.Length;   字符串长度,返回int类型 字符串的长度 2.字符串.TrimSta ...

  2. 【Hbase学习之三】Hbase Java API

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...

  3. SQL提交数据三种类型

    在数据库的插入.删除和修改操作时,只有当事务在提交到数据库时才算完成. SQL语句提交数据有三种类型:显式提交.隐式提交及自动提交. [1]显式提交 显式提交.即用COMMIT命令直接完成的提交方式. ...

  4. 创建 .m2 文件夹

    首次使用 Maven 创建 .m2 文件夹 1. cmd2. mvn help:system

  5. py3中reduce

    前因 因为 Guido 先生讨厌 reduce.(Because Guido hates it. ) 详细过程请翻阅原文:http://www.artima.com/forums/flat.jsp?f ...

  6. [转]Hive开发总结

    看到一篇挺不错的hive开发总结文章,在此转载一下,有兴趣的可以去看原文,传送门HIVE开发总结. 基本数据类型 查看所有函数 搜索函数 搜索表 查看函数使用方法 关键字补全 显示表头 SET环境变量 ...

  7. git log的常见用法

    git log 使用git log命令,什么参数都没有的话,会以下面的格式输出所有的日志(我当前的git仓库只有三个提交).如果日志特别多的话,在git bash中,按向下键来查看更多,按q键退出查看 ...

  8. C#中对Web.Config、App.Config字符串加密与解密的方法

    我们平常的项目里面的配置文件通常都是明文形式的存在,现在就是为了项目安全性增强,同时又显得高逼格点, 我们可以采用加密的方式,而我们C#很强大,因为他内置的一些指令方式,很方便而且使用起来还不用解密, ...

  9. tensorflow serving

    1.安装tensorflow serving 1.1确保当前环境已经安装并可运行tensorflow 从github上下载源码 git clone --recurse-submodules https ...

  10. sparkStreaming插入mysql 必须考虑到实时更新老的key

    原先使用批次提交更新 但数据库无变化,不得不一条一条的插入 公司数据量不大  还未做数据量大的测试 但实时更新是可以的 关键sql : insert into area_user_amt (date, ...