题意:给你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. uvm设计分析——field automation

    uvm中的field_automation主要实现了class中的基础元素的copy,compare等函数, 实现方式分为两种:1)用户注册,field系列宏:uvm内部调用static status ...

  2. <2>基本表达式和语句

    1.基本表达式 1: =, +, -, *, /, 赋值,加减剩除; lua 没有 c/c++的缩写表达式 += -= *=, ++, --; 2: () 改变运算的优先级; 3: 字符串对象加法.. ...

  3. Yii2 nginx配置伪静态

    Yii2 配置 Nginx 伪静态 主要检查以下代码: location / { # Redirect everything that isn't a real file to index.php t ...

  4. C# SQLite 数据库操作

    C# SQLite 数据库操作学习 运行环境:Window7 64bit,.NetFramework4.61,C# 7.0 参考: SQLite 官网 SQL As Understood By SQL ...

  5. MVC请求管道

    下面是请求管道中的19个事件. (1)BeginRequest: 开始处理请求 (2)AuthenticateRequest授权验证请求,获取用户授权信息 (3):PostAuthenticateRe ...

  6. 设计模式之Adapter(适配器)(转)

    定义: 将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份. 为何使用? 我们经常碰到要将两个没有关系的类组合在一起使用,第一解决方案是 ...

  7. 解读 JavaScript 之引擎、运行时和堆栈调用

    https://www.oschina.net/translate/how-does-javascript-actually-work-part-1 随着 JavaScript 变得越来越流行,很多团 ...

  8. tomcat查看并修改jvm大小

    JVM--Java Virtual Machine,Java虚拟机:tomcat不是直接运行在物理操作系统上,而是运行在Java虚拟机上,通常说的配置jvm就是配置分配给Java虚拟机的内存大小: 如 ...

  9. scss简单用法

  10. 怎样从外网访问内网Tomcat?

    本地安装了一个Tomcat,只能在局域网内访问,怎样从外网也能访问到本地的Tomcat呢?本文将介绍具体的实现步骤. 准备工作 安装并启动Tomcat 默认安装的Tomcat端口是8080. 实现步骤 ...