ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมที่จะนับองค์ประกอบเล็กๆ ทางด้านขวาโดยใช้ set ใน C++ STL
สำหรับสิ่งนี้เราจะได้รับอาร์เรย์ งานของเราคือสร้างอาร์เรย์ใหม่และเพิ่มจำนวนองค์ประกอบที่มีขนาดเล็กลงทางด้านขวาขององค์ประกอบปัจจุบันที่ตำแหน่ง
ตัวอย่าง
#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