백준 알고리즘 18870번 : 좌표 압축

Zoo Da·2021년 9월 30일
0

백준 알고리즘

목록 보기
220/337
post-thumbnail

링크

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

sol1) 정렬 후 이분탐색

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define fastio cin.tie(0)->sync_with_stdio(0)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define sz(v) (int)(v).size()
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define debug(x) cout << (#x) << ": " << (x) << '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vs = vector<string>;
using tii = tuple<int,int,int>;
template<typename T> using wector = vector<vector<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;

int main() {
	fastio;
  int n; cin >> n;
  vi x(n),uni(n);
  for(int i = 0; i < n; i++){
    cin >> x[i];
    uni[i] = x[i];
  }
  sort(all(uni));
  uni.erase(unique(all(uni)),uni.end());
  for(int i = 0; i < x.size(); i++){
    cout << lower_bound(all(uni), x[i]) - uni.begin() << ' ';
  } 
	return 0;
}

주어진 배열을 정렬해주고 중복을 제거한 후에 lower_bound를 활용해서 이분탐색을 진행해주면 됩니다.

profile
메모장 겸 블로그

0개의 댓글