HDU 5592——ZYB's Premutation——————【线段树单点更新、单点查询】
2015-12-08 14:55
447 查看
ZYB's Premutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 637 Accepted Submission(s): 301
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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≤5,1≤N≤50000
[align=left]Output[/align]
For each testcase,print the ans.
[align=left]Sample Input[/align]
1
3
0 1 2
[align=left]Sample Output[/align]
3 1 2
[align=left]Source[/align]
BestCoder Round #65
题目大意:给你一个长度为n的序列。给出前缀的逆序,问你原始序列是什么。
解题思路:首先处理出来每个数的逆序。然后从后处理,如果逆序为2,那么这个数是当前所有数中的第3大的数。那么在线段树中查找第3个为1的位置。 这是个偏想法的比较有意思的题。
#include<stdio.h> #include<algorithm> #include<string.h> #include<vector> using namespace std; #define mid (L+R)/2 #define lson rt*2,L,mid #define rson rt*2+1,mid+1,R const int maxn = 55000; struct SegTree{ int sum; }segs[maxn*4]; int a[maxn],ans[maxn]; void PushUp(int rt){ segs[rt].sum = segs[rt*2].sum + segs[rt*2+1].sum; } void buildtree(int rt,int L,int R){ if(L == R){ segs[rt].sum = 1; return ; } buildtree(lson); buildtree(rson); PushUp(rt); } int query(int rt,int L,int R,int k){ if(L == R){ segs[rt].sum = 0; return L; } int ret ; if(segs[rt*2+1].sum >= k){ ret = query(rson,k); }else{ ret = query(lson,k-segs[rt*2+1].sum); } PushUp(rt); return ret; } int main(){ int T,n; scanf("%d",&T); while(T--){ scanf("%d",&n); buildtree(1,1,n); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); } for(int i = n; i >= 1; i--){ ans[i] = query(1,1,n,a[i]-a[i-1]+1); } printf("%d",ans[1]); for(int i = 2; i <= n; i++){ printf(" %d",ans[i]); }puts(""); } return 0; }
相关文章推荐
- error C2039: “SetDefaultDllDirectories”: 不是“`global namespace'”的成员
- Oracle 的 rownum 问题
- .JsonParseException: Invalid UTF-8 start byte 0xb2
- 网络爬虫基本工作流程和抓取策略
- 《计算机图形学与几何造型导论》读书笔记1
- petaPar培训文档
- 等参元的高斯积分详解
- 水平集函数具体实现
- 分片实验的实现
- 自由表面处理,表面张力的实现
- 高等有限元中质量矩阵问题
- 扩展有限元中增强函数实例
- 光滑粒子流体动力学学习随笔
- 扩展有限元学习随笔
- 网格与无网格
- 离散方法(二)——有限差分方法(FDM)
- 离散方法(一)——有限单元方法(FEM)
- 离散方法(三)——光滑粒子流体动力学(SPH)
- 《矢量分析与场论》
- [Navicat] 连接Oracle报错_Cannot load OCI DLL 87