fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #pragma GCC optimize("O2")
  4. #pragma GCC optimize("O3")
  5. #pragma GCC optimize("Ofast")
  6.  
  7. #define pii pair<int,int>
  8. #define ii int,int
  9. #define fi first
  10. #define se second
  11. #define pb push_back
  12. #define lb lower_bound
  13. #define ub upper_bound
  14. #define umap unordered_map
  15. #define BIT(mask, i) ((mask >> i)&1)
  16. #define ALL(c) c.begin(), c.end()
  17.  
  18. using namespace std;
  19.  
  20. mt19937 rd(chrono::high_resolution_clock::now().time_since_epoch().count());
  21. int rand(int l, int r){
  22. return l + rd()*rd()%(r-l+1);
  23. }
  24.  
  25. const int N = 1e6+7;
  26. const int M = 1e3+3;
  27. const int MOD = 1e9+7;
  28. const int inf = 3e18;
  29.  
  30. long long n, a[100005];
  31.  
  32. signed main(){
  33. ios_base::sync_with_stdio(0);
  34. cin.tie(0);
  35. cout.tie(0);
  36. cin >> n;
  37. map<long long, int> mp;
  38. // coi "key" chính là phần tử của mảng và "value" là thứ hạng của phần tử
  39. // mình cho tất cả các phần tử vào map -> Đã được sắp xếp và loại bỏ những cái key trùng lặp
  40. for(int i = 1; i <= n; i++){
  41. cin >> a[i];
  42. mp[a[i]] = 1;
  43. }
  44. // mp đang chứa mảng a được sắp xếp tăng dần
  45. int thuhang = 1;
  46.  
  47. for(map<long long, int>::iterator it = mp.begin(); it != mp.end(); it++){
  48. // mp.begin() -> bắt đầu của map
  49. // mp.end() -> kết thúc của map
  50. // sử dụng pointer để duyệt
  51. // Không dùng <= mà dùng !=
  52. it -> second = thuhang;
  53. thuhang++;
  54. }
  55. // -> mp[a[i]] chính là thứ hạng của a[i];
  56. for(int i = 1; i <= n; i++){
  57. cout << mp[a[i]] << ' ';
  58. }
  59. }
  60. //1 3 6 8 9 | 3 8 6 9 1
  61. //1 2 3 4 5 | 2 4 3 5 1
  62.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty