Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, a, b, c;
cin >> t;
while (t--) {
cin >> a >> b >> c;
cout << (a + c) % 2 << '\n';
}
return 0;
}
1582B - Luntik and Subsequences
Idea: AlFlen
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n, x;
cin >> t;
while (t--) {
cin >> n;
int cnt0 = 0, cnt1 = 0;
for (int i = 1; i <= n; ++i) {
cin >> x;
if (x == 0) cnt0++;
if (x == 1) cnt1++;
}
cout << (1ll << cnt0) * (ll)cnt1 << '\n';
}
return 0;
}
1582C - Grandma Capa Knits a Scarf
Idea: AlFlen
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n;
cin >> t;
while (t--) {
string s;
cin >> n >> s;
int ans = n + 1;
for (int c = 0; c < 26; ++c) {
int l = 0, r = n - 1, cnt = 0;
while (l <= r) {
if (s[l] == s[r]) {
l++, r--;
}
else if (s[l] == char('a' + c)) {
cnt++, l++;
}
else if (s[r] == char('a' + c)) {
cnt++, r--;
}
else {
cnt = n + 1;
break;
}
}
ans = min(ans, cnt);
}
if (ans == n + 1) ans = -1;
cout << ans << '\n';
}
return 0;
}
Idea: AlFlen
Tutorial
Tutorial is loading...
Solution (AlFlen)
ttt = int(input())
for t in range(ttt):
n = int(input())
a = [int(x) for x in input().split()]
start = 0
if n % 2 == 1:
if (a[0] + a[1] != 0):
print(-a[2], -a[2], a[0] + a[1], end = " ")
elif (a[1] + a[2] != 0):
print(a[2] + a[1], -a[0], -a[0], end = " ")
else:
print(-a[1], a[0] + a[2], -a[1], end = " ")
start = 3
while start < n:
print(-a[s + 1], a[s], end = " ")
start += 2
print()
1582E - Pchelyonok and Segments
Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 1e5 + 5, maxk = 450;
int a[maxn], dp[maxn][maxk];
int inf = 1e9 + 7;
ll pref[maxn];
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
int k = 0;
while (k * (k + 1) / 2 <= n) k++;
for (int j = 0; j < k; ++j) {
dp[n + 1][j] = -inf;
}
dp[n + 1][0] = inf;
for (int i = n; i >= 1; --i) {
for (int j = 0; j < k; ++j) {
dp[i][j] = dp[i + 1][j];
if (j && i + j - 1 <= n && pref[i + j - 1] - pref[i - 1] < dp[i + j][j - 1]) {
dp[i][j] = max(dp[i][j], (int)(pref[i + j - 1] - pref[i - 1]));
}
}
}
int ans = 0;
for (int j = 0; j < k; ++j) {
if (dp[1][j] > 0) ans = j;
}
cout << ans << '\n';
}
return 0;
}
1582F2 - Korney Korneevich and XOR (hard version)
Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
int const max_value = (1 << 13);
vector < int > g[max_value];
int ans[max_value], r[max_value];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, x;
cin >> n;
ans[0] = 1;
for (int i = 0; i < max_value; ++i) {
g[i].push_back(0);
}
for (int i = 0; i < max_value; ++i) {
r[i] = max_value;
}
for (int i = 1; i <= n; ++i) {
cin >> x;
for (auto key : g[x]) {
int to = (key^x);
ans[to] = 1;
while (r[to] > x) {
r[to]--;
if (r[to] != x) g[r[to]].push_back(to);
}
}
g[x] = {};
}
int k = 0;
for (int i = 0; i < max_value; ++i) {
k += ans[i];
}
cout << k << '\n';
for (int i = 0; i < max_value; ++i) {
if (ans[i]) cout << i << " ";
}
cout << '\n';
return 0;
}
Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 1e6 + 5;
int a[maxn];
int prime[maxn];
int L[maxn];
vector < int > pos[maxn];
inline void add(int x, int l) {
L[l] = l;
while (x > 1) {
pos[prime[x]].push_back(l);
x /= prime[x];
}
}
inline void del(int x, int l) {
if (x == 1) {
L[l] = l;
return;
}
L[l] = l;
while (x > 1) {
if ((int)pos[prime[x]].size() == 0) {
L[l] = 0;
return;
}
L[l] = min(L[l], pos[prime[x]].back());
pos[prime[x]].pop_back();
x /= prime[x];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 2; i < maxn; ++i) {
if (prime[i] == 0) {
prime[i] = i;
if (i > 1000) continue;
for (int j = i * i; j < maxn; j += i) {
prime[j] = i;
}
}
}
char type;
for (int i = 1; i <= n; ++i) {
cin >> type;
if (type == '*') add(a[i], i);
else del(a[i], i);
}
ll answer = 0;
vector < pair < int, int > > f_min;
for (int i = n; i >= 1; --i) {
int cnt = 1;
while ((int)f_min.size() > 0 && f_min.back().first >= L[i]) {
cnt += f_min.back().second;
f_min.pop_back();
}
f_min.push_back({L[i], cnt});
if (L[i] == i) answer += (ll)cnt;
}
cout << answer << '\n';
return 0;
}
Lighting Fast Editorial and Thanks for the wonderful problems.
Sure, "lightning fast editorial"
Thanks for the contest and the lightning fast editorial, but on my computer the tutorial doesn't work, it says
it would be great if you could fix
The editorial is currently not working though it's already written, we're trying to find out why.
UPD: I've added ABC by hand
UPD2: It's working now
Can someone tell why my logic is failing in B?? I am getting WA in B. ~~~~~ //My short logic cnt = count of 1; cnt0=count of 0 ; final answer will be - cout<<(cnt*pow(2,cnto))<<"\n"; ~~~~~
pow return answer in floating point format. (so, maybe precision error) I also used it and got a WA. Use a loop or self-defined function to calculate power.
I also used the power function which works in O(logy) but this also failed . Can you tell why ?? ~~~~~
ll power(long long x, long long y) { int res = 1; // Initialize result
// Update x if it is more than or // equal to p
}
~~~~~
you declared your result as int. change it to long long
Why do you have the variable res as an int? This will obviously overflow since int can only hold up to 32 bits
Can use
1ll << i
in place ofpow(2,i)
just use the bit operator << for powers of 2 in general.
A usual double uses only 56 bits of precision, but we can have up to 59 zeros. Consider to use 64-bit integers and shift operation instead of pow().
dont forget that you must use long long
2^cnto can go beyond Integer max, maybe use 2ll?
if only has 1,the answer is 1.
AlFlen for D for the case where all elememnts are 10000 and n=100000 then the sum of abs(bi) = 1000010000, which is >10^9 Acc to editorial.
it will only happen for odd numbers(when n is odd) since 10^5 is even it is possible to make it in <=10^9. The highest odd number it can go is 10^5 -1. so the sum will go till 10^4*(10^5-1)+10^4... which is 10^9.
When n=1e5, since n is even the overall sum will be sum(abs(ai))<=1e9. And the largest odd value of n is 1e5-1, in that case one of abs(bi)>=1e4 and hence the total sum still remains less than 1e9.
Problem B cpp failed sol | Problem B python passed sol any idea why cpp solution failed while python passed? they were the same pseudo-code :/
Because the answer will exceed the range of int. You should use long long instead.
Problem — B
https://mirror.codeforces.com/contest/1582/submission/132910881 -- WA on pretest 3
https://mirror.codeforces.com/contest/1582/submission/132911508 -- WA on pretest 3
https://mirror.codeforces.com/contest/1582/submission/132911986 -- pretests passed
Is'nt left shift and logarithimic power method better than pow??
why did my solution with left shift failed and how it is different from one in the editorial
anyone can explain why??
thanks in advance.
1L
is 32-bit, better use1LL
Adding to this, logarithmic power doesn't make any sense for this question. And the pow works because it is deduced as pow(long long, long long) since zc is long long.
Shift only works till integer max value. Write your own function for computing long power
No, shift works on long longs too. But use 1LL instead of 1 and 1L. Like 1LL<<n
You have to use 1LL<<zc because 1<<zc will overflow the int type.
For Problem B,
The suggested solution returns an answer of 4 subsequences for the testcase below:
1
3
0 0 1
When I counted I could only get 3 subsequences:
[empty set]
0
0 0
Could somebody tell me if I had misunderstood? Sorry for bad formatting and thanks in advance.
There are two sets with a single '0', because it is two different positions.
I see... perhaps the question could have been more clear about the definition of a distinct subsequence. It appeared to me that subsequences would be equivalent if they had the same elements in the same order. Thanks for the clarification!
Both 0s are considered different hence total number of sequences will be 4. "0" sequence will be twice. Basically ans=pow(2,zeros)*ones
There are two subsequences which consist of just 0
The subsequences are considered different if they containt elements with different indices, so $$$0$$$ (index $$$1$$$) and $$$0$$$ (index $$$0$$$) are two different subsequences.
Стоит сказать, что при буквальном прочтении русского условия это неправда.
Здесь ничего про индексы не говорится, и исходя из условия, у последовательности не может быть две различных подпоследовательности вида [0]
It's unfortunate that the statement missed the definition of different subsequences, but samples could clear up the misunderstandings, since there was a test with equal in elements but different in indices subsequences. My bad there was no clearer explanation in the legend part.
weak pretests!!! rnm tq!
The editorial is working now
problem A was really misleading and harder than most div2 A
I probably overkilled it, but i tried a fenwick tree solution to F1 but it gives WA on test 7. Could someone help?
https://mirror.codeforces.com/contest/1582/submission/132929945
Why does my code fail on D?
I took the cross-product of the first three elements with (1,1,1) and handled the parallel vector case separately.
Please don't downvote, I might be completely wrong
I had the same approach — consider the vector (1,2,2), i.e. where two elements are the same. Your answer outputs a zero for the first coefficient, which is not allowed.
D is slightly harder if sum of absolute values of $$$b$$$ is restricted to $$$n * max(|a_i|) + 1$$$. I accidentally solved this problem. Anybody interested can try too.
yes, we can let $$$b_i = {-1, 1}$$$ for $$$1 \le i \le n-1$$$, then we've got $$$s = \sum\limits_{i=1}^{n-1}a_i \cdot b_i \le \max(a_i)$$$ and of course $$$a_n \le \max(a_i)$$$. Then we let $$$b_{n} = -s$$$ and multiply every $$$b_i, 1 \le i \le n-1$$$ with $$$a_{n}$$$
How do you solve it?
In fact, we can improve this bound on $$$\sum |b|$$$ to $$$O(n+\sum\limits_{x\in S}|x|)$$$, where $$$S$$$ is the set of values in $$$a$$$ without duplicates. We first process values of $$$a$$$ that are the same by a construction of $$$[1,1,1,\dots,-1,-1,-1,\dots]$$$ or $$$[2,1,1,\dots,-1,-1,-1\dots]$$$, and then process values of $$$a$$$ that only occur once with the original solution.
I spent so long implementing this before realizing that when $$$n$$$ is odd it is at most $$$10^5-1$$$ :/
its atmost 10^5 know?
also if i use editorial approach when $$$ n=10^5 $$$ and all $$$a[i]= 10^4 $$$,wont the sum of $$$ b[i] $$$ becomes $$$ 10^9+10^4 $$$ ? or am i doing wrong ? :( please anyone clarify
D can be solved using the pigeon hole principle as there exist significantly many repetitions in the extreme case where $$$n=10^5$$$ and $$$a_i$$$ takes at most $$$2\times10^4$$$ values, we can sort and eliminate adjacent identical elements using $$$(-1, 1)$$$ which significantly reduces the absolute sum of $$$b_i$$$'s, remaining cases can be handled similar to the editorial.
Actually Could someone pls tell me why my approach on D failed ? consider a[1] * x + a[2] * y = a[3] + a[4] + ...
let t be the gcd(a[1], a[2]) let v1 = a[1] / t, v2 = a[2] / t
so t1 * x + t2 * y = sum, where sum = a[3] + a[4] + ...
we can solve for x and y using the extended eucledian method for finding the inverse and handling some corner cases. (when t2 * y = sum, ... )
Then we assign b[3] = b[4] = ... = t and a[1] = -x, a[2] = -y
Let g be gcd(-x, -y, t) divide the whole b array by g and print the answer.
But wrong answer on test 3 submission. Any help would be appreciated.
EDIT : Ignore this. The hypothesis was wrong.
Let's notice that since a,b,c≥1, it is possible to make a concert of any duration from 0 to S
Seems like you are solving a diophantine equation. But can I get more explanation on that statement please?
In problem A, Can someone explain why we can make every value from 0 to S?
The way I see it is: for numbers that require less than all the segments of length 3, you can always write as 3k + r, where k is how many 3 length segments you take, and if r is 1 or 2, you use the corresponding length (there's at least one of each). If you need all the ones of length 3, just repeat this for the remaining length with those of length 2 and 1, namely, if it's even you use the 2's and you're done, and if it's odd, just add one of length 1.
Thanks a lot. I got it
If you need all the ones of length 3, just repeat this for the remaining length with those of length 2 and 1, namely, if it's even you use the 2's and you're done, and if it's odd, just add one of length 1 The above statement is not clear .can you please explain
You're right. The idea is correct but my last statement was not. Suppose you had to use all length 3 segments. Then proceed like before for the new sum $$$x-3c$$$, where $$$x$$$ was the sum you initially wanted, but you now ran out of length $$$3$$$ segments.
For numbers that require less than all the segments of length $$$2$$$, you can always write as $$$2k + r$$$, where $$$k$$$ is how many $$$2$$$ length segments you take, and if $$$r$$$ is $$$1$$$, you use one $$$1$$$ length segment (there's at least one).
Note that I kind of copypasted my message from before, changing $$$3$$$ by $$$2$$$, to show the idea is analogous.
Now if you had to also use all segments of length $$$2$$$, you now only have segments of length $$$1$$$, so you just need to add these until you get the desired sum trivially (you can get up to the maximum sum $$$S$$$).
My approach to F2 seems a bit different from the editorial's:
Say $$$dp_i$$$ contains all values $$$(ind_j, v_j)$$$, sorted by $$$ind_j$$$, such that it is possible to get an increasing subsequence with an xor of $$$v_j$$$ and last element at index $$$ind_j$$$, using only elements in the array less than or equal to $$$i$$$. If there are multiple possible values for one $$$v_j$$$, only store the one with the minimum $$$ind_j$$$.
To transition from $$$dp_{i - 1}$$$ to $$$dp_{i}$$$, first, add all elements already in $$$dp_{i-1}$$$ to $$$dp_i$$$. Then, for each $$$(ind_j, v_j)$$$ in $$$dp_{i-1}$$$, take the minimum index $$$k$$$ such that $$$a[k] = i$$$ and $$$k > ind_j$$$. Then, add $$$(k, v_j \oplus i)$$$ to $$$dp_i$$$ (if this $$$k$$$ does not exist, don't do anything). This can be done in linear time with two pointers if you store the occurrences of each $$$i$$$.
Two important details:
My code: 132932565
Korney Korneevich sounded like gennady korotkevich to me :-)
How did it sound like?
Sounded like your mom screaming in the bed with me.
Wow !! Since my mom is not alive now, I'm really glad her ghost is Fucking You (~‾▿‾)~
round
132925114, this is my solution for E, i am getting RE on test 7 with exit code 2147483647, can someone please help.
I wonder whether it is possible to solve F2 with python? (python 3.8 or pypy or pypy-64) indeed python is somehow disadvantaged...but indeed it is easier to write-.-
Where is the editorial for F1?
Are there any other solutions for D?
except editorial
Can you somehow solve D with a Diophantine equation? I tried to, but I failed on the third test.
not needed just make pairs if n is even else make pairs with n-3 elements and for first three give a[2] value for result[0] = a[2] and result[1] = a[2] then calculate result[2] = -( a[0]*result[0] + a[1]*result[1] ) / a[2] if a[2] is zero make result[0] = 2a[2] and then calculate result[2]
code
Could someone plz share the tutorial of problem F1 which has a relatively easier solution? Thx!
Solution for 1582F1 - Korney Korneevich and XOR (easy version):
We want to find all $$$x$$$ such that there exists a strictly increasing subsequence with xor sum equal to $$$x$$$ in the given array. Since $$$0 \le x \le 500$$$, there are about $$$512$$$ possible values of $$$x$$$, and they form a contiguous range — $$$x \in [0, 512]$$$. So let's do some dynamic programming where $$$\it{dp}_x$$$ stores the smallest possible ending value of a subsequence which has a xor sum of $$$x$$$. We store the smallest ending value of a subsequence with xor sum $$$x$$$ because when we want to form a particular xor sum by including an element, our strictly increasing condition must be satisfied and storing the smallest value allows for a greater range of new ending values. We can initially set $$$dp_x$$$ to $$$0$$$ and all the other values to $$$\infty$$$. To transition between states, iterate over the elements in the array and for each value of $$$x$$$ try to find a subsequence which makes that $$$x$$$ possible. Minimise $$$dp_x$$$ with $$$A_i$$$ if $$$A_i > dp_{x \oplus A_i}$$$. Finally, we can form xor sum $$$x$$$ for all $$$x$$$ where $$$dp_x < \infty$$$. This works in $$$O(N\times\it{max}(A_i))$$$ which is $$$10^5 \times 500 = 5 \times 10^7$$$ operations in the worst case.
Take a look at my submission for more clarity: 132945454.
I am dumb can you explain it like there is an animal in front of you
Karabasan and Mustafiz, I have edited my explanation and have made it a lot more detailed.
wow. i understood. thanks a lot brother.
brother, please briefly explain how it is work?
C reference code is written very well!!!It's better than what I wrote.
I have a simmilar solution for C that should have better complexity:
First notice that the center of the palindrome can vary, but the edges don't. Because of this we can check the first with last, second with second to last, ... and stop when we find 2 different chars or we checked all pairs(answer is 0).
When we find some
i
such thatS[i]!=S[N-1-i]
(with 0 based indexing) there are 2 possibilities:The reason why there are only 2 cases is that if we remove some other character then we will still have those 2 characters mismatched. So we need one of them to go away.
The complexity would be O(N) with at most 3 passes through the string. The solution presented in the tutorial does a similar thing but doesn't stop when it finds the different characters and so it loses time. With some tighter time limit(Or if we expand the alphabet, maybe with upper case letters or diggits) it might not fit.
As always, here are the video solutions for the first 6 problems(A to F1) :
solutions
There is an easy solution for F2. Let dp[i] be the first index in which its possible to make an increasing subsequence of xor i. Lets also maintaing a vector of vectors V such that V[i] stores all the occurences of i in the array in order.
We can now iterate through the values x in increasing order, and for we to update dp[i] we do dp[i] = min(dp[i],V[x].upperbound(dp[i^x])).
This works in $$$O(n+maxa^2*log(n))$$$: https://mirror.codeforces.com/contest/1582/submission/132952152
could you explain how this solution ensure subsequence is increasing ?
You are iterating through the values in increasing order, so you just worry about the index. If youre worried about repeating one of the values in the answer, notice that in that case you cant lower the value of the dp when we try to do that, so it wont count in the answer
got it, thanks :)
the concept is still not clear. Could you please elaborate more?
What exactly wasnt clear?
Also try checking the code and locate where im doing what im saying.
Hey so (i^x) might be larger than i or smaller than i.... So how do u decide order of traversal of states
We are iterating through the values of $$$x$$$ in increasing order so that every recurrence of the dp preserves that the sequence is increasing (and the search on the vector of ocurrences preserves the order of the indexes)
Also notice that even though choosing the same number twice is invalid it doesnt actually change the dp because its always unoptmimal.
Proof for problem A solution
Can someone explain if this solution to problem G works? I was confident that I had a simple $$$\Theta(n)$$$ solution, but it's failing on test case 18 for some reason so now I'm feeling doubtful. I don't know if the problem is my code or my proof though.
Here is the proof:
We scan the array $$$a$$$ of size $$$n$$$ from left to right. For any integer $$$r$$$, we define $$$S_r$$$ to be the set of possible starting points $$$l$$$ such that the segment $$$[l;r]$$$ is simple. Then it follows from the definition that the solution to the problem is $$$\sum_{r\in [0,n)} |S_r|.$$$ Now, we consider how to compute $$$S_r$$$ from $$$S_{r-1}$$$. If $$$b_r$$$ is '*' or $$$a_r$$$ is 1, then $$$S_{r} = S_{r-1}\cup {r}$$$, since any path that worked before will still work, and the path starting at $$$r$$$ will also work.
Now, if $$$b_r$$$ is '/' and $$$a_r$$$ is not 1, then to get $$$S_r$$$, it suffices to take $$$S_{r-1}$$$ and remove invalid starting positions. (Namely, we remove any element $$$l\in S_{r-1}$$$ such that the segment $$$[l;r-1]$$$ ends with a number that does not divide $$$a_{r}$$$.) Observe that for any two elements $$$l_1 < l_2 \in S_{r-1}$$$, if the segment $$$[l_2;r]$$$ is simple, then the segment $$$[l_1;r]$$$ is also simple. To see this, first note that the segment $$$[l_1, l_2]$$$ must be simple, since otherwise, $$$[l_1, r-1]$$$ would not be simple, contradicting the fact that $$$l_1\in S_{r-1}.$$$ Thus we just need to remove the largest few elements of $$$S_{r-1}$$$ to compute $$$S_r$$$.
For any two consecutive elements $$$l_i, l_{i+1} \in sorted(S_{r-1} \cup r)$$$, we can say that the value of the path $$$[l_i;l_{i+1}]$$$ is the value remaining after traversing this path. To compute $$$S_r$$$, we continuously pop the largest-indexed element from $$$S_{r-1}$$$ and divide $$$a[r]$$$ by that element's value until $$$a[r]$$$ is 1. Some caution is needed to maintain the invariant — you need to update the last value you popped out from being the value of a path $$$[l_i;l_{i+1}]$$$ to the value of the path $$$[l_i, r]$$$.
Here is the code: https://mirror.codeforces.com/contest/1582/submission/132953817
In the code, V is a stack of the value of paths $$$l_i, l_{i+1} \in sorted(S_{r-1} \cup r)$$$.
It doesn't work, check this simple counter example:
I think you will be able to understand why it doesn't work by yourself.
*I tried the same :)
Maybe maintests of F2 are a bit weak.
My sulotion to F2 has only considered the first and last three times of occurrences for each different elements.
So can somebody hack me or prove that it's correct.
Here it is
Anyway,There is also a sulotion has passed maintests but hacked by me. this one
Perhaps we need stronger maintests
Done.
I want to understand F1!
I have another solution for E that works in $$$O(NlogN)$$$. Maybe it is hackable because I am not good at proving.
Here is the submission: 132984246
Reverse the array first. Let $$$dp_{i,j}$$$ be the maximum sum on the segment of length $$$j$$$ considering all elements on the prefix $$$i$$$.
Then the transition should be like:
I added
sum >=dp[i-1][j]
to the condition because ifsum<dp[i-1][j]
thensum>=dp[i+j-1][j]
will not holds.It can be noticed that $$$dp_{i,j} \ge dp_{i,k}$$$ if $$$j \le k$$$ (Like LIS) and
sum
is increasing, so the conditionsum>=dp[i-1][j]
holds for monotonicity. Also,sum<dp[i-1][j-1]
must hold, too.Therefore, the
if condition
will be triggered at most once for every $$$i$$$: when the first $$$j$$$ such that $$$sum(i..(i+j-1))\ge dp_{i-1,j}$$$. And we can use Binary Search to find such $$$j$$$.I request proof of 1582C - Grandma Capa Knits a Scarf. Why this approach indeed find optimal removal for fixed letter? AlFlen can you explain?
Think of the greedy algorithm as of a forming a palindrome from the beginning and from the end. If the symbols are equal, there is no need to erase them, and if they are not, the palindrome can't be formed without the erasure (we can't choose which symbol to erase since only one can be equal to the chosen letter).
Why us not erasing one of equal symbols will not lead to a wrong answer: since they are equal, it's either we can erase both or we can't erase any, and we won't break any future pairs in palindrome (both current symbols are equal to $$$c$$$) because if one of them could be paired (in palindrome) with another symbol (equal to $$$c$$$ since we have the ability to erase current symbols) but it didn't, it's no matter what to erase: current symbol or the future one, but with not erasing the current one we don't break our palindrome sequence and don't disturb further formation.
It's easy to say, but why it doesn't break? I'm starting to understand. If we remove pair — obviously better to keep pair. But if we remove one when we could keep pair, then $$$c$$$ will remain at one side, and we either finally keep it, then it will be paired with other $$$c$$$, so we could pair it in the first place, or it will be removed, so we could remove it in the first place. Not sure if it's rigorous.
Can Anyone Explain or prove point 1 of 1582F2 — Korney Korneevich and XOR (hard version) Tutorial (using a test case would be very helpful) . example test case is:
12
4 2 6 5 2 4 5 6 4 2 5 6
Hello guys,
So, in problem E I tried implementations with 2D array as well as vector<vector >. However, only vector implementation passes and the array implementation gives runtime error on test 8. Can somebody explain this? Besides, the vector implementation needed 1900 ms to pass which either means that the 2s bound was tight or I have implementation issues. Can anyone suggest improvements for vector implementation?
vector impl: 133108745 array impl: 133108661
can anyone pls explain the last line of editorial of B cout << (1ll << cnt0) * (ll)cnt1 << '\n';
(1ll << cnt0) * (ll)cnt1 =( 2^(cnt0) ) * cnt1, where cnt0 = # of 0's and cnt = # of 1's in the array a.
I am getting TLE for Problem E but I think my solution is O(nk), k = sqrt(n). Not sure why. could anybody please help me with this. Solution link : https://mirror.codeforces.com/contest/1582/submission/133125904.
Although I am looping for j=1 to k and then calling memo(), but according to me, the total dp state calculation time is O(nk) only.
In problem D in the case of n being odd
isn't the sum of the array b going to be 1e9+1e4 which exceeds the required constraints?
Can someone please explain it to me?
TL;DR No because the max value for $$$n$$$ where $$$n$$$ is odd is $$$n=10^5-1$$$.
The worst case where $$$n$$$ is odd is $$$n=10^5-1$$$ and $$$a_i=10^4$$$ for all $$$i$$$. So our $$$b$$$ array has $$$10^5-2$$$ elements where $$$|b_i|=10^4$$$ and one where $$$|b_i|=2*10^4$$$. And the sum is $$$(10^5-2)*10^4+2*10^4=10^5*10^4=10^9$$$.
In editorial for G, can someone please explain what next_i is really calculating?
For problem C, the answer can probably be obtained by using either the first or the last character in the original string. There is no need to run the algo for all the 26 characters :p
Can anyone explain how is The sum |b1|+|b2|+…+|bn−3| does not exceed MAXA⋅(MAXN−1−3).
F1 can be done using segment tree + bitset optimization
Submission here: 155885705
could you please explain it in brief
Extremely simple $$$O(n + (max(a[i])^2 + max(a[i]).\log{max(a[i])})$$$ solution for F-2: link
(Basic idea is to consider all elements in increasing order of value, as then we dont have to care about ensuring subsequence is increasing, we just have to care about indices of consecutive elements in the subsequence.)