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的 ...
随机推荐
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- Java多线程-锁的区别与使用
目录 锁类型 可中断锁 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁/读写锁 乐观锁/悲观锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 Synchronized与Static Synchron ...
- Ubuntu_Gedit配置
Ubuntu_Gedit配置 为了换Ubuntu的时候能够更加方便,不用再用手重新打一遍代码,丢几个Gedit配置-- External Tools gdb compile (F2) #!/bin/s ...
- 【Java】集合框架(List Set Map)
文章目录 集合框架 List(列表) ArrayList 案例 Set HashSet 案例 iterator(迭代器) Map HashMap 案例 集合总结 参考资料 重新搞一波 复习巩固 简单记 ...
- Cloudera Manager添加主机节点
为了监控方便,想把研发环境中的主机节点都纳入Cloudera Manager的管理中,这样在遇到问题时可方便的查看主机的硬件资源情况. 添加主机节点有多种方式,由于我是离线工作,所以选择rpm包的方式 ...
- CWE 4.3:强化你的数据自我保护能力
摘要:如何通过软件自动的检查法规中涉及的数据保护, 新版的CWE 4.3 给出了一个解决途径. 1. 按照惯例,先说故事 用12月初在深圳参加的"全球C++及系统软件技术大会"里C ...
- 入门OJ:郭嘉的消息传递
题目描述 我们的郭嘉大大在曹操这过得逍遥自在,但是有一天曹操给了他一个任务,在建邺城内有N(<=1000)个袁绍的奸细 将他们从1到N进行编号,同时他们之间存在一种传递关系,即若C[i,j]=1 ...
- ETCD数据迁移
ETCD数据迁移 本文阅读对象为想要将Rainbond平台rbd-etcd切换至外部etcd的相关人员. 在k8s master节点创建secret 本文中将要切换的ETCD为根据Rainbond官方 ...
- 01. struts2介绍
struts2优点 与Servlet API 耦合性低.无侵入式设计 提供了拦截器,利用拦截器可以进行AOP编程,实现如权限拦截等功能 支持多种表现层技术,如:JSP.freeMarker.veloc ...
- C++ unordered_map/unordered_set 自定义键类型
1. unordered_map 和 unordered_set template < class Key, // unordered_map::key_type class T, // uno ...