在使用离散化方法编程时,通常要知道每个数排序后的编号(rank值),相同的数对应同一个编号。
输入格式:
第1行:一个整数N。 1<=N<=100000。
第2行:有N个整数,每个数都是int范围的。注意:可能有相同整数。
输出格式:
依次输出每个数的排名。
输入样例:
5
8 2 6 9 2
输出样例:
3 1 2 4 1
代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,a[100001],b[100001];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
b[i]=a[i];
}
map<int,int> m;
sort(b,b+n);
int count=1;
m[b[0]]=1;
for(int j=1;j<n;j++){
if(b[j]!=b[j-1]){
count++;
m[b[j]]=count;
}
}
for(int j=0;j<n;j++){
cout<<m[a[j]]<<" ";
}
return 0;
}
使用sort与map
解法参照:https://blog.csdn.net/apple_59169997/article/details/124127202