离散化基础
#include <iostream>#include <algorithm>
using namespace std;
const int MAX = 10001;
struct node
{
int date;//数值
int rank;//排名
int index; //下标
};
node a;
bool comp1(node x, node y)
{
return x.date < y.date; //自定义比较函数
}
bool comp2(node x, node y)
{
return x.index < y.index; //自定义比较函数
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a.date;
a.index = i;
}
sort(a + 1, a + 1 + n, comp1); //根据数值排序,求rank
for (int i = 1; i <= n; i++)
{
a.rank = i;
}
sort(a + 1, a + 1 + n, comp2); //根据下标排序,回到原顺序
for (int i = 1; i <= n; i++)
{
cout << a.rank << " ";
}
return 0;
}
页:
[1]