在本教程中,我们将讨论一个程序,该程序使用C ++ STL中的set在右侧计算较小的元素。
为此,我们将提供一个数组。我们的任务是构造一个新数组,并在当前元素右侧的位置添加较小元素的数量。
#include <bits/stdc++.h>
using namespace std;
void count_Rsmall(int A[], int len){
set<int> s;
int countSmaller[len];
for (int i = len - 1; i >= 0; i--) {
s.insert(A[i]);
auto it = s.lower_bound(A[i]);
countSmaller[i] = distance(s.begin(), it);
}
for (int i = 0; i < len; i++)
cout << countSmaller[i] << " ";
}
int main(){
int A[] = {12, 1, 2, 3, 0, 11, 4};
int len = sizeof(A) / sizeof(int);
count_Rsmall(A, len);
return 0;
}输出结果
6 1 1 1 0 1 0