[HDU5592] ZYB's Premutation
[HDU5592] ZYB's Premutation
题目大意:一个由\([1,n]\)组成的数列,但不知道具体排列,但给出每个前缀的逆序对数目,让你还原排列
Solution
创造一颗\([1,n]\)的权值线段树,初始权值都为\(1\),我们从后往前离线处理,每次拿到一个前缀的逆序对数\(p[i]\),说明在\(i\)上的这个数字\(a_i\)前面有\(sum=p[i]-p[i-1]\)个数字比它大,所以\(a_i\)是\(a_1,\cdots ,a_i\)中第\(i -sum\)大的数字,查询后这个\(a_i\)对前面就没有用了,需要在权值线段树上删去
Code
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define sc(x) scanf("%d", &x)
#define pf(x) printf("%d\n",x)
#define lson cur << 1
#define rson (cur << 1) | 1
const int N = 50005;
int num[N << 2], val[N << 2];
int p[N], ans[N];
int n;
void build(int cur, int l, int r){
num[cur] = r - l + 1;
if(l == r){
val[cur] = l;
return;
}else{
int mid = l + ((r - l) >> 1);
build(lson, l, mid);
build(rson, mid + 1, r);
}
}
int query(int cur, int l, int r, int k){
int mid = l + ((r - l) >> 1);
if(l == r){
// pf(val[cur]);
return val[cur];
}else{
if(num[lson] >= k){
return query(lson, l, mid, k);
}else{
return query(rson, mid + 1, r, k - num[lson]);
}
}
}
void update(int cur, int l, int r, int k){
num[cur]--;
if(l == r){
val[cur] = 0;
return;
}else{
int mid = l + ((r - l) >> 1);
if(k <= mid){
update(lson, l, mid, k);
}else{
update(rson, mid + 1, r, k);
}
}
}
int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
memset(num, 0, sizeof(num));
memset(val, 0, sizeof(val));
build(1, 1, n + 1);//l和r要和数组对应起来,不能建的时候是n+1,query和update时又变成了n
for(int i = 1, la; i <= n; ++i){
scanf("%d", &p[i]);
}
for(int i = n; i; --i){
ans[i] = query(1, 1, n + 1, i - p[i] + p[i - 1]);
update(1, 1, n + 1, ans[i]);
}
for(int i = 1; i < n; ++i){
printf("%d ", ans[i]);
}
printf("%d\n", ans[n]);
}
return 0;
}
Error
- \(l\)和\(r\)要和数组对应起来,不能建的时候是\(n+1\),\(query\)和\(update\)时又变成了\(n\)
[HDU5592] ZYB's Premutation的更多相关文章
- ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...
- ZYB's Premutation(有逆序数输出原序列,线段树)
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- BestCoder Round #65 (ZYB's Premutation)
ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdu 5592 ZYB's Premutation (权值线段树)
最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 5592——ZYB's Premutation——————【线段树单点更新、单点查询】
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 线段树 - ZYB's Premutation
ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ...
- Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- ZYB's Premutation POJ5592
Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...
- HDU 5592 ZYB's Premutation
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 题意: http://bestcoder.hdu.edu.cn/contests/contes ...
随机推荐
- NFS 共享存储
目录 环境准备 NFS服务端 NFS客户端 部署时常见报错 httpd服务 NFS 共享存储的坑 环境准备 主机名 WanIP(Wide Area Network) LanIP(Local Area ...
- sizeof和strlen在string类中的使用
字符串的sizeof和strlen 考虑下面的问题: char a[] = "abcdef"; char b[20] = "abcdef"; string s ...
- Eclipce怎么恢复误删类
选择误删除文件在eclipse所在包(文件夹) 在包上单击右键. 选择restore from local history... 在弹出的对话框中选择需要恢复的文件
- js currying All In One
js currying All In One 柯里化 refs https://juejin.im/post/6844903603266650125 xgqfrms 2012-2020 www.cnb ...
- Nuxt.js SSR Optimizing Tips
Nuxt.js SSR Optimizing Tips 性能优化 FP 首次绘制时间 FCP 首次渲染时间 FMP 首屏渲染时间 FI refs https://vueschool.io/articl ...
- Typescript All In One
Typescript All In One TypeScript 3.5 is now available. https://www.typescriptlang.org/#download-link ...
- vue & $router & History API
vue & $router gotoTemplateManage(e) { e.preventDefault(); this.$router.push({ path: `/operate-to ...
- 云原生系列5 容器化日志之EFK
上图是EFK架构图,k8s环境下常见的日志采集方式. 日志需求 1 集中采集微服务的日志,可以根据请求id追踪到完整的日志: 2 统计请求接口的耗时,超出最长响应时间的,需要做报警,并针对性的进行调优 ...
- MySQL切换版本踩坑记录(包括恢复数据方法)
踩坑起因:在创建数据库时, 字段:create_time datetime DEFAULT CURRENT_TIMESTAMP, 报异常--Error Code: 1067 - Invalid def ...
- Python 股票市场分析实战
目标: 1.股票数据获取 2.历史趋势分析及可视化 3.风险分析 实验数据:来源于Yahoo Finance / Stooq,该网站提供了很多API接口,本文用的工具是pandas-datareade ...