728x90
18870 좌표 압축
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
vector<long long> x1;
vector<long long> x2;
int n;
cin >> n ;
for (int i = 0; i < n; ++i)
{
long long tmp;
cin >> tmp;
x1.push_back(tmp);
x2.push_back(tmp);
}
sort(x1.begin(), x1.end());
x1.erase(unique(x1.begin(), x1.end()), x1.end());
for (int i = 0; i < n; ++i)
{
cout << lower_bound(x1.begin(), x1.end(), x2[i]) - x1.begin() << " ";
}
return 0;
}
- 시간초과로 인해서 머리를 싸맸던 문제
- lower_bound라고하는 함수를 배워서 사용했다.
- sort, unique, erase, lower_bound를 사용하면 조금 더 쉽게 풀 수 있을 것 같다.
728x90
'Study > Baekjoon' 카테고리의 다른 글
[백준/C++] 9012 괄호 (0) | 2024.02.23 |
---|---|
[백준/C++] 10773 제로 (0) | 2024.02.23 |
[백준/C++] 25305 커트라인 (0) | 2024.02.22 |
[백준/C++] 2587 대표값2 (0) | 2024.02.22 |
[백준/C++] 10814 나이순 정렬 (0) | 2024.02.21 |