We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: BitWizz
Since we want to minimize the number of win of our team, we would like to finish on $$$4th$$$ position. Now the problem reduces to finishing on the $$$1st$$$ position with remaining $$$N - 3$$$ teams. Since we can decide the net run-rate of teams, we would try to give equal wins to each team. There will be total $$$N - 3$$$ teams and $$${N - 3 \choose 2}$$$ matches, so if $$$N$$$ is even we can equally divide the wins and each team will win $$$(N - 4)/2$$$ matches and we would give our team the highest net run-rate. If $$$N$$$ is odd then we must give the extra win to our team, then the total wins is $$$(N - 3)/2$$$.
Time Complexity: $$$\mathcal{O}(1)$$$
#include <bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define int int64_t
void solve() {
int n; cin >> n;
if (n % 2 == 0) {
int m = (n - 4) / 2;
cout << m << '\n';
}
else {
int m = floor((n - 4) / 2) + 1;
cout << m << '\n';
}
}
signed main() {
fastio();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int tc;
cin >> tc;
while (tc--)
solve();
return 0;
}
Author: neal27
If you choose $$$x$$$ as the final value for elements of the array, then the number of operations is given by $$$\Sigma | x - a_i |$$$. What value of $$$x$$$ will minimise this sum?
If you tried mean as the final value, try this counter case $$$1$$$ $$$2$$$ $$$1000$$$, and see why it failed.
Median minimises the sum of absolute deviations. For proof, let's first consider the case of just two elements, $$$a_1$$$ and $$$a_2$$$. WLOG, assume $$$a_1 \leq a_2$$$. It is optimal to choose $$$x$$$ in range $$$[a_1, a_2]$$$, giving number of operations $$$= a_2 - a_1$$$. Now, consider the given array $$$a_1$$$, $$$a_2$$$, $$$...$$$, $$$a_n$$$ in sorted order. For now, consider the case of even $$$n$$$. Make $$$n / 2$$$ pairs as follows: $$$(a_1, a_{n})$$$, $$$(a_2, a_{n - 1})$$$, $$$(a_3, a_{n - 2})$$$, $$$...$$$, $$$(a_{n/2}, a_{n/2 + 1})$$$. Now, from the result we got from two-element case, $$$x$$$ must be contained in each of these pairs, leading to the result that x must be in range $$$[a_{n/2}, a_{n/2 + 1}]$$$. For odd n, the range is just a single element $$$a_{(n - 1)/2}$$$.
#include <bits/stdc++.h>
using namespace std;
# define vi vector<int>
# define endl "\n"
void solve() {
int n; cin>>n;
vi a(n);
for(int i = 0; i < n; i++) cin>>a[i];
sort(a.begin(), a.end());
int ans = 0;
int l = 0, r = n - 1;
while(l < r){
ans += (a[r--] - a[l++]);
}
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin.exceptions(cin.failbit);
int t = 1;
cin >> t;
while(t--){
solve();
}
}



