Constructing the Array CodeForces - 1353D(数据结构+分类+建设性算法)

题意:

有长度为 n 的数组 a ,全为 0,接下来循环 n 次,每次选出一段最长的连续区间 [l, r](全为 0 ,如果一样长,就选最左边的)。

如果 r−l+1 是奇数,那么 a[l+r2]=ia[\frac{l+r}{2}]=ia[2l+r]=i;

否则,a[l+r−12]=ia[\frac{l+r-1}{2}]=ia[2l+r1]=i;(i 是第几轮循环)。

输出最终的数组 a。

题目:

You are given an array a of length n consisting of zeros. You perform n actions with this array: during the i-th action, the following sequence of operations appears:

Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one;
Let this segment be [l;r]. If r−l+1 is odd (not divisible by 2) then assign (set) a[l+r2]=ia[\frac{l+r}{2}]=ia[2l+r]=i(where i is the number of the current action), otherwise (if r−l+1 is even) assign (set) a[l+r−12]=ia[\frac{l+r-1}{2}]=ia[2l+r1]=i.
Consider the array a of length 5 (initially a=[0,0,0,0,0]). Then it changes as follows:

Firstly, we choose the segment [1;5] and assign a[3]:=1, so a becomes [0,0,1,0,0];
then we choose the segment [1;2] and assign a[1]:=2, so a becomes [2,0,1,0,0];
then we choose the segment [4;5] and assign a[4]:=3, so a becomes [2,0,1,3,0];
then we choose the segment [2;2] and assign a[2]:=4, so a becomes [2,4,1,3,0];
and at last we choose the segment [5;5] and assign a[5]:=5, so a becomes [2,4,1,3,5].
Your task is to find the array a of length n after performing all n actions. Note that the answer exists and unique.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (1≤n≤2⋅105) — the length of a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — the array a of length n after performing n actions described in the problem statement. Note that the answer exists and unique.

Example
Input
6
1
2
3
4
5
6
Output
1
1 2
2 1 3
3 1 2 4
2 4 1 3 5
3 4 1 5 2 6

分析:

