Submission #807585

#TimeUsernameProblemLanguageResultExecution timeMemory
807585peijarPalembang Bridges (APIO15_bridge)C++17
22 / 100
47 ms9596 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
  bool first = true;
  string res = "[";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "]";
  return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << to_string(H);
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int K, N;
  cin >> K >> N;

  if (K == 1) {
    vector<pair<int, int>> intervals;
    int sol = 0;
    for (int i = 0; i < N; ++i) {
      char c, d;
      int a, b;
      cin >> c >> a >> d >> b;
      if (a > b)
        swap(a, b);
      sol += b - a;
      if (c == d)
        continue;
      intervals.emplace_back(a, b);
    }
    vector<int> as, bs, vals;
    for (auto [a, b] : intervals) {
      as.push_back(a);
      bs.push_back(b);
      vals.push_back(a);
      vals.push_back(b);
    }
    sort(as.begin(), as.end());
    sort(bs.begin(), bs.end());
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
    int best = 1e18;
    int cntBefore = 0, sumBefore = 0;
    int posB = 0;
    int cntAfter = intervals.size();
    int sumAfter = accumulate(as.begin(), as.end(), 0LL);
    int posA = 0;
    int nbInt = intervals.size();
    dbg(bs);
    for (int x : vals) {
      while (posB < nbInt and bs[posB] < x) {
        sumBefore += bs[posB++];
        cntBefore++;
      }
      while (posA < nbInt and as[posA] < x) {
        sumAfter -= as[posA++];
        cntAfter--;
      }
      int cur = sumAfter - x * cntAfter;
      cur += x * cntBefore - sumBefore;
      dbg(x, cntBefore, cntAfter, cur);
      best = min(best, cur);
    }
    if (nbInt == 0)
      best = 0;
    dbg(best, sol);
    cout << 2 * best + sol + nbInt << endl;
  } else {
  }
}
#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...