We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: tt12
Bob will always be able to construct the lexographically largest permutation using the magic spell.
Why?
Let's look at the proof :
By applying the spell on the second index, Bob can generate $$$2$$$ distinct permutations.
Similarly, casting the spell on the third index enables him to create $$$3 * 2 = 3!$$$ different permutations $$$(3$$$ swaps with $$$2$$$ distinct previous permutations.$$$)$$$
Extending this pattern, when the spell is used on the fourth index, Bob can construct 4 * 3 * 2 = 4! permutations $$$(4$$$ swaps with $$$3 * 2$$$ distinct previous permutations.$$$)$$$
In general, by casting the spell on the $$$(n-1)th$$$ index, Bob can generate $$$n!$$$ different permutations, calculated as $$$n * (n-1) * (n-2) * ... * 3 * 2$$$.
Since the number of permutations of size $$$n$$$ are $$$n!$$$, Bob will be able to construct any permutation using the magic spell.
The difference thus can be calculated easily.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void solve(){
ll n, x;
cin >> n;
for(int i=0; i<n; i++){
cin >> x;
}
cout << (n + 1) / 2 << '\n';
}
int main(){
int t;
cin >> t;
while(t--){
solve();
}
}
Author: tt12
Let's solve for the case where there is no constraint on consecutive moves. Hence the final path from the source to the destination would consist of only 2 type of moves each from (either $$$L$$$ or $$$R$$$) and (either $$$U$$$ or $$$D$$$). Let's assume these are $$$R$$$ and $$$U$$$ moves, with their respective counts as say, $$$k_1$$$ and $$$k_2$$$. We also understand that any permutation of these set of moves will result in us reaching the destination. Thus, the final answer for this case will be $$$k_1*r + k_2*u$$$.
Now with the condition on consecutive moves, the optimal way of moving would be to move equal number of $$$R$$$ and $$$U$$$ moves in an alternating manner. We would then be left with only one type of move to perform $$$($$$assume $$$k_1 \gt k_2$$$ and hence cost till now $$$= k_2*r + k_2*u)$$$. Therefore, we would have now reached the $$$y$$$ coordinate of the destination.
Finally, to move to the $$$x$$$ coordinate of the destination, we have to perform the remaining $$$(k_1 - k_2)$$$ $$$R$$$ moves. Since these moves are not allowed to be consecutive, we must perform either a $$$U$$$ move or a $$$D$$$ move after every $$$R$$$ move such that the $$$y$$$ coordinate remains unchanged at the end. Hence during these set of moves, $$$|U| = |D|$$$. Hence, $$$(k_1 - k_2) / 2$$$ moves both of the type $$$U$$$ and $$$D$$$ must be performed in between the $$$R$$$ moves.
$$$Final cost = k_2*(r + u) + (k_1 - k_2)*r + (k_1 - k_2)/2 * (u + d)$$$.
To reduce the complexity of solving multiple types of cases, one can rotate the graph by swapping values of $$$x, y, l, r, d$$$ and $$$u$$$ accordingly so as to always solve for a specific type of solution.
Time complexity: $$$\mathcal{O}(1)$$$
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void solve(){
ll n, m, c, x1, y1, x2, y2, l, r, d, u;
cin >> n >> m >> c >> x1 >> y1 >> x2 >> y2 >> l >> r >> d >> u;
if(x1 > x2){
swap(x1, x2);
swap(l, r);
}
if(y1 > y2){
swap(y1, y2);
swap(u, d);
}
if(y2 - y1 > x2 - x1){
swap(x1, y1);
swap(x2, y2);
swap(u, r);
swap(l, d);
}
ll xdif = x2 - x1;
ll ydif = y2 - y1;
ll ans = ydif * (u + r) + (xdif - ydif) * r + (xdif - ydif) / 2 * (u + d);
cout << max(-1ll, c - ans) << '\n';
}
int main(){
int t;
cin >> t;
while(t--){
solve();
}
}
Author: vrintle
As we have $$$t_i$$$'s only till $$$n \leq 2 \cdot 10^5$$$, therefore we can easily say that the bit count can be upto $$$\log(n) = 18$$$. Let's iterate over every bit count and maximize our answer for each group of nodes.
Let's consider current bit count to be $$$x$$$, so let's mark nodes with bit count $$$x$$$ in red color, and the other nodes in black color. So, this problem boils down to finding maximum distance between any two red colored nodes (a similar codeforces problem). The answer to this sub problem is the diameter of this red-black tree. Note that, in this version, the diameter ends will always be red nodes.
Time complexity: $$$\mathcal{O}(n \cdot \log{n})$$$
PS: I have thought about this partial diameter problem before the actual div3 contest linked above.
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
const int N = 2e5 + 7;
vi g[N];
void solve() {
int n;
cin >> n;
vi a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
a[i] = __builtin_popcount(a[i]);
g[i].clear();
}
for(int _ = 0; _ < n — 1; _++) {
int u, v;
cin >> u >> v;
u--; v--;
g[u].push_back(v);
g[v].push_back(u);
}
int ans = 0;
for(int b = 1; b <= 20; b++) {
vi dist(n, inf);
function<void(int, int)> dfs = [&](int u, int p) {
for(auto& v: g[u]) {
if(v == p) continue;
dist[v] = dist[u] + 1;
dfs(v, u);
}
};
for(int i = 0; i < n; i++) {
if(a[i] == b) {
dist[i] = 0;
dfs(i, -1);
break;
}
}
int mx = 0;
for(int i = 0; i < n; i++) {
if(a[i] == b) mx = max(mx, dist[i]);
}
for(int i = 0; i < n; i++) {
if(a[i] == b && dist[i] == mx) {
dist.assign(n, inf);
dist[i] = 0;
dfs(i, -1);
break;
}
}
for(int i = 0; i < n; i++) {
if(a[i] == b) ans = max(ans, dist[i]);
}
}
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef VRINTLE
freopen("./tests/tree_27.txt", "r", stdin);
freopen("./out.txt", "w", stdout);
freopen("./err.txt", "w", stderr);
#endif
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
}
return 0;
}




