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. MYSQL 排序和分组

    一.MYSQL 中有两种排序方式: 1:通过有序索引顺序扫描直接返回有序数据,这种方式在使用explain 分析查询的时候显示为Using Index ,不需要额外的排序,操作效率较高. 2: 是通过 ...

  2. 2019年 ICPC亚洲区预赛(上海赛区)总结

    首先,我要说,我输了,输给了自己的无知,输给了自己的心态与实力. 上海区域赛,打铁而归,最终还是没有比过自己SLG的朋友.要说什么呢?实力的差距,还是说给自己的失败找借口?不能进入金牌区,为什么铜牌区 ...

  3. java基础篇 之 集合概述(List)

    list,有序集合,元素可重复 LinkedList:底层用链表实现,查找慢,增删快.为什么?? ArrayList:底层用数组实现,查找看,增删慢.为什么?? Vector:跟ArrayList一样 ...

  4. Mysql常用sql语句(八)- where 条件查询

    测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html 前言 ...

  5. vue.js 实现点击展开收起动画

    最近公司项目加了个页面,其中要求是这样的,点击对应列表,展开和收起, 其实就是显示和隐藏内容部分:说来惭愧,我花了半天时间才搞出来(自黑一下~), ,,接下来分享给大家,先上效果图: .vue页面: ...

  6. 【漫画】JAVA并发编程 J.U.C Lock包之ReentrantLock互斥锁

    在如何解决原子性问题的最后,我们卖了个关子,互斥锁不仅仅只有synchronized关键字,还可以用什么来实现呢? J.U.C包中还提供了一个叫做Locks的包,我好歹英语过了四级,听名字我就能马上大 ...

  7. linux centos7 和 windows下 部署 .net core 2.0 web应用

    centos7 下部署asp.net core 2.0应用 安装CentOS7 配置网络[可选] 安装.Net core2.0 创建测试Asp.net Core应用程序 正式部署项目 安装VMware ...

  8. java实例化对象的几种方式

    这里一共整理了四种方法 ①用类的new ()创建 ②用clone方法创建, 需要类实现 Cloneable 接口 ③通过反射机制创建 ④将一个对象实例化后,进行序列化,再反序列化 下面是实现代码 1) ...

  9. webstorm-在不删除硬盘文件的条件下移除项目

    一段时间没用之后会忘记如何在webstorm里移除一个项目,要花很长的时间去找到底如何才能移除,所以特地把它记录下来了,方便下次忘记的时候可以查阅 把鼠标移在你要移除的那个项目上然后按下Delete键 ...

  10. AJAX三

    三.ajax 4.代参数的get方法 ①服务器 ②ajax代码 xhr.open("get",url,true) url="/demo/get_login?uname=& ...