ZYB's Premutation(有逆序数输出原序列,线段树)
ZYB's Premutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 758 Accepted Submission(s): 359
Pair (i,j)(i<j) is considered as a reverse log if Ai>Aj is matched.
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≤5,1≤N≤50000
3
0 1 2
题意:
已知【1,i】的逆序数,让你还原这个区间;
思路:用数组记录第i个数前面比a[i]大的数的个数;
用线段树记录当前区间的空缺数;类似与买车票插队那道;然后从最后一个数开始,往线段树中插;a[i]越大代表这个数越小;就往左树插,插到相应位置这个位置被用过了;也就是0;
为什么要从后往前插;因为如果从前往后假设一组数1,2,3,4;插得结果会是4,3,2,1;那样就错了;倒插是1,2,3,4;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define T_T while(T--)
#define mem(x,y) memset(x,y,sizeof(x))
#define ll root<<1
#define rr root<<1|1
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define V(x) tree[x]
const int MAXN=50010;
int tree[MAXN<<2],a[MAXN],ans[MAXN];
int nowans;
void pushup(int root){
V(root)=V(ll)+V(rr);
}
void made(int root,int l,int r){
V(root)=r-l+1;
int mid=(l+r)>>1;
if(l==r)return;
made(lson);
made(rson);
}
void query(int root,int l,int r,int v){
if(l==r){
nowans=l;
V(root)=0;
return;
}
int mid=(l+r)>>1;
if(v>=V(rr))query(lson,v-V(rr));
else query(rson,v);
pushup(root);
}
int main(){
int T,N;
SI(T);
T_T{
SI(N);
made(1,1,N);
int cur,last=0;
for(int i=0;i<N;i++){
scanf("%d",&cur);
a[i]=cur-last;
last=cur;
}
for(int i=N-1;i>=0;i--){
query(1,1,N,a[i]);
ans[i]=nowans;
}
for(int i=0;i<N;i++){
if(i)P_;
printf("%d",ans[i]);
}puts("");
}
return 0;
}
ZYB's Premutation(有逆序数输出原序列,线段树)的更多相关文章
- ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...
- FZU2018级算法第二次作业 2.10 逆序数(权值线段树)
题目: Nk 最近喜欢上了研究逆序数,给出一个由 1…n 组成的数列 a1,a2,a3…an, a1的逆序数就是在 a2…an 中,比 a1 小的数的数量,而 a2 的逆序数就是 a3….an 中比 ...
- cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】
http://acm.uestc.edu.cn/#/problem/show/841 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others) Memory ...
- Codevs 1688 求逆序对(权值线段树)
1688 求逆序对 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定一个序列a1,a2,…, ...
- poj 2828 Buy Tickets 【买票插队找位置 输出最后的位置序列+线段树】
题目地址:http://poj.org/problem?id=2828 Sample Input 4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31 ...
- 线段树求逆序数方法 HDU1394&&POJ2299
为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...
- HDU 1394 (逆序数) Minimum Inversion Number
原来求逆序数还可以用线段树,涨姿势了. 首先求出原始序列的逆序数,然后递推每一个序列的逆序数. #include <cstdio> #include <cstring> #in ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- 逆序数 UVALive 6508 Permutation Graphs
题目传送门 /* 题意:给了两行的数字,相同的数字连线,问中间交点的个数 逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数 用归并排序或线段树求.想到了就简单了 ...
随机推荐
- 浅谈SpringMVC(二)
一.SpringMVC的拦截器 1.写类implements HandlerInterceptor public class MyMvcInterceptor implements HandlerIn ...
- 向上取整Ceil,向下取整Floor,四舍五入Round
几个数值函数的功能实现: (1)int Ceil(float f) int Ceil(float f) { int integer = (int)f; if (f > (float)intege ...
- 批量修改文件名java
package test0715; import java.io.File; public class FileRename {public static void main(String[] arg ...
- HTTP 错误 401.3 - Unauthorized由于 Web 服务器上此资源的访问控制列表(ACL)解决办法
对应站点目录的IUSR的权限没设造成的...在属性——>安全——> 高级 中把IUSR用户找出来添加好就OK了 注:IUSR(匿名访问 Internet 信息服务的内置帐户)
- Auto login to your computer
in run dialog, type in following words and enter Rundll32 netplwiz.dll,UsersRunDll On user account d ...
- flume 日志采集工具
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并 ...
- perl 爬取上市公司业绩预告
<pre name="code" class="python">use LWP::UserAgent; use utf8; use DBI; use ...
- JavaSE_ IO流 总目录(19~22)
JavaSE学习总结第19天_IO流119.01 集合的特点和数据结构总结19.02 如何选择使用哪种集合19.03 集合常见功能和遍历方式总结19.04 异常的概述和分类19.05 JVM默认处理异 ...
- String VS Cstring(字符串)
#include<string> 与 #include<string.h> 这是两个完全不同的头文件,前者用于C++,后者用于C,一般把这两个头文件都包括进去. 越来越觉得需要 ...
- SqlHelp
using System.Configuration;using System.Data; public class SqlHelp { private static string connectio ...