some dp problems
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
// Stick Lengths
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
int main() {
|
||||
int n; cin >> n;
|
||||
vector<ll> in(n);
|
||||
for (auto &e: in) cin >> e;
|
||||
std::sort(in.begin(), in.end());
|
||||
ll target = in[n/2];
|
||||
ll target2 = in[n/2 + 1];
|
||||
ll count = 0, count2 = 0;
|
||||
for (auto e: in) {
|
||||
count += abs(target - e);
|
||||
count2 += abs(target2 - e);
|
||||
}
|
||||
cout << min(count, count2) << endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user