코딩테스트

[백준] 온라인 저지 18870번 : 좌표 압축(C++), 이분탐색

yeonii_ 2025. 4. 18. 14:00

https://www.acmicpc.net/problem/18870

#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

int main()
{
	int N;
	cin >> N;

	vector<std::pair<int, int>> pos(N);
	unordered_set<int> val;
	int input;
	for (int i = 0; i < N; ++i)
	{
		cin >> input;	
		pos[i] = { input, i };		//val//idx
		val.insert(input);
	}

	vector<int> answer(N, 0);

	vector<int> sorted_vals(val.begin(), val.end());
	sort(sorted_vals.begin(), sorted_vals.end());

	for (auto& p : pos) {
		answer[p.second] = distance(sorted_vals.begin(), lower_bound(
			sorted_vals.begin(), sorted_vals.end(), p.first,
			[](const int& lhs, int val) {
				return lhs < val;
			}
		));
	}

	for (int& i : answer)
		cout << i << " ";

}