[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 ...
随机推荐
- 生成不带签名(BOM)的UTF8格式的XML
生成XML的一种方法如下: using System.Xml; private void SaveXML(string savePath) { XmlWriterSettings setting = ...
- 1.nginx安装和基本配置
作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-10 20:56:10 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...
- Ubuntu第一次使用注意点
第一次装完Ubuntu登录,打开命令行,登录的不是root权限,切换root不成功: 这个问题产生的原因是由于Ubuntu系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...
- HDU 5608 function(莫比乌斯反演 + 杜教筛)题解
题意: 已知\(N^2-3N+2=\sum_{d|N}f(d)\),求\(\sum_{i=1}^nf(i) \mod 1e9+7\),\(n\leq1e9\) 思路: 杜教筛基础题? 很显然这里已经设 ...
- HDU 3065 病毒侵袭持续中(AC自动机 模板)题解
题意:给出主串中每个模式串的个数 思路:毒瘤出题人多组数据没说给的是多组数据. 板子: struct Aho{ struct state{ int next[130]; int fail, cnt; ...
- 洛谷p2216 多次单调队列,扫描矩阵中的最大值减去最小值最的固定大小子矩阵
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int ...
- Vue3(四)从jQuery 转到 Vue工程化 的捷径
不会 webpack 还想学 vue 工程化开发 的福音 熟悉jQuery开发的,学习vue的简单使用是没用啥问题的,但是学习vue的工程化开发方式,往往会遇到各种问题,比如: webpack.nod ...
- 超详细Openstack核心组件——nova部署
目录 OpenStack-nova组件部署 nova组件部署位置 计算节点Nova服务配置(CT配置) 计算节点配置Nova服务-c1节点配置 计算节点-c2(与c1相同)(除了IP地址) contr ...
- C++算法代码——选举学生会
题目来自:https://www.luogu.com.cn/problem/P1271 题目描述 学校正在选举学生会成员,有 n(n\le 999)n(n≤999) 名候选人,每名候选人编号分别从 1 ...
- EasyExcel学习
导入依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</a ...