# 1096 - 距离之和最小

#include <bits/stdc++.h>

using namespace std;

using LL = long long;

int main()
{
for (int n; EOF != scanf("%d", &n); ) {
vector<LL> points(n);
for (int i = 0; i < n; scanf("%lld", &points[i++])) {}
sort(points.begin(), points.end());
LL mid = points[n / 2];
printf("%lld\n", accumulate(points.begin(), points.end(), 0LL, [&mid] (LL ans, LL ele) {
return ans + abs(mid - ele);
}));
}
return 0;
}


# 1108 - 距离之和最小V2

#include <bits/stdc++.h>

using namespace std;

using LL = long long;

LL solve(vector<LL> &points)
{
sort(points.begin(), points.end());
LL mid = points[points.size() / 2];
return accumulate(points.begin(), points.end(), 0LL, [&mid] (LL ans, LL ele) {
return ans + abs(mid - ele);
});
}

int main()
{
for (int n; EOF != scanf("%d", &n); ) {
vector<vector<LL> > arr(3, vector<LL>(n));
for (int i = 0; i < n; scanf("%lld%lld%lld",
&arr[0][i], &arr[1][i], &arr[2][i]), ++i) {}
LL ans = 0;
for (auto &v : arr)
ans += solve(v);
printf("%lld\n", ans);
}
return 0;
}


# 1110 - 距离之和最小V3

#include <bits/stdc++.h>

using namespace std;

using LL = long long;

int main()
{
for (int n; EOF != scanf("%d", &n); ) {
vector<pair<LL, LL> > arr(n);
for (int i = 0; i < n; scanf("%lld%lld", &arr[i].first, &arr[i].second), ++i) {}
sort(arr.begin(), arr.end());
LL sum = accumulate(arr.begin(), arr.end(), 0LL, [] (LL sum, pair<LL, LL> &ele) {
return sum + ele.second;
});
auto it = find_if(arr.begin(), arr.end(), [sum] (pair<LL, LL> &ele) {
static LL index = 0;
return (index += ele.second) > sum / 2;
});
printf("%lld\n", accumulate(arr.begin(), arr.end(), 0LL, [&it] (LL sum, pair<LL, LL> &ele) {
return sum + abs(ele.first - it->first) * ele.second;
}));
}
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120