Idea: myav
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
int sq = ceil(sqrt(n));
if (sq * sq == n) {
cout << 0 << ' ' << sq << "\n";
} else {
cout << "-1\n";
}
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
2114B - Not Quite a Palindromic String
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define int long long
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(time(nullptr));
const int inf = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
void solve(int tc){
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> cnt(2);
for(char c: s){
cnt[c - '0']++;
}
int mn = max(cnt[0], cnt[1]) - n / 2;
int mx = cnt[0] / 2 + cnt[1] / 2;
if(k >= mn && (k - mn) % 2 == 0 && k <= mx) cout << "YES";
else cout << "NO";
}
bool multi = true;
signed main() {
int t = 1;
if (multi)cin >> t;
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve(int tc){
int n;
cin >> n;
int last = -1, ans = 0;
for(int i = 0; i < n; ++i){
int a;
cin >> a;
if(a - last > 1){
ans++;
last = a;
}
}
cout << ans;
}
bool multi = true;
signed main() {
int t = 1;
if (multi)cin >> t;
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define int long long
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(time(nullptr));
const int inf = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
struct min_max{
int mx1, mx2, mn1, mn2;
void fix_mx(){
if(mx1 < mx2){
swap(mx1, mx2);
}
}
void fix_mn(){
if(mn1 > mn2){
swap(mn1, mn2);
}
}
min_max(int a, int b){
mx1 = mn1 = a;
mx2 = mn2 = b;
fix_mx();
fix_mn();
}
void add(int x){
mx2 = max(mx2, x);
mn2 = min(mn2, x);
fix_mx();
fix_mn();
}
int get_seg(int x){
pair<int, int> res = {mn1, mx1};
if(x == mn1) res.x = mn2;
if(x == mx1) res.y = mx2;
return res.y - res.x + 1;
}
};
void solve(int tc){
int n;
cin >> n;
vector<pair<int, int>> coord(n);
for(auto &e: coord){
cin >> e.x >> e.y;
}
if(n <= 2){
cout << n;
return;
}
min_max xc(coord[0].x, coord[1].x), yc(coord[0].y, coord[1].y);
for(int i = 2; i < n; ++i){
xc.add(coord[i].x);
yc.add(coord[i].y);
}
int ans = xc.get_seg(-1) * yc.get_seg(-1);
for(int i = 0; i < n; ++i){
int x = xc.get_seg(coord[i].x);
int y = yc.get_seg(coord[i].y);
if(x * y == n - 1){
ans = min(ans, min((x + 1) * y, x * (y + 1)));
}
else{
ans = min(ans, x * y);
}
}
cout << ans;
}
bool multi = true;
signed main() {
int t = 1;
if (multi)cin >> t;
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}
2114E - Kirei Attacks the Estate
Idea: Gornak40
Tutorial
Tutorial is loading...
Solution
from math import inf
from sys import setrecursionlimit
def solve(v, p, mini, maxi):
global res
res[v] = max(arr[v], mini * -1 + arr[v])
mini = min(arr[v], maxi * -1 + arr[v])
for u in gr[v]:
if u == p:
continue
solve(u, v, mini, res[v])
setrecursionlimit(400_000)
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
gr = [[] for _ in range(n)]
for j in range(n - 1):
v, u = map(int, input().split())
gr[v - 1].append(u - 1)
gr[u - 1].append(v - 1)
res = [0] * n
solve(0, -1, 0, 0)
print(*res)
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define int long long
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(time(nullptr));
const int inf = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
int get_ans(int x, int k){
if(x == 1) return 0;
vector<int> divs;
for(int i = 1; i * i <= x; i++){
if(x % i == 0){
divs.push_back(i);
divs.push_back(x / i);
}
}
sort(all(divs));
int n = divs.size();
vector<int> dp(n, 100);
dp[0] = 0;
for(int i = 1; i < n; i++){
for(int j = i - 1; j >= 0; j--){
if(divs[i] / divs[j] > k){
break;
}
if(divs[i] % divs[j] == 0) {
dp[i] = min(dp[i], dp[j] + 1);
}
}
}
return dp[n - 1] == 100 ? -1 : dp[n - 1];
}
void solve(int tc){
int x, y, k;
cin >> x >> y >> k;
int g = gcd(x, y);
x /= g;
y /= g;
int ax = get_ans(x, k);
int ay = get_ans(y, k);
if(ax == -1 || ay == -1) cout << -1;
else cout << ax + ay;
}
bool multi = true;
signed main() {
int t = 1;
if (multi) cin >> t;
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}
Idea: myav
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define int long long
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(time(nullptr));
const int inf = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
int max_op(int a, int b) {
int min_part = a;
while (min_part % 2 == 0 && min_part / 2 != b) {
min_part /= 2;
}
if (min_part % 2 == 1) {
return a / min_part;
}
int true_min = min_part;
while (true_min % 2 == 0) {
true_min /= 2;
}
return 1 + (a - min_part) / true_min;
}
void solve(int tc){
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &e: a) cin >> e;
vector<int> pre(n, 0);
for (int j = 1; j < n; ++j) {
pre[j] = pre[j - 1] + max_op(a[j - 1], a[j]);
}
vector<int> suf(n, 0);
for (int j = n - 2; j >= 0; --j) {
suf[j] = suf[j + 1] + max_op(a[j + 1], a[j]);
}
for (int i = 0; i < n; i++) {
int res = max_op(a[i], 0) + pre[i] + suf[i];
if (res >= k) {
cout << "YES";
return;
}
}
cout << "NO";
}
bool multi = true;
signed main() {
int t = 1;
if (multi) cin >> t;
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}








can someone explain me B problem editorial again, am not able to picture the suggested approach?
See first understand, if any binary string is there, swapping any two digits can change the number of palindromic pairs by either 0 or 2. Take few examples and try to wrap your head around this fact. You will understand what they are doing after that.
There is an easier way to think about this problem.
For a string of length $$$n$$$, there are exactly $$$n/2$$$ pairs (since $$$n$$$ is guaranteed to be even). So, since you want exactly $$$k$$$ identical pairs, you want exactly $$$n/2 - k$$$ non-identical pairs. Each non-identical pair uses one $$$0$$$ and one $$$1$$$. So you need at least $$$n/2 - k$$$ zeroes and ones, and you will pair them with each other. Now, for the remaining zeroes and ones, you want all zeroes paired up with zeroes, and all ones with ones. This can happen if and only if the remaining number of zeroes and ones are even. So just check that as well. Here is my code for reference. Just look at the
solvefunction.Nice, very Easy approach,thanks
In editorial for problem F, "dp[i]=max(dp[i],dp[j]+1)", shouldn't it be min instead of max, as we desire for the minimum number of operations. Correct me, if I am wrong.
yes, in fact if you look at the code they use min
About problem F:
us
In the Tutorial of F, it should be min(dp[i], dp[j]+1), not max.
D was all about implementation and the editorial code is a bit tough to understand for beginners. So, you can refer the code below.
The main idea here is to find the highest and second highest extremes in all four directions — up, down, right and left. Now, the problem is reduced to removing the extreme most monster in all 4 directions one by one and place inside the rectangle formed by taking second most extreme in that direction and extremes in other 3 directions for eg. rectangle area considering second most right extreme, and up,down,left extreme points.
But here it may be possible that a point is extreme for two adjecent directions, ie. a point on top-right corner is extreme for both right and up directions. So here, the rectangle area is considering the extremes in left and down directions and second most extreme in up and right directions. The two if-elses in AreaC() functions check these 4 corner points.
The Areak() function also takes care of the case where the rectangle is already full ,i.e., the monster removed from one of the extremes cannot be placed inside the proposed smaller rectangle, in this case either the width or the height need to increased by 1 to accomodate the ONE monster that is removed.
Note that each of the variables up, down, right and left are a pair of the highest and second highest extremes in the respective direction.
Feel free to suggest any improvements.
yes this is similar to hat I submitted in Py, and is relatively short: 321482410
Note that the corner case solves itself if you use a set of points and take the min area after removing each
Not able to understand F, please help.
I am trying to do it using prime factorisation, but i say comment that for ~20 we can't do bitmask dp, so i'm getting tle with that approach
Seeing that nobody has reposted this idea anywhere...
Including in a comment above which described 4 ways of solving F
I had actually explained my approach here
Do check it out.. Still have queries, then feel free to ask!
You may try BFS
For problem E, I have no idea why I missed the point that path is towards root during contest, and I solved a delusional version for path starting from each node by rerooting...
I'm actually surprised about how weak problem C's tests were. The fact that some people who use vector.erase() still passes the contest testcases is crazy
G is a cool problem!
Yes. G was nice. Upsolved it after contest without help. Took me around 3 hours, but it was worth it.
In D, correct me if I'm wrong, but it is enough to consider at most 4 monsters, with regard to the relocation. By this I mean, the ones on the extremes in all 4 directions. In the worst scenario, you store at most 4 monsters that you want to move and for each, you brute force normally the min and max coordinates for each axis. No need for a multi-set. For me, way easier to implement.
I tried this and it failed on test case 2. You might have more than one monster on an edge of the bounding box.
In question E, it can also solved by the dp relation f(v) = max(av,av−a(v-1)+f(v-2)) where f(v) — the maximum value of the threat of the vertex and v-1 and v-2 represent the parent and grandparent of node v. We can keep track of both parent and grandparent in vector as we do bfs or dfs down from vertex.
Thank You
what is a^2 in problem F?
$$$a$$$ is equal to the number of divisors of the number that we want to decompose into the minimum number of divisors, each of which does not exceed $$$k$$$ For numbers up to $$$10^9$$$, a is approximately $$$a = \sqrt[3]{x} = \sqrt[3]{10^9} = 1000$$$, respectively, $$$a^2 = 1000 ^ 2 = 10^6$$$
You can quickly find the divisors for a single number by factoring it.
First, we get the factorization of the number in $$$O(\sqrt{i})$$$
Next, we will create an array $$$d = [1]$$$ — these will be all the divisors of our number $$$i$$$.
Let's go through all the prime numbers in the factorization and do the following for each of them: Copy $$$d$$$ to $$$tmp$$$, Then multiply each number from $$$d$$$ by our prime number from the factorization $$$d_i\cdot p_j$$$, where $$$p_j$$$ — is a prime number from the factorization, $$$d_i$$$ — our current divisors. And at the end of each iteration, we will copy our divisors from the previous step, $$$d = d + tmp$$$
Problem F with sieve of eratosthenes+dp
Time complexity:$$$O(n\sqrt{n})$$$
Sample code:321647158
Observation:First try to make so simple observation,you can see that if there is a prime number $$$p$$$ exist at $$$x$$$ but not in $$$y$$$ then we need to divide it,similarly for multiply,so first we know is if the $$$p \gt k$$$ we have not solution,so let us define two number $$$I$$$ and $$$J$$$,$$$I$$$ is the number x need to multiply and $$$J$$$ is the number x need to divide,if we can get $$$I$$$ and $$$J$$$ then the problem of us will reduce to how i use the number not greater than k to construct the $$$I$$$ and $$$J$$$.
I believe most of you already know the trick,for a standard sieve of eratosthenes(short form SIE),we can build an array in $$$O(NloglogN)$$$ to check a number is a prime or not,but additional we can extend the SIE to built an array $$$minprime[i]$$$ denoted the minimum prime factor in number i,so after this you can just simply do a while loop to prime factolization a number in $$$O(logN)$$$,so first prime factorize $$$x$$$ and $$$y$$$ then compare their prime factor from low to high,if they share common prime factor then just calculate it should be multiply or divide,if they don't share the prime factor also do the same thing then you can get $$$I$$$ and $$$J$$$
After this the problem of us is how to construct $$$I$$$ and $$$J$$$ will least factor such that $$$\forall factor \le k$$$,I will introduce a dp solution,we define $$$dp[i]$$$ is the minimum number to construct number i by using factor less equal than k,then the transition is $$$dp[i]=min(dp[i],dp[j]+1),\forall j\mid i\;and\;\frac{i}{j}\le k$$$ why?,the logic here is we observe that if we want to get $$$i$$$ first we need to reach the factor of $$$i$$$ then we multiply a number and reach $$$i$$$,so if $$$j$$$ can completely divide $$$i$$$ and $$$\frac{i}{j}\le k$$$ then obviously the minimum number to reach $$$j$$$ + one step to $$$i$$$,so we can just brute force the dp solution,and the reason it work and won't TLE is for a number $$$num\le 10^{6}$$$ there is at most 240 factor,so $$$240^2$$$ of a dp solution won't TLE.
I'm curious about how "240" is got
Use your computer and write a $$$O(nlogn)$$$ factor count algorithm,remember you are not alone when doing content,your computer is your best teammate
Editorial isn't linked on contest page.
I don't understand this hate for D. It was simple to do in constant time, no? 🤥 it took me 5 minutes to do it. Submission
It's linked on announcement page, why didn't you check it
Exactly D was very obvious to me.
I would say I even suffered with B more than D.
can somebody explain me the problem D? I am not able to understand how we are finding the maximum coordinates
Using a C++ Multiset. code
.
I solved problem 2114F - Small Operations using simulated annealing, which is certainly not the best way, but it was interesting. My solution 321712725. I create arrays of prime factors to divide and multiply our number by, and then try to greedily divide them into groups with product less than k, starting from the beginning of the array.
Why can't we solve it without simulated annealing, I used the same approch without annealing and got WA on 3
Let's look at an example. x = 1, y = 2^4 * 3^3 ^ 7^2 ( = 21168), k = 28. Then your algorithm will combine the prime factors into the following groups: [7, 3], [7, 3], [3, 2, 2, 2], [2]. It would be better though: [7, 2, 2], [7, 2, 2], [3, 3, 3]
Got it, thanks dude
can't see your solution dude
My Solution
how do we prove that the greedy way above of finding the max number of operations for G is correct ? why cant we do it in more operations ?
In problem G, we can calculate $$$x$$$ using
c&-cin $$$O(1)$$$ time, then the algorithm can be finished in $$$O(n)$$$.Problem F, as some people have already mentioned, can be reduced to this problem. However, I did not see any comment explaining a good way to apply this standard technique to the problem F.
The main simplification is the fact that our bits in bitmasks correspond to prime factors of $$$x$$$, and each mask represents multiplication of these prime numbers, which is some factor of $$$x$$$. This way we can get rid of actual bitmasks and bits, and consider only factors and prime factors of $$$x$$$.
Now, the transitions are the pretty much the same as in the linked problem. For each factor of $$$x$$$ we divide it for each prime factor, and update our $$$dp$$$ state for this factor, which is the total number of subsets, and value of the current subset. This gets us $$$O(d(x) * p(x) + sqrt(x))$$$ solution where $$$d(x)$$$ is the number of factors of $$$x$$$, and $$$p(x)$$$ is the number of distinct prime factors of $$$x$$$. Here is my code, but it's pretty messy. Hope this helps.
For problem F, why is this solution giving TLE ??
322437145
c can be simply solved with stack and returning the size of stack just implementation question of stack