ALG/ALG Solve

C++) 백준 18870번 - 좌표 압축

jh2ee 2023. 2. 21. 11:12

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

 

18870번: 좌표 압축

수직선 위에 N개의 좌표 X1, X2, ..., XN이 있다. 이 좌표에 좌표 압축을 적용하려고 한다. Xi를 좌표 압축한 결과 X'i의 값은 Xi > Xj를 만족하는 서로 다른 좌표의 개수와 같아야 한다. X1, X2, ..., XN에 좌

www.acmicpc.net

 

#include <bits/stdc++.h>
using namespace std;

int n;
int x[1000002]; //입력받을 배열
vector<int> temp,uni; //temp는 정렬된 배열 저장, uni는 temp에서 중복된 수를 제외한 배열

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x[i];
        temp.push_back(x[i]);
    }
    sort(temp.begin(),temp.end());
    for(int i=0;i<n;i++){
        if(i==0||temp[i]!=temp[i-1]) uni.push_back(temp[i]); //중복 삭제
    }

    for(int i=0;i<n;i++){
        cout<<lower_bound(uni.begin(),uni.end(),x[i])-uni.begin()<<" ";
        //lower_bound의 반환형은 iterator이므로 uni.begin()을 빼줘야 몇번째 index인지 알 수 있음
    }
    return 0;
}