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();
}
}
Author: ShivamKapoor23
This problem is equivalent to finding the number of non-decreasing sequences of length $$$n$$$ where each element is between $$$1$$$ and $$$n$$$.
How about opening your modules to revisit "STARS AND BARS"?
The stars and bars theorem is a combinatorial method to determine the number of ways to distribute indistinguishable objects (stars) into distinguishable bins (separated by bars).
For non-decreasing sequences, we can model the problem as follows: We want to count the number of sequences $$$1 \leq x_1 \leq x_2 \leq \cdots \leq x_n \leq n$$$.
All the numbers which are same would come together, and the resultant sequence would be in sorted order. This means that we can find an integral equation:
$$$f_1 + f_2 + f_3 + \cdots + f_n = n$$$ where $$$f_i$$$ ($$$0 \leq f_i \leq n$$$) is the frequency of the number $$$i$$$.
This is exact template of Stars and Bars where we distribute $$$n$$$ identical objects among $$$r$$$ people where each person can get $$$0$$$ or more number objects.
The Formula for that is : {}_n+r-1 \mathrm{ C }_r-1 For our problem: $$$n = n$$$ and $$$r = n$$$, thus our solution reduces to: {}_2*n-1 \mathrm{ C }_n-1
// Author : Shivam Kapoor
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll mod7 = 1e9 + 7, mod9 = 998244353, INF = 1e18;
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) ((ll)(v).size())
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define rrep(i, a, b) for (ll i = (b)-1; i >= (a); --i)
#ifndef ONLINE_JUDGE
#define debug(x) cout << "DEBUG: " << #x << " = " << (x) << endl;
#define print(v) do { \
cout << "vect--" << #v << " = [ "; \
for (auto it = v.begin(); it != v.end(); ++it) cout << *it << " "; \
cout << "]\n"; \
} while(0)
#define print2d(v) do { \
cout << "Matrix:\n"; \
for (auto it = v.begin(); it != v.end(); ++it) { \
for (auto jt = it->begin(); jt != it->end(); ++jt) cout << *jt << " "; \
cout << "\n"; \
} \
} while(0)
#define printmap(m) do { \
cout << "Map:\n"; \
for (auto it = m.begin(); it != m.end(); ++it) { \
cout << it->first << " -> " << it->second << "\n"; \
} \
} while(0)
#define printpp(v) do { \
cout << "[ "; \
for (auto it = v.begin(); it != v.end(); ++it) cout << "(" << it->first << ", " << it->second << ") "; \
cout << "]\n"; \
} while(0)
#else
#define debug(x)
#define print(v)
#define print2d(v)
#define printmap(m)
#define printpp(v)
#endif
void myerase(ordered_set &t, int v) {
auto it = t.find_by_order(t.order_of_key(v));
if (it != t.end() && *it == v) t.erase(it);
}
ll modAdd(ll a, ll b, ll m = mod7) { return (a + b) % m; }
ll modSub(ll a, ll b, ll m = mod7) { return (a - b + m) % m; }
ll modMul(ll a, ll b, ll m = mod7) { return (a * b) % m; }
ll modExp(ll base, ll exp, ll m = mod7) {
ll res = 1;
while (exp) {
if (exp % 2) res = modMul(res, base, m);
base = modMul(base, base, m);
exp /= 2;
}
return res;
}
ll modInv(ll a, ll m = mod7) { return modExp(a, m - 2, m); }
ll nCr(ll n, ll r, ll m = mod7) {
if (r > n - r) r = n - r;
ll numerator = 1, denominator = 1;
for (ll i = 0; i < r; ++i) {
numerator = modMul(numerator, n - i, m);
denominator = modMul(denominator, i + 1, m);
}
return modMul(numerator, modInv(denominator, m), m);
}
void solve() {
ll n; cin >> n;
cout << nCr(2*n - 1, n - 1) << endl;
}
int main() {
fast_io
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
ll t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
Author: Utkarsh59
It is always possible to make all elements equal.
What are the possible values p can take after an operation?
Using the given operation, $$$p$$$ can either remain $$$p$$$ or be changed to any number in the range $$$[0, p/2)$$$. Every number $$$z$$$ in this range can be obtained by selecting $$$y = p - z$$$.
Case 1: $$$k = 0$$$
For the entire array, we compute $$$min_1$$$ (the smallest element) and $$$min_2$$$ (the second smallest element).
If $$$min_1 \gt (min_2 - 1)/2$$$, then every element can be set to $$$min_1$$$, making it the answer. Otherwise, the answer is $$$(min_1 - 1)/2$$$.
Case 2: $$$1 \leq k \leq n - 2$$$
As we iterate through the array, we maintain a set to efficiently track elements seen so far. At each step, we define $$$min_2 = a[k + 1]$$$, which represents the second smallest element. For each $$$k$$$, we determine the possible minimum value($$$min_1$$$) as follows:
Option 1 ($$$r_1$$$):Take $$$a[k]$$$ as the minimum element.
Option 2 ($$$r_2$$$): The largest element from previously seen values that is less than or equal to $$$(min_2 - 1)/2$$$ is taken as the minimum element.
The final answer for each $$$k$$$ is the maximum of these two options. The current element $$$a[k]$$$ is then inserted into the set for future queries.
Case 3: $$$k = n - 1$$$
In this case, the result is simply $$$a[n - 1]$$$.
Time Complexity: $$$\mathcal{O}(n * log(n))$$$
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
if (n == 1) {
cout << v[0] << endl;
return;
}
if (n == 2) {
if (v[0] * 2 < v[1]) {
cout << v[0] << " ";
} else {
cout << (v[0] - 1) / 2 << " ";
}
cout << v[1] << endl;
return;
}
vector<int> res(n);
res[n - 1] = v[n - 1];
if (v[0] * 2 < v[1]) {
res[0] = v[0];
} else {
res[0] = (v[0] - 1) / 2;
}
set<int> s;
s.insert(v[0]);
for (int k = 1; k <= n - 2; k++) {
int min2 = v[k + 1];
int min11 = v[k];
int r1 = 0;
if (min11 * 2 < min2) {
r1 = min11;
} else {
r1 = (min11 - 1) / 2;
}
int r2 = 0;
auto it = s.upper_bound((min2 - 1) / 2);
if (it != s.begin()) {
it--;
r2 = *it;
}
res[k] = max(r1, r2);
s.insert(v[k]);
}
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
cout << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
Author: MadhavCoding
The answer will never be -1.
First try to solve for two Arithmetic Progressions.
Suppose we have two Arithmetic Progressions with the same starting value $$$a$$$ and common differences $$$d_1$$$ and $$$d_2$$$.
$$$AP_1 :$$$ $$$a, a+d_1, a+2d_1,....$$$
$$$AP_2 :$$$ $$$a, a+d_2, a+2d_2,....$$$
The first common element is $$$a$$$. Let the next common element be $$$a + k$$$.
Hence, the common difference of the common elements of both the Arithmetic Progressions will be $$$k$$$. We now calculate $$$k$$$.
Assume that the next common element appears at the $$$n_1 ^ {th}$$$ term in $$$AP_1$$$ and at the $$$n_2 ^ {th}$$$ term in $$$AP_2$$$ (with the first term corresponding to $$$n = 0$$$).
Then we have:
$$$\implies a + k = a + n_1d_1 = a + n_2d_2$$$
$$$\implies k = n_1d_1 = n_2d_2$$$
$$$\implies d_1 \mid k$$$ and $$$d_2 \mid k$$$
Thus, both $$$d_1$$$ and $$$d_2$$$ divides $$$k$$$, which means that $$$k$$$ must be a common multiple of $$$d_1$$$ and $$$d_2$$$. Since we are interested in the smallest such positive $$$k$$$, it follows that:
$$$\implies k = \operatorname{lcm}(d_1, d_2)$$$
Therefore, the common difference of the common elements of both the Arithmetic Progressions is $$$\operatorname{lcm}(d_1, d_2)$$$.
Extending this observation to multiple arithmetic progressions, for each Result Query $$$[l, r]$$$, we need to return:
$$$\operatorname{lcm}(d_l, d_{l+1}, d_{l+2},....,d_r)$$$.
To efficiently handle both Update and Range queries, we can utilize a data structure like Segment Tree.
/*
Author : MadhavCoding
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define pi pair<int, int>
#define pll pair<ll, ll>
#define endl "\n"
const ll MOD = 1e9 + 7;
const ll N = 1e3;
using namespace std;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<ll, ll, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
vector <bool> isPrime(N+1, true);
vector <long long> prime;
void sieve()
{
for (ll i = 2; i*i <= N; i++)
{
if (isPrime[i])
{
for (int j = i*i; j <= N; j=j+i)
{
isPrime[j] = false;
}
}
}
for(ll i = 2; i <= N; i++)
{
if(isPrime[i]) prime.push_back(i);
}
}
ll modmul(ll a, ll b)
{
return ((a % MOD) * (b % MOD)) % MOD;
}
ll modpower(ll a, ll b, ll m)
{
if(b==0) return 1;
else if (b%2==0)
{
int t = modpower(a, b/2, m);
return (1ll * t * t%m);
}
else
{
int t = modpower(a, b/2, m);
t = 1ll*t*t%m;
return (1ll * a * t%m);
}
}
class SegTree
{
ll size;
vector<vector<ll>> sums;
ll n1 = prime.size();
vector<ll> NEUTRAL;
public:
void init(ll n)
{
size = 1;
while(size < n) size *= 2;
NEUTRAL.resize(n1, 0);
sums.resize(2 * size - 1, NEUTRAL);
}
void build(vector<ll>& a, ll x, ll lx, ll rx)
{
if(rx - lx == 1)
{
if(lx < (ll)a.size())
{
ll temp = a[lx];
for(int i = 0; i < n1; i++)
{
sums[x][i] = 0;
while(temp % prime[i] == 0)
{
sums[x][i]++;
temp /= prime[i];
}
}
}
return;
}
ll m = lx + (rx - lx) / 2;
build(a, 2 * x + 1, lx, m);
build(a, 2 * x + 2, m, rx);
for(int i = 0; i < n1; i++)
{
sums[x][i] = max(sums[2 * x + 1][i], sums[2 * x + 2][i]);
}
}
void build(vector<ll>& a)
{
build(a, 0, 0, size);
}
void set_x(ll i, ll v, ll x, ll lx, ll rx)
{
if(rx - lx == 1)
{
ll temp = v;
for(int i = 0; i < n1; i++)
{
sums[x][i] = 0;
while(temp % prime[i] == 0)
{
sums[x][i]++;
temp /= prime[i];
}
}
return;
}
ll m = lx + (rx - lx) / 2;
if(i < m) set_x(i, v, 2 * x + 1, lx, m);
else set_x(i, v, 2 * x + 2, m, rx);
for(int i = 0; i < n1; i++)
{
sums[x][i] = max(sums[2 * x + 1][i], sums[2 * x + 2][i]);
}
}
void set_x(ll i, ll v)
{
set_x(i, v, 0, 0, size);
}
ll find_sum(ll l, ll r, ll x, ll lx, ll rx, ll ind)
{
if(r <= lx || rx <= l) return 0;
else if(l <= lx && r >= rx) return sums[x][ind];
ll m = lx + (rx - lx) / 2;
ll s1 = find_sum(l, r, 2 * x + 1, lx, m, ind);
ll s2 = find_sum(l, r, 2 * x + 2, m, rx, ind);
return max(s1, s2);
}
ll find_sum(ll l, ll r)
{
ll res = 1;
for(int i = 0; i < n1; i++)
{
ll pr = find_sum(l, r, 0, 0, size, i);
res = modmul(res, modpower(prime[i], pr, MOD));
}
return res;
}
};
void solve()
{
ll n, q;
cin>>n>>q;
vector<ll> vec(n);
for (int i = 0; i < n; i++)
{
cin>>vec[i];
}
SegTree st;
st.init(n);
st.build(vec);
while (q--)
{
int op; cin>>op;
if(op == 1)
{
ll ind, num;
cin>>ind>>num;
ind--;
st.set_x(ind, num);
}
else
{
ll l, r;
cin>>l>>r;
l--; r--;
cout<<st.find_sum(l, r + 1)<<endl;
}
}
}
int main(int argc, char const *argv[])
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
sieve();
int t = 1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}



