제출 #970681

#제출 시각아이디문제언어결과실행 시간메모리
970681kilkuwuPalembang Bridges (APIO15_bridge)C++17
100 / 100
253 ms13500 KiB
#include <bits/stdc++.h>

#define nl '\n'

#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

struct MedianSolver {
  std::multiset<int> pql, pqr;
  int64_t suml = 0, sumr = 0;

  void balance() {
    while (pql.size() > pqr.size()) {
      int to_move = *pql.rbegin();
      pqr.insert(to_move);
      pql.erase(--pql.end());
      suml -= to_move;
      sumr += to_move;
    }
    while (pqr.size() > pql.size() + 1) {
      int to_move = *pqr.begin();
      pql.insert(to_move);
      pqr.erase(pqr.begin());
      suml += to_move;
      sumr -= to_move;
    }
  }

  void add_value(int x) {
    if (pqr.empty() || x >= *pqr.begin()) {
      sumr += x;
      pqr.insert(x);
    } else {
      suml += x;
      pql.insert(x);
    }
    balance();
  }

  void rem_value(int x) {
    if (x >= *pqr.begin()) {
      sumr -= x;
      pqr.erase(pqr.find(x));
    } else {
      suml -= x;
      pql.erase(pql.find(x));
    }
    balance();
  }

  int get_median() {
    return *pqr.begin();
  }

  int64_t get_solution() {
    if (pqr.empty()) return 0;
    int64_t med = get_median();
    return med * pql.size() - suml + sumr - med * pqr.size();
  }
};

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int theta, n;
  std::cin >> theta >> n;

  if (theta == 1) {
    std::vector<int> x_vals;
    int64_t ans = 0;
    for (int i = 0; i < n; i++) {
      char p, q;
      int s, t;
      std::cin >> p >> s >> q >> t;
      if (p == q) {
        ans += std::abs(t - s);
      } else {
        x_vals.push_back(s);
        x_vals.push_back(t);
        ans++;
      }
    }
    std::sort(x_vals.begin(), x_vals.end());

    int med = x_vals.size() / 2;

    for (int i = 0; i < (int)x_vals.size(); i++) {
      ans += std::abs(x_vals[i] - x_vals[med]);
    }

    std::cout << ans << nl;
    return 0;
  }

  std::vector<int> xs;
  std::vector<std::pair<int, int>> peoples;

  int64_t obvious = 0;

  for (int i = 0; i < n; i++) {
    char p, q;
    int s, t;
    std::cin >> p >> s >> q >> t;
    if (p == q) {
      obvious += std::abs(t - s);
    } else {
      if (s > t) std::swap(s, t);
      xs.push_back(s);
      xs.push_back(t);
      peoples.emplace_back(s, t);
    }
  }

  n = peoples.size();

  if (n == 0) {
    std::cout << obvious << nl;
    return 0;
  }

  if (n == 1) {
    std::cout << peoples.front().second - peoples.front().first + 1 + obvious << nl;
    return 0;
  }

  MedianSolver solverl, solverr;
  for (int i = 0; i < n; i++) {
    solverr.add_value(peoples[i].first);
    solverr.add_value(peoples[i].second);
  }

  std::sort(peoples.begin(), peoples.end(), [&](auto& a, auto& b) {
    return a.first + a.second < b.first + b.second;
  });

  int64_t ans = solverl.get_solution() + solverr.get_solution();
  for (int i = 0; i < n; i++) {
    solverr.rem_value(peoples[i].first);
    solverr.rem_value(peoples[i].second);
    solverl.add_value(peoples[i].first);
    solverl.add_value(peoples[i].second);

    ans = std::min(ans, solverl.get_solution() + solverr.get_solution());
  }

  std::cout << ans + n + obvious << nl;

}

/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...