ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解
题意:给你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自带二分 + 贪心)题解的更多相关文章
- 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 ...
- zoj 3963 heap partion
https://vjudge.net/problem/ZOJ-3963 题意: 给出一个数列,可以用这个数列构造一种二叉树,这个二叉树满足数的下标 i <= j,并且 si <= sj,s ...
- Heap Partition ZOJ - 3963(贪心)
ZOJ - 3963 贪心做一下就好了 反正别用memset #include <iostream> #include <cstdio> #include <sstrea ...
- 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 ...
- zoj-3963 Heap Partition(贪心+二分+树状数组)
题目链接: Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- STL中的二分查找
本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
随机推荐
- 准备MyBatis
MyBatis下载:https://github.com/mybatis/mybatis-3/releases MyBatis文件目录: 中文参考文档:http://www.mybatis.org/m ...
- MySql 查询表中字段的数据类型
[1]MySQL中查询某表中字段的数据类型 (1)DESC 表名: (2)DESCRIBE 表名: (3)SHOW COLUMNS FROM 表名: 应用示例: DESC cfg_acct_free_ ...
- python os.path.join()
>>> import os >>> path = '/Users/beazley/Data/data.csv' >>> # Get the las ...
- Dirichlet分布深入理解
Dirichlet分布 我们把Beta分布推广到高维的场景,就是Dirichlet分布.Dirichlet分布定义如下 Dirichlet分布与多项式分布共轭.多项式分布定义如下 共轭关系表示如下 D ...
- django2.0 路由规则
Django2.0中URL的路由机制 路由是关联url及其处理函数关系的过程.Django的url路由配置在settings.py文件中ROOT_URLCONF变量指定全局路由文件名称. Django ...
- Kylin, Mondrian, Saiku系统的整合
本文主要介绍有赞数据团队为了满足在不同维度查看.分析重点指标的需求而搭建的OLAP分析工具.这个工具对Kylin.Mondrian以及Saiku做了一个整合,主要工作包括一些定制化的修改以及环境的配置 ...
- Linux基础命令---显示路由表route
route route指令用于显示或者修改IP路由表.它的主要用途是在使用ifconfig(8)程序配置接口后,通过接口设置到特定主机或网络的静态路由.当使用add或del选项时,路由将修改路由表.如 ...
- vue对比其他框架详细介绍
vue对比其他框架详细介绍 对比其他框架 — Vue.jshttps://cn.vuejs.org/v2/guide/comparison.html React React 和 Vue 有许多相似之处 ...
- JustOj 2043: N!
题目描述 输出N的阶乘.(注意时间限制150ms&&注意不能打表后输出,赛后我们会检查代码,如有发现,该位同学总分记0分处理) 打表的定义:在本地主机预先计算出了每个值对应的答案,并把 ...
- Codeforce 287A - IQ Test (模拟)
In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the ...