ABC036-C: 座圧
問題
解説
与えられた数列
計算量は与えられた数列の長さを
実装例
C++
cpp
#include <algorithm>
#include <iostream>
#include <vector>
template <class T> std::vector<int> shrink_coordinate(std::vector<T> &a) {
std::vector<T> b = a;
std::sort(b.begin(), b.end());
b.erase(std::unique(b.begin(), b.end()), b.end());
int N = a.size();
std::vector<int> res(N);
for (int i = 0; i < N; i++) {
res[i] = std::lower_bound(b.begin(), b.end(), a[i]) - b.begin();
}
return res;
}
int main() {
int N;
std::cin >> N;
std::vector<int> a(N);
for (int i = 0; i < N; i++) {
std::cin >> a[i];
}
std::vector<int> b = shrink_coordinate(a);
for (int i = 0; i < N; i++) {
std::cout << b[i] << "\n";
}
return 0;
}
Python
python
#!/usr/bin/env python3
def shrink_coordinate(a):
b = sorted(list(set(a)))
table = {v: k for k, v in enumerate(b)}
return list(map(lambda x: table[x], a))
N = int(input())
a = [int(input()) for _ in range(N)]
b = shrink_coordinate(a)
print("\n".join(map(str, b)))