codeforces 1353D(优先队列)
题意描述
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]:=i (where i is the number of the current action), otherwise (if r−l+1 is even) assign (set) a[l+r−12]:=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.
思路
因为每次要找到长度最长的靠左的0数组,所以我们可以使用优先队列来优化,然后模拟操作即可。
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=2*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int a[N];
struct node{
int l,r,len;
bool operator >(const node& b)const{
if(len!=b.len) return len>b.len;
return l<b.l;
}
bool operator <(const node& b) const{
return !(*this>b);
}
};
void solve(){
int n;cin>>n;
priority_queue<node> pq;
pq.push({1,n,n});
rep(i,1,n+1){
node t=pq.top();pq.pop();
int l=t.l,r=t.r,len=t.len;
if((r-l+1)%2==0){
int idx=(l+r-1)/2;
a[idx]=i;
if(idx!=l){
int newl=l,newr=idx-1,newlen=newr-newl+1;
pq.push({newl,newr,newlen});
newl=idx+1,newr=r,newlen=newr-newl+1;
pq.push({newl,newr,newlen});
}else{
int newl=l+1,newr=r,newlen=newr-newl+1;
pq.push({newl,newr,newlen});
}
}else{
int idx=(l+r)/2;
a[idx]=i;
if(idx!=l){
int newl=l,newr=idx-1,newlen=newr-newl+1;
pq.push({newl,newr,newlen});
newl=idx+1,newr=r,newlen=newr-newl+1;
pq.push({newl,newr,newlen});
}else{
int newl=l+1,newr=r,newlen=newr-newl+1;
pq.push({newl,newr,newlen});
}
}
}
rep(i,1,n+1) cout<<a[i]<<' ';
cout<<endl;
}
int main(){
IOS;
int t;cin>>t;
while(t--){
solve();
}
return 0;
}
- MySQL 5.6, 5.7并行复制测试(二)(r12笔记第10天)
- MySQL主从报错解决:Failed to initialize the master info structure
- 使用pt工具检测MySQL主从延迟(r12笔记第7天)
- WordPress发布/更新文章、提交/审核评论自动清理VeryCloud缓存
- MySQL中的binlog和redo浅析(r12笔记第5天)
- WordPress发布/更新文章、提交/审核评论自动清理腾讯云CDN缓存
- 使用sysbench压力测试MySQL(一)(r11笔记第3天)
- 分享一下php7和其他php多版本共存的配置教程
- 使用sysbench压力测试MySQL(二)
- 使用sysbench压力测试MySQL(三)(r12笔记第6天)
- 解决IE响应式的解决方案css3-mediaqueries.js不生效问题
- MySQL传输表空间小结(r12笔记第2天)
- swingbench压测Oracle小记(r12笔记第20天)
- 分享最近对网站外链跳转页面代码的一些改善
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 如何利用Gitlab-ci持续部署到远程机器(详细教程)
- Linux常用命令之grep命令用法详解
- 详解Linux动态库生成与使用指南
- Vue 3 入门基础知识
- 在Linux系统中使用Vim读写远程文件的命令详解
- Vue.js实现咸鱼底部Tab凸起|vue自定义导航条组件
- 在Linux中查看所有正在运行的进程的方法
- Apache跨域资源访问报错问题解决方案
- Windows Telemetry服务特权提升
- 滥用DComposition在外部窗口上渲染
- 通过LUMP_PAKFILE的源引擎内存损坏
- MongoDB Change Stream之一——上手及初体验
- MongoDB 慢日志字段解析
- leetcode哈希表之第一个只出现一次的字符
- 转录组RNA-Seq使用docker+bioconda搭建分析环境