[BOJ / C++] 18870 좌표 압축

Seulguo·2022년 7월 18일
0

Algorithm

목록 보기
86/185
post-thumbnail
post-custom-banner

🐣 문제

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


🐥 코드

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int n, x, cnt = 0;
vector<pair<int, int>> v, ans;

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++){
		cin >> x;
		v.push_back({x, i}); 
	}

	sort(v.begin(), v.end());

	ans.push_back({v[0].second, 0});

	for (int i = 1; i < n; i++){
		if (v[i-1].first == v[i].first){
			ans.push_back({ v[i].second, cnt });
		}
		else {
			ans.push_back({ v[i].second, ++cnt }); 
		}
	}

	sort(ans.begin(), ans.end());

	for (int i = 0; i < n; i++)
		cout << ans[i].second << " ";
}
post-custom-banner

0개의 댓글