线段树 - ZYB's Premutation
ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to
restore the premutation.
Pair (i, j)(i < j) is considered as a reverse log if Ai > Aj is matched.
Input
In the first line there is the number of testcases T.
For each teatcase:
In the first line there is one number N.
In the next line there are N numbers Ai,describe the number of the reverse logs of each prefix,
The input is correct.
1 <= T <= 51,1 <= N <= 50000.
Output
For each testcase,print the ans.
Sample Input
1
3
0 1 2
Sample Output
3 1 2
----------------------------------------我是分割线^_^------------------------------------------- 又是一道线段树的题目,普通方法也可以做,只不过肯定超时了,因为用线段树进行优化之后都差点超时= =,
又是逆序数,题目意思是说给出逆序数对数的前缀和,然后让你求出原序列,查看题解后你会发现一个规律:
题目给出的Ai,可以通过与前一项相减获得第i个数在1到n的第几大蛇王位置,第i个数为A(i) - A(i-1) + 1
大,括号中的数为下标,然后通过线段树找得到这个数在剩下的为筛选出去的数中的第几大的位置。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<sstream>
using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 0x3f3f3f3f
#define Int __int64
#define pii pair<int,int> const int MAXN = 55555;
int sum[MAXN<<2];
int num[MAXN];
int ans[MAXN]; void PushUp(int rt) {
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void Build(int l, int r, int rt) {
if (l == r) {
sum[rt] = 1;
return ;
}
int m = (l + r)>>1;
Build(lson);
Build(rson);
PushUp(rt);
}
void UpDate(int pos, int l, int r, int rt) {
if (l == r) {
sum[rt] = 0;
return ;
}
int m = (l + r)>>1;
if (pos <= m) UpDate(pos, lson);
else UpDate(pos, rson);
PushUp(rt);
}
int Query(int value, int l, int r, int rt) {
if (l == r) {
return r;
}
int ret;
int m = (l + r)>>1;
if (value - sum[rt<<1|1] > 0) ret = Query(value - sum[rt<<1|1], lson);/*注意理解这里,
如果第几大的位置超过右边的范围了,就去左边找*/
else ret = Query(value, rson);
return ret;
}
int main() {
//freopen("input.txt", "r", stdin);
int T;
while (cin>>T) {
while (T--) {
int n;
cin>>n;
memset(sum, 0, sizeof(sum));
Build(1, n, 1);
for (int i = 0; i < n; i++) {
cin>>num[i];
}
for (int i = n - 1; i >= 1; i--) {
int t = num[i] - num[i - 1] + 1;
ans[i] = Query(t, 1, n, 1);
UpDate(ans[i], 1, n, 1);
}
ans[0] = Query(1, 1, n, 1);
for (int i = 0; i < n; i++) {
cout<<ans[i];
if (i != n - 1) cout<<" ";
else cout<<endl;
}
}
}
return 0;
}
线段树 - ZYB's Premutation的更多相关文章
- 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(有逆序数输出原序列,线段树)
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
- BC 65 ZYB's Premutation (线段树+二分搜索)
题目简述:有一个全排列,一直每个前缀区间的逆序对数,还原这个排列. fi记录逆序对数,pi记录该位置数值,则k=fi-f(i-1)表示前i-1个数比pi大的数的个数,那么只要在剩余元素求出按大小顺序第 ...
- HDU - 5592 ZYB's Premutation (权值线段树)
题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...
- hdu 5592 ZYB's Premutation(线段树优化)
设f_ifi是第ii个前缀的逆序对数,p_ipi是第ii个位置上的数,则f_i-f_{i-1}fi−fi−1是ii前面比p_ipi大的数的个数.我们考虑倒着做,当我们处理 ...
- [HDU5592] ZYB's Premutation
[HDU5592] ZYB's Premutation 题目大意:一个由\([1,n]\)组成的数列,但不知道具体排列,但给出每个前缀的逆序对数目,让你还原排列 Solution 创造一颗\([1,n ...
- BestCoder Round #65 (ZYB's Premutation)
ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- web加密的基本概念
1.需求 了解web加密的一些基础概念. 2.基本概念 a.对称加密方式 对称加密方式 加密和解密用同一个密钥 不足之处是,交易双方都使用同样钥匙,安全性得不到保证.此外,每对用户每次使用对称加密算法 ...
- HTML5复习整理
一.推出的目标 web浏览器兼容性低:文档结构不明确:web应用程序的功能受限 二.语法的改变 内容类型(html或htm):DOCTYPE声明简化:指定字符编码简化:可以省略标记的元素:具有Bool ...
- 各种Android手机Root方法
Root的介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别的管理权限.我们root手机的过程 ...
- 在CentOS下搭建自己的Git服务器
首先需要装好CentOS系统,作为测试,你可以选择装在虚拟机上,这样比较方便.这步默认你会,就不讲了.有了CentOS,那么如何搭建Git服务器呢?1.首先需要安装Git,可以使用yum源在线安装: ...
- GridBagConstraints 参数解释
GridBagConstraints 的具体参考为: http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/awt ...
- [转]C/C++ 程序员必须收藏的资源大全
from: https://github.com/jobbole/awesome-cpp-cn C++ 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome – XXX 系列 ...
- Java面试常见知识点总结(三)
21.volatile关键字: 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: (1) 保证了不同线程对这个变量进行操作时的可见性,即一个线程 ...
- NFC读写实例
package com.sy.nfc.test; import java.io.IOException; import android.nfc.NdefMessage; import android. ...
- Python-内置函数小结
内建函数,Python内置的函数(build in function),不需要引用其他包,一般成为BIF abs() 计算绝对值,abs(-10),接收number,返回一个number ma ...
- git 的基本使用
git 的使用步骤: 1. 新建一个文件夹,然后进入终端, 2. cd <文件夹路径> ——->进入当前目录: 2.psw ————>查看当前路径 3.git init ...