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,它前面 ...
随机推荐
- JavaEE编码题
1.请编写代码实现登录效果(5分) 要求: 1)手写出相应的HTML和CSS代码 2)字体大小12px,表格宽300px,按钮行占两列并水平居中, 3)可以写在style节点内,也可使用行内CSS或者 ...
- iOS应用架构谈(一):架构设计的方法论
当我们讨论客户端应用架构的时候,我们在讨论什么? 其实市面上大部分应用不外乎就是颠过来倒过去地做以下这些事情: 简单来说就是调API,展示页面,然后跳转到别的地方再调API,再展示页面. 那这有什么好 ...
- Linux vmstat字段解析
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- 常用iOS的第三方框架
图像:1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等 ...
- 三、jQuery--Ajax基础--Ajax全接触--jQuery中的AJAX
用jQuery实现Ajax jQuery.ajax([settings]) type:类型,“POST”或“GET”,默认为“GET” url:发送请求的地址 data:是一个对象,连同请求发送到服务 ...
- php上传文件进度条
ps:本文转自脚本之家 Web应用中常需要提供文件上传的功能.典型的场景包括用户头像上传.相册图片上传等.当需要上传的文件比较大的时候,提供一个显示上传进度的进度条就很有必要了. 在PHP 5.4以前 ...
- 重温WCF之数据契约中使用枚举(转载)(十一)
转载地址:http://www.zhuli8.com/wcf/EnumMember.html 枚举类型的定义总是支持序列化的.当我们定义一个新的枚举时,不必应用DataContract特性,就可以在数 ...
- Linux文件系统(inode、block……)
内容源于<鸟哥的Linux私房菜> 认识 EXT2 文件系统 文件系统的特殊观察与操作 文件系统 superblock,inode,block superblock,inode,block ...
- MVC授权认证
处于安全性考虑,MVC可以完成授权认证,授权认证的方式如下: 1.配置Config文件,设置登录页面: <authentication mode="Forms"> &l ...
- Oracle锁定和解锁用户的命令
转:http://database.51cto.com/art/200910/158576.htm 在DBA的日常工作中,经常遇到为Oracle用户解锁的操作:这篇文章给出在命令行下进行Oracle用 ...