제출 #970659

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

#define nl '\n'

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();

  std::sort(xs.begin(), xs.end());
  xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

  int sz = xs.size();

  int64_t final_ans = 1e18;
  for (int i = 0; i < sz; i++) {
    for (int j = i + 1; j < sz; j++) {
      int64_t cand = 0;
      for (int k = 0; k < n; k++) {
        int64_t d1 = std::abs(peoples[k].first - xs[i]);
        d1 += std::abs(peoples[k].second - xs[i]);
        int64_t d2 = std::abs(peoples[k].first - xs[j]);
        d2 += std::abs(peoples[k].second - xs[j]);
        cand += std::min(d1, d2);
      }

      final_ans = std::min(final_ans, cand);
    }
  }

  std::cout << final_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...