백준 알고리즘 1365번 : 꼬인 전깃줄

Zoo Da·2021년 12월 4일
0

백준 알고리즘

목록 보기
280/337
post-thumbnail

링크

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

sol1) LIS + lower_bound

#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define int int64_t
using namespace std;

using pii = pair<int, int>;

int32_t main() {
  fastio;
  int n; cin >> n;
  vector<pii> v(n);
  vector<int> LIS;
  for(int i = 0; i < n; i++){
    v[i].first = i;
    cin >> v[i].second;
  }
  sort(v.begin(), v.end());
  for(int i = 0; i < n; i++){
    int t = v[i].second;
    if(LIS.empty() || LIS.back() < t) LIS.push_back(t);
    else *lower_bound(LIS.begin(), LIS.end(), t) = t;
  }
  cout << n - LIS.size() << "\n";
}
profile
메모장 겸 블로그

0개의 댓글