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}$$$.
Time Complexity: $$$\mathcal{O}(n * log(n))$$$
Some other solutions exist as well. As the constraints are low, you can try a brute force solution with time complexity: $$$\mathcal{O}(n * \text{max}(A))$$$. Also, as seen in the behaviour of the function $$$f(x) = \Sigma | x - a_i |$$$, this function is initially decreasing for $$$x \lt \text{median}_1$$$, then it stays at a constant minimum value for x in range $$$[\text{median}_1$$$, $$$\text{median}_2]$$$, and then it is increasing for $$$x \gt \text{median}_2$$$. So ternary search could be applied to find the minima of this function, with time complexity: $$$\mathcal{O}(n * log(n))$$$.
#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();
}
}
Author: neal27
Aim is to maximise $$$\Sigma (p_i * (d_i - f_i))$$$. Firstly, break this sum into two separate sums: $$$\Sigma (p_i * d_i)$$$ and $$$\Sigma (p_i * f_i)$$$. The first sum is constant, so now focus on minimising the second sum that is being subtracted, in order to maximise the overall sum. Can you find a greedy strategy to find an optimal ordering of tasks?
Intuitively, higher priority shorter tasks should be performed first. Just consider two tasks $$$t_1$$$ and $$$t_2$$$. If doing $$$t_1$$$ before $$$t_2$$$ is preferable than doing $$$t_2$$$ before $$$t_1$$$, what inequality can you derive in terms of their priority and burst values?
Processing tasks in decreasing order of $$$p_i/b_i$$$ is optimal.
For proof, let there be an arrangement of tasks, in which we get an optimal answer, but some tasks are not arranged in decreasing order of $$$p_i/b_i$$$, then there will exist two consecutive tasks $$$x$$$, and $$$y$$$, with $$$p_x$$$, $$$b_x$$$, and $$$p_y$$$, $$$b_y$$$, such that $$$p_x/b_x$$$ < p_y/b_y$. Let’s only focus on the sum to be subtracted, say this is $$$A$$$. Let time taken to process tasks before $$$x$$$ be $$$T$$$.
In the claimed optimal arrangement, $$$x$$$ is done before $$$y$$$. So, $$$x$$$’s and $$$y$$$’s contribution to $$$A$$$ is, $$$p_x * (T + b_x) + p_y * (T + b_x + b_y) = \delta_1$$$.
Now let’s swap these tasks $$$x$$$ and $$$y$$$, i.e. $$$x$$$ is done after $$$y$$$. Then $$$x$$$’s and $$$y$$$’s contribution to $$$A$$$ is, $$$p_y * (T + b_y) + p_x * (T + b_y + b_x) = \delta_2$$$.
Now $$$\delta_2 - \delta_1 = (p_x * b_y) – (p_y * b_x) \lt 0$$$, by our assumption $$$p_x/b_x \lt p_y/b_y$$$, hence swapping the tasks $$$x$$$ and $$$y$$$, leads to a smaller value of $$$A$$$, which is $$$A + \delta_2 - \delta_1$$$, and therefore the sum subtracted from total reward value is minimized. Thus, if any tasks are not arranged by decreasing order of $$$p_i/b_i$$$, then we can swap to get a better answer. Hence proved.
Time Complexity: $$$\mathcal{O}(n * log(n))$$$
#include <bits/stdc++.h>
using namespace std;
# define ll long long
# define endl "\n"
struct task{
ll priority;
ll deadline;
ll burst;
};
void solve() {
int n; cin>>n;
vector<task> a(n);
for(int i = 0; i < n; i++) cin>>a[i].priority;
for(int i = 0; i < n; i++) cin>>a[i].deadline;
for(int i = 0; i < n; i++) cin>>a[i].burst;
sort(a.begin(), a.end(), [](task &x, task &y){
return (x.priority * y.burst) > (y.priority * x.burst);
});
ll finish = 0, ans = 0;
for(int i = 0; i < n; i++){
finish += a[i].burst;
ans += a[i].priority * (a[i].deadline - finish);
}
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();
}
}



