BestCoder Round #65 HDOJ5592 ZYB's Premutation(树状数组+二分)
ZYB's Premutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 638 Accepted Submission(s): 302
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.
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
1
3
0 1 2
3 1 2
题目链接:点击打开链接
能够想到a[i] - a[i - 1]为i前比a[i]大的数的个数, 从后向前遍历, 通过二分与树状数组确定i应当填入的位置.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e4 + 5;
int n, a[MAXN], c[MAXN], ans[MAXN];
int lowbit(int x)
{
return x & (-x);
}
void update(int x, int y)
{
while(x <= n) {
c[x] += y;
x += lowbit(x);
}
}
int get_sum(int x)
{
int sum = 0;
while(x > 0) {
sum += c[x];
x -= lowbit(x);
}
return sum;
}
int binary_search(int x)
{
int l = 1, r = n, m;
while(l <= r) {
m = (l + r) >> 1;
if(get_sum(m) >= x) r = m - 1;
else l = m + 1;
}
return l;
}
int main(int argc, char const *argv[])
{
int t;
scanf("%d", &t);
while(t--) {
memset(c, 0, sizeof(c));
memset(ans, 0, sizeof(ans));
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for(int i = n; i >= 1; --i)
a[i] -= a[i - 1];
for(int i = 1; i <= n; ++i)
update(i, 1);
for(int i = n; i >= 1; --i) {
int pos = i - a[i] - 1;
ans[i] = binary_search(pos + 1);
update(ans[i], -1);
}
for(int i = 1; i < n; ++i)
printf("%d ", ans[i]);
printf("%d\n", ans[n]);
}
return 0;
}
BestCoder Round #65 HDOJ5592 ZYB's Premutation(树状数组+二分)的更多相关文章
- HDU 5592 ZYB's Premutation(树状数组+二分)
题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列. 思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字 ...
- BestCoder Round #65 (ZYB's Premutation)
ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...
- BestCoder Round #65 (ZYB's Game)
ZYB's Game Accepts: 672 Submissions: 1207 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- BestCoder Round #65 (ZYB's Biology)
ZYB's Biology Accepts: 848 Submissions: 1199 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 13 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- HDU 5592 ZYB's Game 【树状数组】+【二分】
<题目链接> 题目大意: 给你一个由1~n,n个数组成的序列,给出他们每个的前缀逆序数,现在要求输出这个序列. 解题分析: 由前缀逆序数很容易能够得到每个数的逆序数.假设当前数是i,它前面 ...
- Codeforces Round #381 (Div. 2) D dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组
D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...
随机推荐
- ubuntu -redis
ubentu 布置redis,基本操作和CentO感觉相差不多,主要是使用命令有所差异 mark如下: ① download ② tar -zxvf xxx.tar.gz ③ cd redis-xxx ...
- 今日SGU 6.6
sgu 177 题意:给你一个一开始全是白色的正方形,边长为n,然后问你经过几次染色之后,最后的矩形里面 还剩多少个白色的块 收获:矩形切割,我们可以这么做,离散处理,对于每次染黑的操作,看看后面有没 ...
- Android studio树形
原创作品,允许转载,转载时请务必声明作者信息和本声明. http://www.cnblogs.com/zhu520/p/8349553.html 这个是上网找了好久才弄出来的,我把我上网找的总结也写 ...
- TIME定时器
一.定时器分类 STM32F1 系列中,除了互联型的产品,共有 8 个定时器,分为基本定时器,通用定时器和高级定时器.基本定时器 TIM6 和 TIM7 是一个 16 位的只能向上计数的定时器,只能定 ...
- python main函数
关于Python的主(main)函数问题 2007-07-23 19:14 初次接触Python的人会很不习惯Python没有main主函数.这里简单的介绍一下,在Python中使用main函数的方法 ...
- zookeeper_相关命令 以及 API
(区分大小写) 启动ZooKeeper服务 进入主目录下的 /bin 文件夹. zkServer.sh start. 需要每个节点运行启动命令 客户端启动 zkCli ...
- MyEclipse连接不上genymotion的解决方式
奇怪的是我的MyEclipse有时候连接得上genymotion,有时候又连接不上.之前连接不上的时候,就直接用真机调试,因此出现这个问题非常久了一直都没有去找解决方式.今天认真的反省了自己,再也不能 ...
- HTML5学习笔记简明版(11):新增的API
HTMLDocument上的扩展 HTML5在DOM Level 2 HTML上扩展了一些HTMLDocument的接口.这些接口在全部实现了Document接口的元素对象上进行了实现. HTML5在 ...
- 智课雅思词汇---六、fer是什么意思
智课雅思词汇---六.fer是什么意思 一.总结 一句话总结:词根:fer = to carry(拿), to bring(带来), to bear(负担, 1.equ是什么意思? 词根:-equ- ...
- 流量数据iftop命令
yum install flex byacc libpcap ncurses ncurses-devel libpcap-devel tar zxvf iftop-0.17.tar.gz cd ift ...