Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
full = 'Yes' * 18
t = int(input())
for _ in range(t):
if full.find(input()) >= 0:
print('YES')
else:
print('NO')
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
t = int(input())
for _ in range(t):
n, s = map(int, input().split())
a = [int(x) for x in input().split()]
s += sum(a)
sm = 0
cnt = 0
for i in range(1, s + 1):
if sm >= s:
break
sm += i
cnt = i
if sm != s or max(a) > cnt or cnt <= n:
print("NO");
else:
print("YES")
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
def solve():
l, r, x = map(int, input().split())
a, b = map(int, input().split())
if a == b:
return 0
if abs(a - b) >= x:
return 1
if r - max(a, b) >= x or min(a, b) - l >= x:
return 2
if r - b >= x and a - l >= x or r - a >= x and b - l >= x:
return 3
return -1
t = int(input())
for _ in range(t):
print(solve())
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define eb emplace_back
using ll = long long;
void solve() {
ll n,m; cin >> n >> m;
ll n0 = n;
int cnt2 = 0, cnt5 = 0;
ll k = 1;
while (n > 0 && n % 2 == 0) {
n /= 2;
cnt2++;
}
while (n > 0 && n % 5 == 0) {
n /= 5;
cnt5++;
}
while (cnt2 < cnt5 && k * 2 <= m) {
cnt2++;
k *= 2;
}
while (cnt5 < cnt2 && k * 5 <= m) {
cnt5++;
k *= 5;
}
while (k * 10 <= m) {
k *= 10;
}
if (k == 1) {
cout << n0 * m << endl;
} else {
k *= m / k; // 1 <= m/k < 10
cout << n0 * k << endl;
}
}
int main() {
int t;
cin >> t;
forn(tt, t) {
solve();
}
}
Idea: Gornak40
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
int n;
int arr[MAXN];
int solve(int i, long long h, int s2, int s3) {
if (i == n) return 0;
if (arr[i] < h)
return solve(i + 1, h + (arr[i] / 2), s2, s3) + 1;
int ans1 = (s2 ? solve(i, h * 2, s2 - 1, s3) : 0);
int ans2 = (s3 ? solve(i, h * 3, s2, s3 - 1) : 0);
return max(ans1, ans2);
}
int main() {
int t; cin >> t;
while(t--) {
long long h; cin >> n >> h;
for (int i = 0; i < n; ++i)
cin >> arr[i];
sort(arr, arr + n);
cout << solve(0, h, 2, 1) << endl;
}
}
Idea: senjougaharin
Tutorial
Tutorial is loading...
Solution
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int newDigit = -1;
bool check(set<int> digits, int l, int r, bool useNewDigit) {
for (int i = l; i <= r; ++i) {
if (useNewDigit && i == newDigit) {
continue;
}
if (!digits.count(i)) {
return false;
}
}
return true;
}
void solve() {
int n, p;
cin >> n >> p;
vector<int> a(n + 1);
set<int> digits;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
digits.insert(a[i]);
}
if (digits.size() == p) {
cout << "0\n";
return;
}
for (int i = n - 1; i >= 0; --i) {
if (a[i] < p - 1) {
newDigit = a[i] + 1;
break;
}
}
int l = 0, r = p - 1;
int x = a[n];
while (l < r) {
int m = (l + r) >> 1;
bool res = false;
if (x + m >= p) {
if (check(digits, x + m + 1 - p, x - 1, true)) {
res = true;
}
} else {
if (check(digits, 0, x - 1, false) && check(digits, x + m + 1, p - 1, false)) {
res = true;
}
}
if (res) {
r = m;
} else {
l = m + 1;
}
}
cout << l << '\n';
}
bool multitest = true;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(25);
size_t number_of_tests = 1;
if (multitest) {
cin >> number_of_tests;
}
for (size_t _ = 0; _ < number_of_tests; ++_) {
solve();
}
return 0;
}
1759G - Restore the Permutation
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include "bits/stdc++.h"
using namespace std;
int n;
void solve(){
cin >> n;
vector<int>b(n / 2), p(n);
vector<bool>isUsed(n + 1, false);
set<int>unused;
for(int i = 0; i < n / 2; i++){
cin >> b[i];
p[i * 2 + 1] = b[i];
isUsed[b[i]] = true;
}
for(int i = 1; i <= n; i++){
if(!isUsed[i]) unused.insert(i);
}
if(int(unused.size()) != n / 2){
cout << "-1\n";
return;
}
for(int i = n / 2 - 1; i >= 0; i--){
auto k = unused.upper_bound(p[2 * i + 1]);
if(k == unused.begin()){
cout << "-1\n";
return;
}
k--;
if(*k < p[2 * i + 1]){
p[2 * i] = *k;
unused.erase(k);
}
else{
cout << "-1\n";
return;
}
}
for(auto i : p) cout << i << ' ';
cout << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--){
solve();
}
}
I solved A by checking if there is a letter that is not in the word "Yes", and checking that each Y has s before it, each e has Y before it, and each s has e before it. Also I solved D using lcm, for each i such that 1<=i<=18 check if lcm(n,10^i)/n<=m. If it is, multiply the answer by m/answer
Great round! Hope to see another Div.3 soon. Div.3 almost always meant +ve delta for me, so I am always hyped for them.
To point out, F didn't need a very complex data structure. A single set (
a sorted set would be preferrableI just checked, an unordered set worked equally as fast) was enough. If you look at the constraints, you will notice that $$$n \leq 100$$$. Therefore, if you save digits that did exist in the original number and the number after a carry, the size of the set is guaranteed to be less than 200. Now if we follow the process I explained in that another comment, we know we can manage $$$\mathbf{pp}$$$ and $$$\mathbf{pk}$$$ linearly starting from $$$p$$$ and the number of the last digit, and the loop won't take over 200 iterations. This is more than enough to fit in the TL. (I have not tested using an array here though, but the process will be the same anyways.) One great thing to note is that this solution's time complexity only relies on $$$n$$$. Maybe for this reason (and due to some additional constants), my solution turns out to be 5x faster than the jury solution. Good to know!Here is a submission based on this approach — 181506873.
upd: The TeX for problem D's tutorial seems to be broken. Maybe you forgot come curly braces?
yeah true, I also did a similar approach
Submission: 187409654
1759D - Make It Round
Here's my clear and concise code for D
Greedy approach : We will try to append as many 0's at the end within given constraint How to find it ?
Let's take input examples : 6 11
Now we will build our answer by checking if I append zeros can I get some k such that it falls under constraint, once we can't append 0's we will break loop
How to do it? How to find possible values of k? Well it's easy, we can use some mathematics
Thanks :), You can ask doubt
I solved D with binary search for answer. I think it would be better to add binary search tag to D.
I almost copy-pasted solution for problem E from the editorial and it's giving WA on test 1?!
Submission: 181810855
Those global variables are the issue. Try declaring them inside the function.
Thank you, it worked, but why?
Not quite sure aswell
Think about how
solve()
works. First, you computeg
. No problems here. Second, you computeb
. This uses a recursive call tosolve()
, so the global variableg
may get overwritten during this subcall.Finally, you compute
max(b, g)
. This can give WA ifg
is no longer the correct value.I figured it out on my own already, but thank you for the comment anyways.
how this work i don't understand
I hope these few lines will make it clearer.
$$$\text{green}$$$ is the number of green serums left and $$$\text{blue}$$$ is the number of blue serums left.
$$$\text{g}$$$ is the answer if we use green serum at the $$$\text{i}$$$ -th step and $$$\text{b}$$$ is the answer if we use blue serum at the $$$\text{i}$$$ -th step.
When writing boolean valus,
x
is same as writingx!=0
where x is a number.For E, shouldn't it be $$$O(NlogN)$$$ instead of $$$O(N)$$$ since you need to sort the astronauts?
Good remark, thank you
Gornak40 harryzhengipsum Gornak40 can you help me calculate the TC of E? how I Calculate (No. states * transition) So, According to me, TC comes out to be n*h*s1*s2*2. Which is too high. What I am doing Wrong? I thought if I memories it will give MLE with 4 states.
Can somebody explain why the solution to D works, or what is the idea about it?
Obviously it is somehow about the number of divisiors 2 and 5 in n, but how?
And how/why does the sol find "If there are several possible variants, output the one in which the new price (value n⋅k) is maximal."?
The input number $$$n$$$ number can be factored as $$$x 2^a5^b$$$, $$$a, b \geq 0$$$
The number of zeroes at the end of this number is equal to $$$min(a,b)$$$, so our goal is to maximize $$$min(a,b)$$$
We start with $$$k=1$$$.
while $$$k \leq m$$$, do:
if $$$a < b$$$, multiply $$$k$$$ by $$$2$$$ and increment $$$a$$$.
if $$$a > b$$$, multiply $$$k$$$ by $$$5$$$ and increment $$$b$$$.
if $$$a=b$$$, multiply it by $$$10$$$. and increment $$$a$$$ and $$$b$$$.
This guarantees that $$$k \cdot n$$$ has the largest amount of zeroes at the end, this does not guarantee that $$$k$$$ is the biggest possible number, to do this, we try to make $$$k$$$ as close to $$$m$$$ as possible.
$$$k \cdot q \leq m$$$
$$$q \leq \frac{m}{k}$$$
$$$q = \frac{m}{k}$$$
So the answer is $$$q \cdot k \cdot n$$$
The greedy approach can be derived through the fact that we know that multiplying 2*5 always would add a 0 at the end, so greedy we say that instead of multiply k with 10, why not multiply it with an even smaller number -> 2 or 5, Thus i can say, we would try to equalize the count of 2 and 5.
Problem E: Please someone explain to me why we use solve(0, h, 2, 1) in the initial call, I don't understand why we use 2,1.? what's the logic behind it.?
oh, my bad, I missed that part — "the humanoid took with him two green serums and one blue serum".
I am getting no idea what we are doing in que E. why are we recursively calling that function and what that function is doing?
@hemantpatil10125 it's just an observation. Try to figure out how it works, recursion is always hard, but very easy if you can catch the basic and base cases, sometimes tricky logic. Keep trying at least 20 easy recursion problems, understand how they work, then it will be much easier. I am also a newbie, so It may sound inappropriate to make a statement about that. Hope you will expert one day if you solve problems daily.
thank u @survo_datta. What resources did you follow to learn recursion? How's striver's playlist?
@hemantpatil10125 you can follow tutorial from Luv(@iamluv) in youtube. Follow his DP series. I tried different tutorials and find his tutorial more accurate and beginner friendly though he covers many advanced problems too. Firstly you should solve problems from this: https://codeforces.me/group/MWSDmqGsZm/contest/223339 after solving all of the problems from this, follow his tutorials on youtube. you will be an expert in it.
thank you...
Can someone explain the segment tree solution for problem G?
Did you got any segment Tree approach for this problem?
I hope it's not too late, please see my comment below.
hey have you got the approach for segment tree, i could not come up with the solution of G i was trying using segment tree but i got stuck somewhere, if you got it please could you share the code.
I think I finally got it. Imagine there's a bracket sequence $$$s$$$ of length n, for each $$$i$$$, we set $$$s_{b_i}$$$ to a right bracket and set the rest to left brackets (e.g. the first example input is
(())()
). So our target would became for each right bracket we want to match it to a left bracket to its left, so the solution exists only if it's a valid bracket sequence, i.e. all prefix sums are greater than or equal to 0.Because we want the lexicographically minimal permutation, so for each $$$b_i$$$ we want to find the smallest $$$j < b_i$$$ such that if we remove $$$s_j$$$ and $$$s_{b_i}$$$, the remaining bracket sequence is still valid. We claim that we should choose the largest $$$j$$$ such that $$$pref_{j - 1} = 0$$$ (I'm too lazy to explain it so try to come up with it by yourself). In order to find such $$$j$$$, we use a segment tree by maintaining the sum, min prefix, and the position of the min prefix. See jiangly's code or my code 188831632 for implementation.
You can also use a Range Max-Query Segment tree and then binary search for each value in the unused array so that you find the right most element in the input array ($$$a_i$$$) such that $$$a_i$$$ > $$$unused_j$$$
Here is the code for this approach:
And the corresponding submission: 210703511
For the problem G, can we binary search on the value we put before each element of the input array? For each element, the search space will be the elements in the unused set which are smaller than this element. And to check if a value is feasible, we will check that we still have sufficient options left for rest of the elements if we put this value before the current element in the final array. This can be done using ordered set (refer my solution). We will choose the least among all feasible values, to maintain the lexicographic condition. Also note that the search space will follow monotonicity.
I used the above approach, but got WA on TC3. Solution: LINK
Take a look at Ticket 16479 from CF Stress for a counter example.
Thanks man
can someone tell me the mistake in my code of problem G. I have done pretty much the same as editorial's approach,but I am getting WA on test case 14. (basically,my code is also including 0 in one of the outputs which I am unable to think of why) https://codeforces.me/contest/1759/submission/202043617
You are dereferencing a pointer after you've deleted the corresponding element, hence you get a garbage value of zero as seen in the checker logs.
thanks a lot sir...!!!
1759F - All Possible Digits
I implemented the algorithm below, which is O(n) time-complexity and O(n) space-complexity. But still, I am getting TLE for the 7th test case.
Could anyone tell me the error in my code and how I can optimize the algorithm's run-time?
The code can be found here: 218600714
There are so many places, where it is not O(n), vector allocation, loops
O(p), maybe, but p = 10^9, that's why TLE,
Here is the solution that works in O(nlogn):
259545032
In problem E, I kept failing using non recursive approach, didn't think about the case where you'd use green twice
what a incredible problem E
I was only thinking the recursive DP approaches which were too slow because to it depended on the health tooo, but reading editorial solution made me think I have lot to learn bring more problems like these.
In problem G, After getting the numbers not in the array, I kept track of the number of options available to be placed before each element in b. For example, if n = 8 and b = [8, 7, 4, 5], the numbers not in the array would be [1, 2, 3, 6], and 8 and 7 would both have 4 options (as they are greater than all four numbers not in the array b), and 4 and 5 would have 3 options (as they are not greater than 6).
It makes sense that for the number of options i, at most i elements of the array can have it. Example A: for n = 8, and b = [2, 3, 7, 6], both 2 and 3 have only one option ( 1 ), but only one of them can use it, so there would be no answer. Example B: n = 8 and b = [8, 7, 4, 5], 4 and 5 have three options, so we can use two for them, and that would leave two options for 7 and 8 each.
My solution (in Java) maintained a TreeMap of the numbers of options and the indices of the corresponding elements that have those options. For every iteration, I would check the smallest number of options, and check if the elements having that number are more (which means no answer), or less (which means I have room to give the next smallest number to the next element in b), or equal (which means that they must be given the next smallest numbers).
This seems reasonable to me, and it passed the first two tests. However, I got a WA in test 3, and the test case is hidden. Can someone help me understand what is wrong with my solution? Here is my submission — 270539680.