离散化模板

复制代码
 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 */
复制代码

 

posted @   std&ice  阅读(82)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示