问题描述:
1010. Lehmer Code (35)
According to Wikipedia: "In mathematics and in particular in combinatorics, the Lehmer code is a particular way to encode each possible permutation of a sequence of n numbers." To be more specific, for a given permutation of items {A1, A2, ..., An}, Lehmer code is a sequence of numbers {L1, L2, ..., Ln} such that Li is the total number of items from Ai to An which are less than Ai. For example, given {24, 35, 12, 1, 56, 23}, the second Lehmer code L2 is 3 since from 35 to 23 there are three items, {12, 1, 23}, less than the second item, 35.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then N distinct numbers are given in the next line.
Output Specification:
For each test case, output in a line the corresponding Lehmer code. The numbers must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.
Sample Input:6 24 35 12 1 56 23Sample Output:
3 3 1 0 1 0
又是一道来给我pb_ds tree送经验的题。。。。
不多说,直接上AC代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; int main() { // freopen("data.txt","r",stdin); int n,x,c1,c2; tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> tr; scanf("%d",&n); vector<int> v(n); for(int i=0;i<n;i++) scanf("%d",&v[i]); for(int i=n-1;i>-1;i--) { tr.insert(v[i]); v[i]=tr.order_of_key(v[i]); } printf("%d",v[0]); for(int i=1;i<n;++i) printf(" %d",v[i]); return 0; } |