This is the editorial for the Unofficial Div 4 Round #2 created by SlavicG and mesanu.
We hope everyone had fun and enjoyed the contest!
Problem A — Catching the Impostor.
The problem is just implementation. Check if exactly $$$n-1$$$ numbers are encountered. You can easily do this using a set or a frequency array.
#include "bits/stdc++.h"
using namespace std;
int main()
{
set<int> s;
int n, m;
cin >> n >> m;
for(int i = 0;i < m;i++){
int x;
cin >> x;
s.insert(x);
}
if(s.size() == n - 1){
cout << "YES";
}else{
cout << "NO";
}
}
Problem C — Similar Arrays
The array we make should consist only of the median of the array (the middle element after sorting the array). If $$$n$$$ is even and there are two medians, both medians and all values between them are optimal choices.
The median is an optimal choice because if $$$x$$$ (any element from the array) is smaller than the median, the sum becomes smaller by increasing $$$x$$$, and if $$$x$$$ is larger then the median, the sum becomes smaller by decreasing $$$x$$$. Hence, the optimal solution is that $$$x$$$ is the median.
#include "bits/stdc++.h"
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for(int i = 0;i < n;i++){
cin >> a[i];
}
sort(a,a+n);
long long ans = 0;
int median = a[n/2];
for(int i = 0;i < n;i++){
ans += abs(median - a[i]);
}
cout << ans;
}
Problem D — Sanda's Job
We need to find if the difference between $$$n$$$ and $$$x$$$ is also a permutation of $$$x$$$'s digits. There are many ways to do this, and our solution is to transform $$$x$$$ and $$$n-x$$$ into strings, then sort the strings and see if they are equal. This is possible in C++ using to_string(int value) function.
#include "bits/stdc++.h"
using namespace std;
int main()
{
long long n,m;
cin >> n >> m;
m-=n;
if(m <= 0){
cout << "NO";
return 0;
}
string s = to_string(n);
string k = to_string(m);
sort(s.begin(), s.end());
sort(k.begin(), k.end());
if(k==s){
cout << "YES\n";
}else{
cout << "NO\n";
}
}
Problem E — Count Substrings
We iterate through string $$$s$$$, and whenever we encounter a substring $$$t$$$, we add to the answer the number of unique substrings we can make using it. We can calculate this with the formula (number of characters since the start of the last encounter)$$$\cdot$$$(the number of characters until the end of the string). This is because the start of the substring must be between the start of the last $$$t$$$ substring in $$$s$$$ and the start of the $$$t$$$ substring just encountered(so we don't double count previous substrings). The end must be between the last element of this $$$t$$$ substring and the last element of string $$$s$$$.
#include "bits/stdc++.h"
using namespace std;
int main()
{
string s, t;
long long n;
cin >> n >> s >> t;
long long l = 0;
long long ans = 0;
for(int i = 0; i < n-1; i++)
{
if(s[i] == t[0] && s[i+1] == t[1])
{
long long from = i-l+1;
long long to = n-(i+1);
ans += from*to;
l = i + 1;
}
}
cout << ans << endl;
}
Problem F — Game on Grid
If $$$n$$$ % $$$4$$$ $$$=$$$ $$$2$$$ Alice wins, if not and $$$n$$$ % $$$2$$$ $$$=$$$ $$$0$$$ it is a draw and otherwise Bob wins.
Suppose that squares with odd column and odd row are coloured red, odd column and even row are coloured green, even column and odd row are coloured blue, even column and even row are coloured gray. If n = 0 (mod2) then you can split the grid into sections of squares with 4 smaller squares each, wherever Bob colours he will at most nullify 4 squares. If n = 2 (Mod 4) then if Bob always plays correct(nullifying 4 squares every moves) Alice still wins because they will play for an odd number of moves. If n = 0 (mod 4) then then Bob's strategy is to just eliminate one colour. Because Alice needs all colours present to move the game will end in $$$\frac{n^2}{8}$$$ moves, this number being even so because playing optimally they can at most get 4 squares each move each will get the same number of squares. If n is odd, then Bob will focus on the colour that is least present ( even column, even row ). Alice will get $$$4\cdot(\lfloor\frac{(n-1)^2}{8}\rfloor+1)$$$ and Bob will get $$$4\cdot\lfloor\frac{(n-1)^2}{8}\rfloor + 2\cdot n-1$$$ so Bob will get more for $$$n \geq 3$$$. Exception for $$$n = 1$$$ but its easy to see that Bob would win.
#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
if(n%4==2){
cout << "Alice\n";
}else if(n%2==0){
cout << "Draw\n";
}else{
cout << "Bob\n";
}
}
}