Stick Lengths
En este problema nos dan un arreglo con elementos. Queremos hallar el costo mínimo para hacer todos los elementos iguales.
Solución
Otra solución y algunas demostraciones
Implementación
Aquí está la implementación del enfoque más sencillo.
#include <bits/stdc++.h>
using namespace std;
// variables used for the current problem
int n, median;
vector<int> p;
long long ans, cnt;
void solve() {
cin >> n;
p.resize(n);
for (int &x : p) { cin >> x; }
sort(p.begin(), p.end());
median = p[n / 2];
for (const int &x : p) {
ans += abs(median - x); // Calculate the cost to modify the stick
// length
}
cout << ans << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}import java.io.*;
import java.util.*;
public class StickLengths {
// CodeSnip{Kattio}
public static void main(String[] args) {
Kattio io = new Kattio();
int N = io.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++) { arr[i] = io.nextInt(); }
Arrays.sort(arr);
/*
* Take the median stick length,
* and calculate the cost to change each stick length to the median.
*/
int median = arr[N / 2];
long costs = 0;
for (int i = 0; i < N; i++) { costs += Math.abs(arr[i] - median); }
io.println(costs);
io.close();
}
}n = int(input())
sticks = sorted(list(map(int, input().split())))
median = sticks[n // 2]
ans = 0
for x in sticks:
ans += abs(median - x)
print(ans)