Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 175 Accepted Submission(s): 74
restore the premutation.
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
思路:容易想到k = (a[i] - a[i - 1] + 1)就是原序列第i个数的左边比其大的个数,可以从右往左依次计算
用线段树实现:序列中每个存在的位置相当于有一个1,不存在位0,从中找出第k个1所在的位置,找到后删除该数相当于置0
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <cstdlib>
- #include <queue>
- #include <vector>
- #include <map>
- #include <set>
- #include <iostream>
- #define lson l, m, rt << 1
- #define rson m + 1, r, rt << 1|1
- using namespace std;
- const int INF = 0x3f3f3f3f;
- typedef long long ll;
- const int N = ;
- int num[N << ], a[N], ans[N];
- void up(int rt) { num[rt] = num[rt << ] + num[rt << |]; }
- void build(int l, int r, int rt)
- {
- if(l == r) {
- num[rt] = ;
- return ;
- }
- int m = (l + r) >> ;
- build(lson);
- build(rson);
- up(rt);
- }
- void update(int l, int r, int rt, int p)
- {
- if(l == p && r == p) {
- num[rt]--;
- return ;
- }
- int m = (l + r) >> ;
- if(p <= m) update(lson, p);
- else update(rson, p);
- up(rt);
- }
- int pos;
- void get(int l, int r, int rt, int x)
- {
- if(l == r) {
- pos = l;
- return;
- }
- int m = (l + r) >> ;
- if(num[rt << ] < x) get(rson, x - num[rt << ]);
- else get(lson, x);
- }
- int main()
- {
- int _; scanf("%d", &_);
- while(_ --)
- {
- int n; scanf("%d", &n);
- for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
- build(, n, );
- int c = n;
- for(int i = n; i >= ; --i)
- {
- int d = c - (a[i] - a[i - ]);
- get(, n, , d);
- update(, n, , pos);
- // cout << pos << endl;
- ans[i] = pos;
- c--;
- }
- get(, n, , ); ans[] = pos;
- for(int i = ; i < n; ++i) printf("%d ", ans[i]);
- printf("%d\n", ans[n]);
- }
- return ;
- }
Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树的更多相关文章
- hdu 5592 ZYB's Premutation(线段树优化)
设f_ifi是第ii个前缀的逆序对数,p_ipi是第ii个位置上的数,则f_i-f_{i-1}fi−fi−1是ii前面比p_ipi大的数的个数.我们考虑倒着做,当我们处理 ...
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- HDU 5592——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
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 题意: http://bestcoder.hdu.edu.cn/contests/contes ...
- hdu 5592 ZYB's Premutation (权值线段树)
最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 5592 ZYB's Premutation(树状数组+二分)
题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列. 思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字 ...
- HDU - 5592 ZYB's Premutation (权值线段树)
题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...
- BC 65 ZYB's Premutation (线段树+二分搜索)
题目简述:有一个全排列,一直每个前缀区间的逆序对数,还原这个排列. fi记录逆序对数,pi记录该位置数值,则k=fi-f(i-1)表示前i-1个数比pi大的数的个数,那么只要在剩余元素求出按大小顺序第 ...
- HDU 5592 ZYB's Game 【树状数组】+【二分】
<题目链接> 题目大意: 给你一个由1~n,n个数组成的序列,给出他们每个的前缀逆序数,现在要求输出这个序列. 解题分析: 由前缀逆序数很容易能够得到每个数的逆序数.假设当前数是i,它前面 ...
随机推荐
- html与js传json值给php
//一段js代码 var data = {}, act = [], list = []; $('.set').find('input, textarea').each(function() { act ...
- October 3rd 2016 Week 41st Monday
Better to light one candle than to curse the darkness. 与其诅咒黑暗,不如燃起蜡烛. Sitting in the darkness and wa ...
- Codeforces Round #304 C(Div. 2)(模拟)
题目链接: http://codeforces.com/problemset/problem/546/C 题意: 总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张 ...
- java 缩略图
http://www.cnblogs.com/digdeep/p/4829471.html http://www.jb51.net/article/57648.htm http://blog.csdn ...
- CentOS FTP基于虚拟用户的配置
详细可以看:http://www.linuxidc.com/Linux/2013-12/94242.htm 所谓虚拟用户就是没有使用真实的帐户,只是通过映射到真实帐户和设置权限的目的.虚拟用户不能登录 ...
- 【PHP数组的使用】
PHP数组使用关键字array标识,数组内的元素可以是任意类型,而且可以不是同一种类型,这和c.java不同. 遍历数组的方法可以使用foreach,也可以使用for循环 可以使用print_r或者v ...
- [LeetCode] Ugly Number
Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...
- postgresql设置默认的search_path
-- Use this to show the current search_path -- Should return: "$user",public SHOW search_p ...
- C# Qrcode生成二维码支持中文,带图片,带文字 2015-01-22 15:11 616人阅读 评论(1) 收藏
1.下载Qrcode库源码,下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 2.打开源码时,部分类库 ...
- 1-02 启动和停止Sql Sever的服务
启动Sql Sever服务的三种方式 1:后台启动服务. 2:Sql Sever配置管理员启动服务. 3:在运行窗口中使用命令启动和停止服务: 启动:net start mssqlsever. 停止 ...