[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的更多相关文章

  1. ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...

  2. ZYB's Premutation(有逆序数输出原序列,线段树)

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. BestCoder Round #65 (ZYB's Premutation)

    ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  4. hdu 5592 ZYB's Premutation (权值线段树)

    最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  5. HDU 5592——ZYB's Premutation——————【线段树单点更新、单点查询】

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. 线段树 - ZYB's Premutation

    ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ...

  7. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. ZYB's Premutation POJ5592

    Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...

  9. HDU 5592 ZYB's Premutation

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

随机推荐

  1. JPG学习笔记1(附完整代码)

    #topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...

  2. 智能社讲解js基础

    一,Javascript的组成ECMAScript:翻译,核心,解释器,DOM:文档对象模型,Document Object Model 操作HTML的能力,document对象BOM:浏览器对象模型 ...

  3. markdown嵌入图片

    这里嵌入指不会因为本地文件丢失而丢失. 参考:https://blog.csdn.net/testcs_dn/article/details/78952358 https://blog.csdn.ne ...

  4. codeforces 1C (非原创)

    C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...

  5. document.URL vs window.location.href All In One

    document.URL vs window.location.href All In One document.URL 与 window.location.href 两者有啥区别 document. ...

  6. dynamic creat svg in js

    dynamic creat svg in js SVG title https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title < ...

  7. vscode Paste Image插件使用

    Paste Image 在编写md需要插入图片,这个插件可以将粘贴板的图片保存到本地资源 假如我在/readme.md中编写文档,我需要将粘贴板的图片放在/images/下面,配置两个关键配置即可: ...

  8. Java基础篇(04):日期与时间API用法详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.时间和日期 在系统开发中,日期与时间作为重要的业务因素,起到十分关键的作用,例如同一个时间节点下的数据生成,基于时间范围的各种数据统计和分 ...

  9. 百度 Apollo无人车平台增加传感器

    https://github.com/ApolloAuto/apollo/issues/1649 如果想加入一个新的传感器不是百度官方推荐的传感器到Apollo平台做法: First you can ...

  10. 【转】ROS之topic和service通信比较

    实验速度 1. via topic 上图是以前ROS课上做的一个实验,内容是测试一个publisher和一个subscriber之间通讯所用的时间.两个node都很简单,publisher发送一个字符 ...