CF1353D Constructing the Array(优先队列)
Question
给你一个长度为n的全为0的序列,让你从1-n填数,填的位置为找出最长的0序列,如序列长度为奇数,则为(l+r)/2,为偶数,则为(l+r-1)/2
Solution
运用优先队列,将[l,mid-1],[mid,r]push进去,运用重载运算符排序
Code
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define maxn 1005
#define inf 0x3f3f3f
#define endl '\n'
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
//ll fpm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//ll fastPow(ll a,ll b) {ll res=1; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
int t;
int n;
struct node
{
int l,r,si;
bool operator > (const node& a) const
{
if(si!=a.si){
return si<a.si;
}
return l>a.l;
}
};
int ans[200050];
int main(){
//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n;
memset(ans,0,sizeof(ans[1]*(n+10)));
priority_queue<node,vector<node>,greater<node> >q;
q.push(node{1,n,n});
int zz=1;
while(!q.empty()){
node qq=q.top();
q.pop();
int ll=qq.l,rr=qq.r;
if(ll==rr){
ans[ll]=zz;
zz++;
}
else{
if((rr-ll+1)&1){
int temp=(rr+ll)>>1;
ans[temp]=zz;
zz++;
if(ll<temp){
q.push(node{ll,temp-1,temp-ll});
}
if(temp<rr){
q.push(node{temp+1,rr,rr-temp});
}
}
else{
int temp=(ll+rr-1)>>1;
ans[temp]=zz;
zz++;
if(ll<temp){
q.push(node{ll,temp-1,temp-ll});
}
if(temp<rr){
q.push(node{temp+1,rr,rr-temp});
}
}
}
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
return 0;
}
CF1353D Constructing the Array(优先队列)的更多相关文章
- Codeforces Round #642 (Div. 3) D. Constructing the Array (优先队列)
题意:有一个长度为\(n\)元素均为\(0\)的序列,进行\(n\)次操作构造出一个新序列\(a\):每次选择最长的连续为\(0\)的区间\([l,r]\),使得第\(i\)次操作时,\(a[\fra ...
- CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)
链接: https://vjudge.net/problem/CodeForces-721D 题意: Recently Maxim has found an array of n integers, ...
- Codeforces Round #468 Div. 2题解
A. Friends Meeting time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- CF 1329B Dreamoon Likes Sequences
传送门 题目: Dreamoon likes sequences very much. So he created a problem about the sequence that you can' ...
- Codeforces Round #642 (Div. 3)
比赛链接:https://codeforces.com/contest/1353 A - Most Unstable Array 题意 构造大小为 $n$,和为 $m$ 的非负数组 $a$,使得相邻元 ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- 华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列
Problem G: Array C Time Limit: 1 Sec Memory Limit: 128 MB Description Giving two integers and and ...
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
随机推荐
- Nacos作为配置中心时,多个服务共用一个dataId的配置
写在前面 本文是对我之前一篇文章<Spring Cloud+nacos+Feign,实现注册中心及配置中心>的补充.此文章中简单写了如何将Nacos作为配置中心.在使用配置中心时,我们会遇 ...
- thrift的使用
简介 thrift 原来是facebook的rpc框架,根据数据结构和接口描述生成多种语言的接口,方便使用多种语言进行开发,详细信息这里不再赘述,下文以一个简单的代码(C++)示例来介绍使用方法. 示 ...
- 一个数number的n次幂 python的pow函数
@ 目录 解法1:暴力法 解法2:根据奇偶幂分类(递归法,迭代法,位运算法) 实现 pow(x, n),即计算 x 的 n 次幂函数.其中n为整数. 链接: pow函数的实现--leetcode. 解 ...
- linux gdb快速入门教程
文章目录 前言 常用指令概览 开始使用gdb 一个完整流程一般所需步骤 1 加载程序 2 查看 2.1 查看函数 3 设置断点 3.1 根据函数名设置断点 3.2 根据程序位置(第几行) 4 运行程序 ...
- java web 开发之 office(excel、doc等)文件转pdf
一.开发工具:office 16.jacob-1.18-M2.jboss 1.6 二.开发配置: 1.解压缩---> 2.配置jacob: A C:\Windows\System32 jacob ...
- [hdu4416 Good Article Good sentence]后缀自动机SAM
题意:给出串A和串集合B={B1,B2,...,Bn},求串A的所有不同子串中不是B中任一串的子串的数目. 思路:把A和B中所有字符串依次拼接在一起,然后构造后缀自动机,计算每个状态的R集合元素的最大 ...
- [hdu5375 Gray code]DP
题意:给一个二进制码,其中有一些位上为'?',对每个问号确定是'0'还是'1',最后以它对应的格雷码来取数,第i位为1则取第i个数,求取得的数的和的最大值. 思路:二进制码B转换成格雷码G的方法是,G ...
- 跨站点请求伪造(CSRF)总结和防御
什么是CRSF 构建一个地址,比如说是删除某个博客网站博客的链接,然后诱使已经登录过该网站的用户点击恶意链接,可能会导致用户通过自己的手将曾经发布在该网站的博客在不知情的情况下删除了.这种构建恶意链接 ...
- 08JAVA基础关键字(final、static)以及抽象类和接口
一.关键字 1.final 修饰类 修饰变量 修饰成员方法 该类为最终类,不能被继承 该变量为常量 该成员方法不能被重写 2.static (1).生命周期 随着类的加载而加载 (2).特点 被本类所 ...
- python--正则表达式|re模块学习
学习来源:https://www.liaoxuefeng.com/wiki/1016959663602400/1017639890281664 正则表达式是一种用来匹配字符串的一种强大的武器,用一种描 ...