离散化模板
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 */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】