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

Problem Description
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.
 
Input
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
 
Output
For each testcase,print the ans.
 
Sample Input
1
3
0 1 2
 
Sample Output
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&#39;s Premutation(树状数组+二分)的更多相关文章

  1. HDU 5592 ZYB's Premutation(树状数组+二分)

    题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列. 思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字 ...

  2. BestCoder Round #65 (ZYB's Premutation)

    ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  3. ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...

  4. BestCoder Round #65 (ZYB's Game)

    ZYB's Game Accepts: 672 Submissions: 1207 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...

  5. BestCoder Round #65 (ZYB's Biology)

    ZYB's Biology Accepts: 848 Submissions: 1199 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 13 ...

  6. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  7. HDU 5592 ZYB's Game 【树状数组】+【二分】

    <题目链接> 题目大意: 给你一个由1~n,n个数组成的序列,给出他们每个的前缀逆序数,现在要求输出这个序列. 解题分析: 由前缀逆序数很容易能够得到每个数的逆序数.假设当前数是i,它前面 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. caioj 1077 动态规划入门(非常规DP1:筷子)

    首先可以看出排序之后,最优解肯定是每一对都相邻才是最优的 那么我们就要找构成最优解的相邻组 设f[i][j]是前i个字符,k对的最小值 如果当前这个筷子不取的话,f[i][j] = f[i-1][j] ...

  2. 大话设计模式C++实现-第15章-抽象工厂模式

    一.UML图 二.概念 抽象方法模式(Abstract Factory):提供一个创建一系列相关或互相依赖对象的接口,而无需指定他们详细的类. 三.包括的角色 (1)抽象工厂 (2)详细工厂:包含详细 ...

  3. ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

    题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...

  4. codeforces 558E A Simple Task 线段树

    题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...

  5. 2015上海网络赛 HDU 5478 Can you find it 数学

    HDU 5478 Can you find it 题意略. 思路:先求出n = 1 时候满足条件的(a,b), 最多只有20W对,然后对每一对进行循环节判断即可 #include <iostre ...

  6. 洛谷P2415 集合求和

    题目描述 给定一个集合s(集合元素数量<=30),求出此集合所有子集元素之和. 输入输出格式 输入格式: 集合中的元素(元素<=1000) 输出格式: 和 输入输出样例 输入样例#1: 2 ...

  7. oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息

    来源于网上整理 总结了一下oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助: 1.查询出所有的用户表select * fro ...

  8. Ubuntu 16.04 实现有线 无线同时用

    因为工作的原因,经常会用有线网卡连接服务器进行配置,无线网卡上外网. 一.查看当前网关信息 pipci@ubuntu:~$ ip route showdefault via 192.168.2.1 d ...

  9. 服务器无法远程管理客户端”计算机配置"解决方法

    1.确认是否可ping 通 如无法ping通, 在入门规则中找到“文件和打印机共享(回显请求-ICMPv4-In)”,设定为启用(配置文件为:域) 2. 打开139端口 1.2在防火墙中设置如下:

  10. ES6特性-对比两个值是否相等

    因为JavaScript中有语言缺陷,所以出了个Object.is()