题目链接:点击查看

题目大意:给出一个长度为 n ,初始时全部为 0 的数组 a ,后续进行 n 次操作,每次操作找到最长的连续 0 ,如果有多个则选择位置最靠左边的,将这组连续 0 的,最中间位置的数赋值为 i ,i 为第 i 次操作,输出最后的数列

题目分析:一开始没什么思路,用线段树区间合并暴力模拟的,果不其然 TLE 了,其实自己手算模拟几次就会发现,每个位置至多会被遍历到一次,且是按照深度扩展的,所以我们可以用 bfs + 优先队列 不断扩展就好了,具体实现可以看代码

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;int ans[N],pos;struct Node
{int l,r;Node(int l,int r):l(l),r(r){}bool operator<(const Node& t)const{if(r-l+1!=t.r-t.l+1)return r-l+1<t.r-t.l+1;return l>t.l;}
};void bfs(int l,int r)
{priority_queue<Node>q;q.push(Node(l,r));while(q.size()){Node cur=q.top();q.pop();l=cur.l;r=cur.r;int mid=l+r>>1;ans[mid]=++pos;if(l==r)continue;if(mid+1<=r)q.push(Node(mid+1,r));if(l<=mid-1)q.push(Node(l,mid-1));}
}int main()
{
#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);int w;cin>>w;while(w--){int n;scanf("%d",&n);pos=0;bfs(1,n);for(int i=1;i<=n;i++)printf("%d ",ans[i]);puts("");}return 0;
}

CodeForces - 1353D Constructing the Array(bfs)相关推荐

  1. CodeForces 1514A Perfectly Imperfect Array

    CodeForces 1514A Perfectly Imperfect Array 题意: 给你n个数,是否存在一个数不是平方数 题解: 先开方,转int,判断是否等于平方 代码: #include ...

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

    题意: 有长度为 n 的数组 a ,全为 0,接下来循环 n 次,每次选出一段最长的连续区间 [l, r](全为 0 ,如果一样长,就选最左边的). 如果 r−l+1 是奇数,那么 a[l+r2]=i ...

  3. CodeForces - 1534E Lost Array(bfs+交互)

    题目链接:点击查看 题目大意:初始时给出一个长度为 nnn 的序列,每次可以询问 kkk 个位置的异或和,现在需要以最少的询问获得整个序列的异或和 题目分析:因为是异或,我们只关心每个位置被询问的次数 ...

  4. Educational Codeforces Round 11A. Co-prime Array 数学

    地址:http://codeforces.com/contest/660/problem/A 题目: A. Co-prime Array time limit per test 1 second me ...

  5. CodeForces - 1480D2 Painting the Array II(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 小.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

  6. CodeForces - 1480D1 Painting the Array I(贪心)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 大.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

  7. Codeforces 540C Ice Cave (BFS)

    题目:http://codeforces.com/problemset/problem/540/C 代码: #include<stdio.h> #include<string.h&g ...

  8. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

  9. codeforces Gargari and Permutations(DAG+BFS)

    1 /* 2 题意:求出多个全排列的lcs! 3 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 4 最后用b ...

最新文章

  1. final 修饰方法参数
  2. 搜索专题【2010】四2.过河问题
  3. HDFS文件导出本地合并为一个文件
  4. JS-循环(while,for,嵌套)-跳转语句(break,continue)
  5. oracle内存最多用到2g,在Oracle数据库中如何使用超过2G内存
  6. python argparse_Python 命令行之旅——初探 argparse
  7. 矩池云上使用nohup和让任务后台运行
  8. yii2 关系...
  9. RDS数据订阅服务使用说明
  10. (转)如何编写testbench的总结(非常实用的总结)
  11. 读Ext之九(事件管理)
  12. Python数据探索性分析和预处理
  13. 如何把图片制作做成GIF表情包?分享在线快速制作GIF图片的方法
  14. ElasticSearch 7.15.2 使用java canal 接入实现灵活化增量数据准实时同步
  15. vim 剪贴板历史插件
  16. siggraph_SIGGRAPH的Unity
  17. iVMS-4200 Vs区别_34973足球推荐分析 英冠 03:45 布伦特福德 VS 诺维奇
  18. SQL查询语句练习(一)
  19. 应用程序存在文件包含漏洞(Unix系统) WASC Threat Classification 解决办法
  20. UCOS操作系统基础

热门文章

  1. 查看tcp连接的命令
  2. 查询常量、表达式、函数
  3. OpenResty快速入门
  4. 事务注解 @Transactional
  5. 代理模式中的静态代理
  6. 自己写一个实现ApplicationListener​接口并且把该组件加入到容器中
  7. webpack打包js文件
  8. ThreadLocal的好处
  9. SpringAOP概念
  10. 数据库-优化-数据库系统配置优化-配置文件优化