Codeforces Round #642 (Div. 3) D. Constructing the Array (优先队列)

题意:有一个长度为\(n\)元素均为\(0\)的序列,进行\(n\)次操作构造出一个新序列\(a\):每次选择最长的连续为\(0\)的区间\([l,r]\),使得第\(i\)次操作时,\(a[\frac{l+r}{2}]=i\)(下取整),求\(a\).
题解:刚开始我打算用归并分治的思想来写,但是发现左区间递归不到,然后就gg了.
之后才发现这题用_优先队列_直接模拟就过了,题目就不说了,这儿讲一下优先队列.
优先队列与队列的区别在于,优先队列是按照某种优先级来决定谁在队头,C++默认它是一个大根堆,但是我们往往需要自己来定义优先级,所以我们就可以用一个结构体来重载运算符,再结合堆的性质来理解就好了.
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int> PII;
typedef pair<long,long> PLL; int t;
int n;
int ans[N];
int cnt; struct Node{
int l,r;
bool operator < (const Node &w)const{
if(r-l==w.r-w.l) return l>w.l;
else return r-l<w.r-w.l;
}
}; int main() {
ios::sync_with_stdio(false);
cin>>t;
while(t--){
cin>>n;
cnt=0;
priority_queue<Node> q;
q.push({1,n});
while(!q.empty()){
auto cmp=q.top();
q.pop();
int l=cmp.l;
int r=cmp.r;
int mid=l+r>>1;
ans[mid]=++cnt;
if(l!=mid) q.push({l,mid-1});
if(r!=mid) q.push({mid+1,r});
}
for(int i=1;i<=n;++i) printf("%d ",ans[i]);
puts("");
} return 0;
}
Codeforces Round #642 (Div. 3) D. Constructing the Array (优先队列)的更多相关文章
- Codeforces Round #642 (Div. 3)
比赛链接:https://codeforces.com/contest/1353 A - Most Unstable Array 题意 构造大小为 $n$,和为 $m$ 的非负数组 $a$,使得相邻元 ...
- Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改
A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...
- Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题
B. Wilbur and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...
- Codeforces Round #258 (Div. 2) B. Sort the Array
题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...
- Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列
B. DZY Loves Modification 题目连接: http://www.codeforces.com/contest/446/problem/B Description As we kn ...
- Codeforces Round #390 (Div. 2) A. Lesha and array splitting
http://codeforces.com/contest/754/problem/A 题意: 给出一串序列,现在要把这串序列分成多个序列,使得每一个序列的sum都不为0. 思路: 先统计一下不为0的 ...
随机推荐
- 【Linux】系统打开文件最大数量限制(进程打开的最大文件句柄数设置)
利用ulimit命令可以对资源的可用性进行控制. -H选项和-S选项分别表示对给定资源的硬限制(hard limit)和软限制(soft limit)进行设置. 硬限制(hard limit)一旦被设 ...
- CTS相关的几个表
TMSALOG/TMSALOGAR :STMS传输日志表 TMSCNFS:传输组 TMSCDOM:传输域 TMSCDES:传输目的地 TMSBUFTXT:传输请求的短文本和用户 TMSCSYS:tms ...
- SAP中的F4帮助
今天在调试标准程序的时候,意外的发现了一个F4帮助的函数,感觉还是挺好用的. F4IF_FIELD_VALUE_REQUEST从函数名就可以看出是给字段添加F4帮助的. F4 help for fie ...
- 注入器(injector)
1.0 注入器/injector 注入器是AngularJS框架实现和应用开发的关键,这是一个DI/IoC容器的实现. AngularJS将功能分成了不同类型的组件分别实现,这些组件有一个统称 ...
- Django--虛擬環境Virtualenv的安裝使用
Django--虛擬環境Virtualenv的安裝使用 本次隨筆只要記錄在windows下安裝virtualenvwrapper,以及簡單的使用命令. virtualenvwrapper的安裝 ...
- 并发编程常用工具类(二) SymaPhore实现线程池
1.symaPhore简介 symaphore(信号量)用来控制同时访问某个资源的线程数量,一般用在并发流量控制.个人对它的理解相当于是接待室每次只能接待固定数量的人,当达到最高接待数的时候,其他人就 ...
- ftp上传文件出现553 Could not creat files 严重文件传输错误
之前上传文件到云服务器上一直出错,发现可以下载但是不能上传和编辑,后来终于找到原因了,是因为上传文件所在文件夹默认只有root用户才有写权限,所以我们还要将写权限赋予给其他用户.可以用Xshell 5 ...
- CentOS安装mysql、JDK、Tomcat部署环境
1.1. 安装mysql-5.6 1.1.1. 检测系统内部有没有安装其他的mysql数据库 $ rpm -qa | grep mysql 1.1.2. 如果内部有需要先删除Mysql $ yum r ...
- day03 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...
- Compile-time Dependency Injection With Go Cloud's Wire 编译时依赖注入 运行时依赖注入
Compile-time Dependency Injection With Go Cloud's Wire - The Go Blog https://blog.golang.org/wire Co ...