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的第 ...
随机推荐
- 初识gispro
因为之前一直用的arcmap,由于项目中用到三维数据的服务发布,需要用到gispro.Gispro与arcmap用法还是有些不同.仅用此文来记录一些简易操作. Gispro简介 ArcGIS Pro是 ...
- Linq To SQL LEFT OUTER JOIN (Left Join)
SQL: SELECT [t0].[ProductName], [t1].[TotalPrice] AS [TotalPrice] FROM [Product] AS [t0] LEFT OUTER ...
- 支持向量机SVM 参数选择
http://ju.outofmemory.cn/entry/119152 http://www.cnblogs.com/zhizhan/p/4412343.html 支持向量机SVM是从线性可分情况 ...
- Python OS模块常用功能 中文图文详解
一.Python OS模块介绍 OS模块简单的来说它是一个Python的系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. 可以查看OS模块的帮助文档: >>> i ...
- dict['source'] = list[1],出现这种情况大多是数据的格式发生错误
修改数据的格式
- py3中reduce
前因 因为 Guido 先生讨厌 reduce.(Because Guido hates it. ) 详细过程请翻阅原文:http://www.artima.com/forums/flat.jsp?f ...
- mongodb查看操作记录方法以及用户添加删除权限修改密码
前一阵跑程序时发现一个问题,同事导出了部分数据,但是在merge回原库时竟然和原库的数据对不上,后来找了半天发现是原库数据少了. 找了很多资料发现很多人认为的操作日志和我想的不太一样...找了半条才发 ...
- 20165316 预备作业3 Linux安装及学习
Linux安装 我下载的是VirtualBox 5.2.6和Ubuntu 17.10.1,感觉这两个版本的兼容性不是太好,因为我在Oracle的官网社区中看到不少新版本的问题没有得到解决,而老版本(V ...
- javanio2
package com.lanhuigu.nio.selector; import java.net.InetSocketAddress; import java.nio.ByteBuffer; im ...
- easyui form提交和formdata提交记录
1 easyui form提交 $('form').form('submit',{ url:''; onSubmit:''; success:function(data){ //这种方法获取到的da ...