2 条题解
-
1
#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main() { int n; cin >> n; vector<int> x_coords, y_coords; vector<pair<int, int>> points; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; x_coords.push_back(x); y_coords.push_back(y); points.push_back({x, y}); } sort(x_coords.begin(), x_coords.end()); sort(y_coords.begin(), y_coords.end()); int x_median = x_coords[n / 2]; // x中位数 int y_median = y_coords[n / 2]; // y中位数 long long total_dist = 0; for (const auto& p : points) { total_dist += abs(p.first - x_median) + abs(p.second - y_median); } cout<< total_dist << endl; return 0; }
-
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> x(n), y(n); for (int i = 0; i < n; ++i) { cin >> x[i] >> y[i]; } sort(x.begin(), x.end()); sort(y.begin(), y.end()); int median_x = x[n / 2]; int median_y = y[n / 2]; long long total_distance = 0; for (int i = 0; i < n; ++i) { total_distance += abs(x[i] - median_x) + abs(y[i] - median_y); } cout << total_distance << endl; return 0; }
- 1
信息
- ID
- 5
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 7
- 标签
- 递交数
- 16
- 已通过
- 10
- 上传者