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(优先队列)的更多相关文章

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

    题意:有一个长度为\(n\)元素均为\(0\)的序列,进行\(n\)次操作构造出一个新序列\(a\):每次选择最长的连续为\(0\)的区间\([l,r]\),使得第\(i\)次操作时,\(a[\fra ...

  2. CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)

    链接: https://vjudge.net/problem/CodeForces-721D 题意: Recently Maxim has found an array of n integers, ...

  3. Codeforces Round #468 Div. 2题解

    A. Friends Meeting time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. CF 1329B Dreamoon Likes Sequences

    传送门 题目: Dreamoon likes sequences very much. So he created a problem about the sequence that you can' ...

  5. Codeforces Round #642 (Div. 3)

    比赛链接:https://codeforces.com/contest/1353 A - Most Unstable Array 题意 构造大小为 $n$,和为 $m$ 的非负数组 $a$,使得相邻元 ...

  6. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  7. 华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列

    Problem G: Array C Time Limit: 1 Sec  Memory Limit: 128 MB Description Giving two integers  and  and ...

  8. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  9. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

随机推荐

  1. Knapsack Problem

    0-1背包 描述:N件物品,第i件的重量是w[i],价值v[i].有一个容量为W的背包,求将哪些物品放入背包可使总价值最大.每件物品可以用0或1次. 分析:根据题意,可以写出表达式: \[max(\S ...

  2. C#时间与时间戳格式互相转化

    C#时间格式转换为时间戳(互转) 时间戳定义为从格林威治时间 1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. using UnityEn ...

  3. 一张图告诉你E-R图怎么画

    E-R图也称实体-联系图(Entity Relationship Diagram),提供了表示实体类型.属性和联系的方法,用来描述现实世界的概念模型. 它是描述现实世界关系概念模型的有效方法.是表示概 ...

  4. python selenium(用例断言)

    1.if ...else ...判断进行断言 from time import * from selenium import webdriver "): driver = webdriver ...

  5. PLAI那些事_06 FAE

    没有了with表达,with,exp,body的id换成exp的lambda函数,从而可以没有with来进行实现.即,{with {id exp} body}换成了{{fun {id} body} e ...

  6. print函数的全面认识

    # 输出打印 数字 print(123) a = 100 print(a) # 输出打印 字符串 print('字符串123') print('''锄禾日当午 汗滴禾下土''') # 输出打印 列表 ...

  7. Oracle条件判断

    一. if/else 语法:if 条件表达式 then语句块:if 条件表达式 then 语句块end if;elsif 条件表达式 then语句块:...else语句块:end if;举例:输入一个 ...

  8. 送你一份Redis书单,以后使用缓存的问题不用再问我啦!

    点击蓝色"程序员书单"关注我哟 加个"星标",每天带你读好书!

  9. opencv-12-高斯滤波-双边滤波(附C++代码实现)

    开始之前 这几天由于自己的原因没有写, 一个是因为自己懒了, 一个是感觉这里遇到点问题不想往下写了, 我们先努力结束这个章节吧, 之前介绍了比较常用而且比较好理解的均值和中值滤波, 但是呢,在例程Sm ...

  10. LRU 的C# 实现

    首先 先写点儿感悟吧: 本来计划是 晚上回家写的  后来发现还是没坚持的了  上午花了一个多小时  做了一下这个题目  应该还有提高的空间 的,这个题目是在力扣里面看到的  为什么看到这个题目 是因为 ...