AT1865 座圧
题意
给定序列 ,对其离散化,输出离散化后的结果。
解法
先用 vector 储存并排序去重,然后 lower_bound 即可,注意从 开始,要换行。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N], n;
vector<int> b;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
b.push_back(a[i]);
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
for (int i = 1; i <= n; i++)
{
a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
printf("%d\n", a[i]);
}
return 0;
}

浙公网安备 33010602011771号