标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 175 Accepted Submission(s): 74
思路:容易想到k = (a[i] - a[i - 1] + 1)就是原序列第i个数的左边比其大的个数,可以从右往左依次计算
用线段树实现:找出序列中第k个正数对应的下标位置,没找到一个,就相当于把该位置的数变为负数O(log)
#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 = 50005; int num[N << 2], a[N], ans[N]; void up(int rt) { num[rt] = num[rt << 1] + num[rt << 1|1]; } void build(int l, int r, int rt) { if(l == r) { num[rt] = 1; return ; } int m = (l + r) >> 1; 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) >> 1; 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) >> 1; if(num[rt << 1] < x) get(rson, x - num[rt << 1]); else get(lson, x); } int main() { int _; scanf("%d", &_); while(_ --) { int n; scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); build(1, n, 1); int c = n; for(int i = n; i >= 2; --i) { int d = c - (a[i] - a[i - 1]); get(1, n, 1, d); update(1, n, 1, pos); // cout << pos << endl; ans[i] = pos; c--; } get(1, n, 1, 1); ans[1] = pos; for(int i = 1; i < n; ++i) printf("%d ", ans[i]); printf("%d\n", ans[n]); } return 0; }
Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/5022615.html