Cod sursa(job #2619425)

Utilizator segtreapMihnea Andreescu segtreap Data 27 mai 2020 17:22:50
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.93 kb
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = (int) 1e5 + 7;
int n, a[N], fent[N];
bool act[N];
vector<int> id;

void add(int i, int x) {
  if (x == 1 && act[i]) {
    return;
  }
  if (x == -1 && !act[i]) {
    return;
  }
  act[i] ^= 1;
  while (i <= n) {
    fent[i] += x;
    i += i & (-i);
  }
}

int get(int i) {
  int sol = 0;
  while (i) {
    sol += fent[i];
    i -= i & (-i);
  }
  return sol;
}

struct T {
  int i;
  ll best;
};


void add(int l, int r, int x) {
  if (l > r) {
    swap(l, r);
  }
  for (auto &i : id) {
    if (i > r) {
      break;
    }
    if (l <= i) {
      add(i, x);
    }
  }
}

int compute(int p, int a, int b) {
  int sol = 0;
  sol += abs(get(p) - get(a));
  add(p, a, -1);
  sol += abs(get(a) - get(b));
  sol++;
  add(p, a, +1);
  return sol;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);


  map<int, vector<int>> where;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    add(i, 1);
    where[a[i]].push_back(i);
  }
  vector<vector<int>> kek;
  for (auto &it : where) {
    kek.push_back({});
    for (auto &i : it.second) {
      kek.back().push_back(i);
    }
  }
  pair<T, T> cur;
  cur.first = cur.second = {1, 0};
  for (auto &_ : kek) {
    id = _;
    ll best1 = (ll) 1e18;
    ll best2 = (ll) 1e18;
    ll a = 0, b = 0, c = 0, d = 0;
    a = cur.first.best + compute(cur.first.i, id.back(), id[0]);
    b = cur.second.best + compute(cur.second.i, id.back(), id[0]);
    c = cur.first.best + compute(cur.first.i, id[0], id.back());
    d = cur.second.best + compute(cur.second.i, id[0], id.back());
    add(1, n, -1);
    best1 = min(a, b);
    best2 = min(c, d);
    ///cout << " : " << best1 << " " << best2 << "\n";
    cur.first = {id[0], best1};
    cur.second = {id.back(), best2};
  }
  cout << min(cur.first.best, cur.second.best) + 1 << "\n";
}