离散化模板
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 1e3 + 10; 4 int n, d[N]; //n个数 离散化后数组 5 struct node { 6 int x, id; 7 bool operator<(const node& t)const { 8 //按照值小的排前面 值相同先出现的排前面 可调整 9 return x == t.x ? id < t.id : x < t.x; 10 } 11 } a[N]; 12 signed main() { 13 cin >> n; 14 for(int i = 1; i <= n; i++) { 15 cin >> a[i].x; 16 a[i].id = i; 17 } 18 19 sort(a + 1, a + 1 + n); 20 21 for(int i = 1; i <= n; i++) { 22 d[a[i].id] = i; 23 } 24 25 for(int i = 1; i <= n; i++) cout << d[i] << " "; 26 27 return 0; 28 } 29 /* 30 input1: 31 5 32 2 1 99 100 4 33 output1: 34 2 1 4 5 3 35 36 input2: 37 5 38 1 1 100 99 1 39 1 2 5 4 3 40 */