我们可以直接暴力去做。即我们每次选出符合条件的 [l, r],然后对应的给 a[i] 赋值,又得到了两个新的更小的区间,我们需要存储下来,并按照上述的规则对所有的区间排序。显然优先队列可以完美的满足我们的要求。优先队列的BFS。每次处理完一段区间后就把这段区间拆分,丢进优先队列里就好。注意priority_queue本身是一个大根二叉堆,所以重载运算符时符号要反一下
(或者按照蓝书讲的 把len换成相反数)。
#AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=2e5+10;
int s[M];
int t,n,tot;
struct node
{int l,r,d;
};
bool operator<(const node &a,const node &b)
{if(a.d<b.d)return 1;else if(a.d==b.d)if(a.l>b.l)return 1;return 0;
}
int main()
{scanf("%d",&t);while(t--){tot=0;memset(s,0,sizeof(s));scanf("%d",&n);priority_queue<node>q;node u,v;u.l=1,u.r=n,u.d=u.r-u.l+1;q.push(u);while(!q.empty()){u=q.top();q.pop();int x=u.l;int y=u.r;int mid=(x+y)>>1;s[mid]=++tot;//printf("%d****\n",s[mid]);v.l=x,v.r=mid-1,v.d=v.r-v.l+1;if(v.l<=v.r)q.push(v);v.l=mid+1,v.r=y,v.d=v.r-v.l+1;if(v.l<=v.r)q.push(v);}for(int i=1;i<=n;i++)printf("%d ",s[i]);printf("\n");}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309736.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[翻译]用于.NET Core的Windows窗体设计器发布

本文由微信公众号《开发者精选资讯》翻译首发&#xff0c;转载请注明来源今天我们很高兴地宣布&#xff0c;.NET Core 项目的 Windows 窗体设计器现在可以在 Visual Studio 2019 16.6 版中作为预览使用&#xff01;我们在 Visual Studio 16.7 预览版 1 中也提供了更新的设计器版…

运动会加油稿计算机学院,信息工程学院运动会加油稿

信息工程学院运动会加油稿1.你的汗水洒在跑道&#xff0c;浇灌着成功的花朵开放。你的欢笑飞扬在赛场&#xff0c;为班争光数你最棒。跑吧&#xff0c;追吧在这广阔的赛场上&#xff0c;你似骏马似离铉的箭。跑吧&#xff0c;追吧你比虎猛比豹强2你们挥舞着充满力量的双臂看着实…

K-periodic Garland CodeForces - 1353E(暴力+贪心+dp)

题意&#xff1a; 给定长为 n 的 0, 1 字符串&#xff0c;你可以通过一次操作改变一个字符&#xff08;0 变 1 or 1 变 0&#xff09;&#xff0c;问最少几次操作可以使任意相邻两个 1 之间的距离为 k ? 题目&#xff1a; You are given a garland consisting of n lamps. …

【视频回放与课件】零基础入门AI开发

今天上午&#xff0c;受广州图书馆邀请&#xff0c;在第一讲《零代码上手人工智能》的基础上&#xff0c;以《零基础入门AI开发》为主题&#xff0c;分四步解锁人工智能学习的概念与开发工具&#xff0c;让您在一小时内轻松掌握人工智能开发要领。本次课程内容主要包括&#xf…

三年级计算机群鸭戏水教案导入,三年级下册信息技术教案-3.7群鸭戏水-插入自选图形|清华版.doc...

第七课??《群鸭戏水——自选图形》??【教学内容分析】?本课&#xff0c;是小学信息技术(清华版)三年级下册第七课——自选图形的内容&#xff0c;通过前面章节的学习&#xff0c;学生已经学会了插入剪贴画和图片的操作。本节课创设了森林里要开动物运动会&#xff0c;请同…

Sequence with Digits CodeForces - 1355A(暴力+数学)

题意&#xff1a; 定义&#xff1a; an1anminDigit(an)maxDigit(an)。 给定 a1 和 k&#xff0c;求 ak &#xff1f; 题目&#xff1a; Let’s define the following recurrence: an1anminDigit(an)⋅maxDigit(an). Here minDigit(x) and maxDigit(x) are the minimal and …

Redis背后的故事

导语Redis已成为世界上最受欢迎的数据库之一&#xff0c;但当初正是因为Sanfilippo对数据库“缺乏经验”&#xff0c;使他敢于打破“良好”数据库工程的各种神圣规则&#xff0c;创建了Redis。正文如果Redis之父萨尔瓦多桑菲利波普&#xff08;Salvatore Sanfilippo&#xff09…

C++实现AOE网中的关键路径算法(邻接表存储)

代码如下: #include <iostream> #include <stack> #include <string> using namespace std; const int N 10010; using vnodeType int;typedef struct Node {int adj;int tw;//弧的时间权值Node *next; }Node;typedef struct Vnode {vnodeType v;//存储图…

哈工大威海计算机组成原理,哈工大威海计算机组成原理复习.pdf

第一章 绪论1.1 计算机的产生与发展现代计算机的发展电子管时代晶体管时代集成电路时代超大规模集成电路时代1.2 冯.诺伊曼计算机模型冯诺伊曼计算机的组成&#xff0c;各部分的作用.冯诺伊曼计算机的特点.(1) 计算机由运算器、存储器、控制器和输入设备、输出设备五大部件组成…

Minimal Square CodeForces - 1360A(简单思维和图形判断)

题意&#xff1a; 给你两个大小一样的&#xff0c;边长为a&#xff0c;b的矩形将其放入一个正方形里&#xff0c;问怎样放可以使正方形面积最小&#xff08;要求正方形边和矩形边平行&#xff09; 题目&#xff1a; Find the minimum area of a square land on which you ca…

基于 abp vNext 和 .NET Core 开发博客项目 - 接入GitHub,用JWT保护你的API

上一篇文章再次把Swagger的使用进行了讲解&#xff0c;完成了对Swagger的分组、描述和开启小绿锁以进行身份的认证授权&#xff0c;那么本篇就来说说身份认证授权。开始之前先搞清楚几个概念&#xff0c;请注意认证与授权是不同的意思&#xff0c;简单理解&#xff1a;认证&…

安徽计算机学业水平测试内容,【2017年整理】安徽省学业水平测试信息技术(必修)知识点.doc...

【2017年整理】安徽省学业水平测试信息技术(必修)知识点第一章 信息与信息技术1、香农信息是用来消除不确定性的东西维纳信息就是信息&#xff0c;不是物质&#xff0c;也不是能量钟义信信息是事物运动的状态和方式物质能量是构成世界的三大要素。信息一报纸是信息报上登载的足…

Honest Coach CodeForces - 1360B(简单贪心)

题目&#xff1a; 把所给的数组分成a和b两个子数组&#xff08;元素不重复使用&#xff09;&#xff0c;令a数组的的最大值和b数组的最小值的差最小&#xff0c;并输出。 题意&#xff1a; There are n athletes in front of you. Athletes are numbered from 1 to n from l…

第五站 使用winHex利器加深理解数据页

这篇我来介绍一个winhex利器&#xff0c;这个工具网上有介绍&#xff0c;用途大着呢&#xff0c;可以用来玩数据修复&#xff0c;恢复删除文件等等。。。。它能够将一个file解析成hex形式&#xff0c;这样你就可以对hex进行修改&#xff0c;然后你就可以看到修复后的结果&#…

法国 计算机金融 大学,捷报|GPA3.0,计算机转申金融,斩获法国顶级商学院录取!...

原标题&#xff1a;捷报|GPA3.0&#xff0c;计算机转申金融&#xff0c;斩获法国顶级商学院录取&#xff01;NutsCongratulationsNuts北大学员,GPA3.0计算机转申金融&#xff0c;斩获✨ 全法排名第二ESSEC金融录取✨OfferESSEC法国著名学府埃塞克高等商学院(cole suprieure des…