HDU 5592 ZYB's Premutation(树状数组+二分)
题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列。
思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字之外,它是在剩下的数字当中第k+1大的。
知道这个之后,可以用树状数组来帮助找出剩下的数中第k大的数,刚开始我们可以让1~n中每个元素都标记为1,那么他们的前缀和就代表它是第几小。所以,我们可以对于他们的和来二分快速寻找第k大数。其实在树状数组里面是按照第(i-k)小来找的。找完之后要删除这个元素的值。将它标记为0;
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = ;
int f[maxn];
int c[maxn];
int ans[maxn];
int lowbit(int x)
{
return x & (-x);
}
void add(int pos, int num, int n)
{
while (pos <= n)
{
c[pos] += num;
pos += lowbit(pos);
}
}
int getSum(int pos)
{
int sum = ;
while (pos > )
{
sum += c[pos];
pos -= lowbit(pos);
}
return sum;
}
void init(int n)
{
memset(c, , sizeof(c));
for (int i = ; i <= n; i++)
add(i, , n);
}
int search(int k, int n)
{
int l = , r = n, mid;
while (l <= r)
{
mid = (l + r) / ;
int sum = getSum(mid);
if (sum >= k) r = mid - ;
else l = mid + ;
}
return l;
}
int main()
{
int T, n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
init(n);
for (int i = ; i <= n; i++)
scanf("%d", &f[i]);
for (int i = n; i >= ; i--)
{
int k = f[i] - f[i - ];//第k+1大
k = i - k;//在剩下的里面第i - k 小
int pos = search(k, n);//二分找出第k小的位置
ans[i] = pos;
add(pos, -, n);
}
for (int i = ; i < n; i++) printf("%d ", ans[i]);
printf("%d\n", ans[n]);
}
return ;
}
HDU 5592 ZYB's Premutation(树状数组+二分)的更多相关文章
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...
- BestCoder Round #65 HDOJ5592 ZYB's Premutation(树状数组+二分)
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 2852 KiKi's K-Number 树状数组 + 二分
一共最多才100000个数,并且数值范围0~100000. 树状数组 C[i] 记录数值为 i 的数有多少个. 删除时如果Query( a ) - Query( a - 1 ) == 0 则该数不存在 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 2852 KiKi's K-Number 树状数组
先补充从n个数中求第k小数的理论知识........ 睡觉去~ ------------------------------------------又要睡觉的分割线------------------ ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- TZOJ 4602 高桥和低桥(二分或树状数组+二分)
描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
随机推荐
- jQuery设置按钮被点击状态
js和jquery如何使按钮失效,很简单,只要设置disabled属性为true即为不可用状态即可 1.JS方法一: document.getElementByIdx("btn") ...
- BZOJ 3994 约数个数和
Description 设\(d(x)\)为\(x\)的约数个数,给定\(N,M\),求\[\sum_{i=1}^{N}\sum_{j=1}^{M}d(ij)\]. Input 输入文件包含多组测试数 ...
- Innodb和MyISAM比较
Innodb和MyISAM比较 (1)MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快 (2)MyISAM不支持事务.外键,InnoDB支持事务和外键 (3)MyISAM使用的表级锁 ...
- 14.7.2 Changing the Number or Size of InnoDB Redo Log Files 改变InnoDB Redo Log Files的数量和大小
14.7.2 Changing the Number or Size of InnoDB Redo Log Files 改变InnoDB Redo Log Files的数量和大小 改变 InnoDB ...
- SQLite: sqlite_master
SQLite数据库中一个特殊的名叫 SQLITE_MASTER 上执行一个SELECT查询以获得所有表的索引.每一个 SQLite 数据库都有一个叫 SQLITE_MASTER 的表, 它定义数据库的 ...
- 【HDOJ】2988 Dark roads
最小生成树. /* */ #include <iostream> #include <string> #include <map> #include <que ...
- poj1150
这道题告诉我们递推一定要慢慢细细的推Pmn=n!/m!,我们可以先考虑n!的最后一位是什么首先最后一位非0位我们首先想到把0都干掉也就是先把2和5提出来,这两个其实是同样的方法对于N!中有多少个因数2 ...
- 转 @RenderBody()和@RenderSection()
强大的Razor引擎 一.Razor基础简介 Razor采用了cshtml后缀的文件名,截图如下: A. 版面布局 从图上看到,新的视图引擎已经没有了Site.Master这种MasterPage了, ...
- 嵌入式系统烧写uboot/bootloader/kernel的一般方法
嵌入式系统烧写uboot/bootloader/kernel的一般方法 本文介绍了在嵌入式系统中烧写uboot/bootloader/kernel 的一般方法,以及如果uboot或者内核出现错误, ...
- HDOJ/HDU 1865 1sting(斐波拉契+大数~)
Problem Description You will be given a string which only contains '1'; You can merge two adjacent ' ...