Transmission Door
The main idea: there is a 1-n arrangement, the data given from the second to the nth number of each number in front of a few numbers smaller than this number, required to restore the arrangement of this 1-n
Idea: Recently do this kind of problem seems to feel a little, it is natural to think, we can from the last number to reverse, has been launched the first after the end can be finished. In that case, it's easy to write a N2 The algorithm. But as a oier, we ask for progress, so we will think, can optimize it. In fact, we found that there are several operations in the front that are smaller than the target value are redundant, we can consider using line tree to optimize. Then we'll be able to O(N Log N ) To find out the whole sequence in time.
Code:
#include <cstdio>#define MAXN 8005structnode{intL, R, Len;} T[MAXN *4];voidBuild (intIintLintr) {t[i].l = L, T[I].R = R; T[i].len = r-l+1;if(L = = r)return;intMid = (L + r) >>1; Build (i<<1, L, mid); Build (i<<1|1, Mid +1, r);}intQ (intIintnum) {--T[i].len;if(T[I].L = = T[I].R)returnT[I].L;if(Num > t[i<<1].len)returnQ (i<<1|1, num-t[i<<1].len);Else returnQ (i<<1, num);}intN, A[maxn], ANS[MAXN];intMain () {scanf("%d", &n); Build (1,1, n); for(inti =2; I <= N; i + +)scanf("%d", &a[i]); for(inti = n; i >0; I--) ans[i] = Q (1, A[i] +1); for(inti =1; I <= N; i + +)printf("%d\n", Ans[i]);}
Copyright NOTICE: Please feel free to reprint O (∩_∩) o
poj2182 Lost Cows (segment tree)