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的 ...
随机推荐
- QA职责
- 入门训练 - 蓝桥杯(Python实现)
A+B问题: 题目: 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 输入A.B,输出A+B. 输入格式 输入的第一行包括两个整数,由空格分隔,分别表示A.B. 输出格式 输出一行, ...
- 深度解读设备的“万能语言”HarmonyOS的分布式软总线能力
摘要:本文分享鸿蒙分布式软总线,并对相关源代码进行解析,为在鸿蒙系统平台上工作的相关人员的信息参考和指导. 总线是一种内部结构,在计算机系统中,主机的各个部件通过总线相连,外部设备通过相应的接口电路再 ...
- pytorch——预测值转换为概率,单层感知机
softmax函数,可以将算出来的预测值转换成0-1之间的概率形式 导数的形式 import torch import torch.nn.functional as F x=torch.tensor( ...
- Linux中让终端输入变为非阻塞的三种方法
介绍 在linux下每打开一个终端,系统自动的就打开了三个文件,它们的文件描述符分别为0,1,2,功能分别是"标准输入"."标准输出"和"标准错误输出 ...
- spring data JPA 使用EntityentiListeners实现数据审计功能设计
当系统中有审计需求时,特别是需要对某些数据进行动态监控时,我们可以使用EntityentiListeners来实现,当然这是基于使用JPA而不是mybatis的情况下. 当前我们的需求场景: 1.需要 ...
- c#使用谷歌身份验证GoogleAuthenticator
此功能相当于给系统加了个令牌,只有输入对的一组数字才可以验证成功.类似于QQ令牌一样. 一丶创建最核心的一个类GoogleAuthenticator 此类包含了生成密钥,验证,将绑定密钥转为二维码. ...
- https://twistedmatrix.com/documents/current/core/howto/defer.html
https://twistedmatrix.com/documents/current/core/howto/defer.html
- oracle模糊查询mysql的区别
https://blog.csdn.net/weixin_38673554/article/details/86503982#_1 oracle与使用mysql的区别 https://www.cnbl ...
- 【LinuxShell】cp 用法详解
Linux cp命令主要用于复制文件或目录. -a:此选项通常在复制目录时使用,它保留链接.文件属性,并复制目录下的所有内容.其作用等于dpR参数组合. -d:复制时保留链接.这里所说的链接相当于Wi ...