Submission #970681

# Submission time Handle Problem Language Result Execution time Memory
970681 2024-04-27T04:51:21 Z kilkuwu Palembang Bridges (APIO15_bridge) C++17
100 / 100
253 ms 13500 KB
#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 time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 484 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 488 KB Output is correct
12 Correct 15 ms 2268 KB Output is correct
13 Correct 32 ms 2648 KB Output is correct
14 Correct 23 ms 2264 KB Output is correct
15 Correct 18 ms 2012 KB Output is correct
16 Correct 18 ms 2440 KB Output is correct
17 Correct 21 ms 2772 KB Output is correct
18 Correct 27 ms 2512 KB Output is correct
19 Correct 30 ms 3048 KB Output is correct
20 Correct 21 ms 2492 KB Output is correct
21 Correct 29 ms 3020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 516 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 2 ms 356 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 110 ms 12192 KB Output is correct
26 Correct 204 ms 12208 KB Output is correct
27 Correct 253 ms 12956 KB Output is correct
28 Correct 232 ms 13500 KB Output is correct
29 Correct 249 ms 13496 KB Output is correct
30 Correct 97 ms 7108 KB Output is correct
31 Correct 134 ms 12704 KB Output is correct
32 Correct 135 ms 13296 KB Output is correct
33 Correct 123 ms 12900 KB Output is correct
34 Correct 136 ms 13240 KB Output is correct
35 Correct 114 ms 12732 KB Output is correct
36 Correct 131 ms 12992 KB Output is correct
37 Correct 135 ms 12068 KB Output is correct