All ideas belong to MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int n;
cin >> n;
vector<int> a(n);
int pos = -1;
for (int j = 0; j < n; ++j) {
cin >> a[j];
if (a[j] == 1) pos = j;
}
bool okl = true, okr = true;
for (int j = 1; j < n; ++j) {
okl &= (a[(pos - j + n) % n] == j + 1);
okr &= (a[(pos + j + n) % n] == j + 1);
}
if (okl || okr) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int n;
cin >> n;
vector<int> a(4 * n);
for (int j = 0; j < 4 * n; ++j) {
cin >> a[j];
}
sort(a.begin(), a.end());
int area = a[0] * a.back();
bool ok = true;
for (int i = 0; i < n; ++i) {
int lf = i * 2, rg = 4 * n - (i * 2) - 1;
if (a[lf] != a[lf + 1] || a[rg] != a[rg - 1] || a[lf] * 1ll * a[rg] != area) {
ok = false;
}
}
if (ok) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
long long g = 0;
for (int i = 0; i < n; ++i) {
long long x;
cin >> x;
g = __gcd(g, x);
}
int ans = 0;
for (int i = 1; i * 1ll * i <= g; ++i) {
if (g % i == 0) {
++ans;
if (i != g / i) {
++ans;
}
}
}
cout << ans << endl;
return 0;
}
1203D1 - Remove the Substring (easy version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
string s, t;
cin >> s >> t;
int ans = 0;
for (int i = 0; i < int(s.size()); ++i) {
for (int j = i; j < int(s.size()); ++j) {
int pos = 0;
for (int p = 0; p < int(s.size()); ++p) {
if (i <= p && p <= j) continue;
if (pos < int(t.size()) && t[pos] == s[p]) ++pos;
}
if (pos == int(t.size())) ans = max(ans, j - i + 1);
}
}
cout << ans << endl;
return 0;
}
1203D2 - Remove the Substring (hard version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
string s, t;
cin >> s >> t;
vector<int> rg(t.size());
for (int i = int(t.size()) - 1; i >= 0; --i) {
int pos = int(s.size()) - 1;
if (i + 1 < int(t.size())) pos = rg[i + 1] - 1;
while (s[pos] != t[i]) --pos;
rg[i] = pos;
}
int ans = 0;
int pos = 0;
for (int i = 0; i < int(s.size()); ++i) {
int rpos = int(s.size()) - 1;
if (pos < int(t.size())) rpos = rg[pos] - 1;
ans = max(ans, rpos - i + 1);
if (pos < int(t.size()) && t[pos] == s[i]) ++pos;
}
cout << ans << endl;
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a.rbegin(), a.rend());
int lst = a[0] + 2;
int ans = 0;
for (int i = 0; i < n; ++i) {
int cur = -1;
for (int dx = 1; dx >= -1; --dx) {
if (a[i] + dx > 0 && a[i] + dx < lst) {
cur = a[i] + dx;
break;
}
}
if (cur == -1) continue;
++ans;
lst = cur;
}
cout << ans << endl;
}
1203F1 - Complete the Projects (easy version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
bool comp(const pair<int, int>& a, const pair<int, int>& b) {
return a.first + a.second > b.first + b.second;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, r;
cin >> n >> r;
vector<pair<int, int>> pos, neg;
for (int i = 0; i < n; ++i) {
pair<int, int> cur;
cin >> cur.first >> cur.second;
if (cur.second >= 0) pos.push_back(cur);
else {
cur.first = max(cur.first, abs(cur.second));
neg.push_back(cur);
}
}
sort(pos.begin(), pos.end());
sort(neg.begin(), neg.end(), comp);
int taken = 0;
for (int i = 0; i < int(pos.size()); ++i) {
if (r >= pos[i].first) {
r += pos[i].second;
++taken;
}
}
vector<vector<int>> dp(neg.size() + 1, vector<int>(r + 1, 0));
dp[0][r] = taken;
for (int i = 0; i < int(neg.size()); ++i) {
for (int cr = 0; cr <= r; ++cr) {
if (cr >= neg[i].first && cr + neg[i].second >= 0) {
dp[i + 1][cr + neg[i].second] = max(dp[i + 1][cr + neg[i].second], dp[i][cr] + 1);
}
dp[i + 1][cr] = max(dp[i + 1][cr], dp[i][cr]);
}
}
int ans = 0;
for (int cr = 0; cr <= r; ++cr) ans = max(ans, dp[int(neg.size())][cr]);
cout << (ans == n ? "YES" : "NO") << endl;
return 0;
}
1203F2 - Complete the Projects (hard version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
bool comp(const pair<int, int>& a, const pair<int, int>& b) {
return a.first + a.second > b.first + b.second;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, r;
cin >> n >> r;
vector<pair<int, int>> pos, neg;
for (int i = 0; i < n; ++i) {
pair<int, int> cur;
cin >> cur.first >> cur.second;
if (cur.second >= 0) pos.push_back(cur);
else {
cur.first = max(cur.first, abs(cur.second));
neg.push_back(cur);
}
}
sort(pos.begin(), pos.end());
sort(neg.begin(), neg.end(), comp);
int taken = 0;
for (int i = 0; i < int(pos.size()); ++i) {
if (r >= pos[i].first) {
r += pos[i].second;
++taken;
}
}
vector<vector<int>> dp(neg.size() + 1, vector<int>(r + 1, 0));
dp[0][r] = taken;
for (int i = 0; i < int(neg.size()); ++i) {
for (int cr = 0; cr <= r; ++cr) {
if (cr >= neg[i].first && cr + neg[i].second >= 0) {
dp[i + 1][cr + neg[i].second] = max(dp[i + 1][cr + neg[i].second], dp[i][cr] + 1);
}
dp[i + 1][cr] = max(dp[i + 1][cr], dp[i][cr]);
}
}
int ans = 0;
for (int cr = 0; cr <= r; ++cr) ans = max(ans, dp[int(neg.size())][cr]);
cout << ans << endl;
return 0;
}
Thanks for the great editorial ! The tutorial for the problem F2 seems to be unavailable for me. I get the following error "Unable to parse markup [type=CF_MATHJAX]".
vovuh Can you check the editorial for F2, thanks
Has the problem been fixed yet? It works fine on my device.
Yes ! It's fixed now.
Thanks for fast editorial
Can someone elaborate how to handle case when b_i is negative in problem F. Why are we setting a_i = max(a_i,-b_i). Why sorting in the order of a_i+b_i works?
a_i = max(a_i, -b_i) — u delete variants, when yours current rating plus b_i < 0. For example: r = 10, a_0 = 9, b_0 = -20. U can take this project, but yours rating will be negative. And I don’t know, why the sort a_i + b_i works.
First, let me tell you why we are setting ai = max(ai, -bi):let's talk both the situations: ai >= -bi and ai < -bi , if ai >= -bi, that means ai doesn't change, when ai < -bi, the problem let us ensure r > 0, when we do this project i, r will be r + bi(bi if negative), so it's obviously why we let ai be max(ai, -bi), we must ensure r+bi > 0, that means we let ai = -bi(the case 2), we can decrease the judgement.Second, why sorting in the order of ai + bi can work? This problem trouble me for a long time at first, when we let ai be max(ai, -bi), we can ensure ai >= -bi, that means ai + bi is not negative, but it can be zero, if we can't do project i, for j (aj + bj < ai + bi) we must can't do all of this, too.That is because aj is greater than ai or not, if aj >= bi , we can't do project j, if aj is smaller than ai, and aj + bj < ai + bi, we can't do project i that means r < ai, when we do the left thing, r will be smaller, that means we can't do project i, either. So this can work.
I think that you should think about this array like “how much rating I need, to make i-th order”.
Let's try to determine the optimal order to complete all the projects.
Let's first focus on two adjacent projects $$$i$$$,$$$j$$$ in the current arrangement (supposing $$$j=i+1$$$), and see when we should swap them.(Obviously, the swap will not affect other projects.) That's to say, we should see which way will make completing both projects "easier".
$$$I.$$$ If we can take both of them by taking $$$i$$$ first, that means our initial rating $$$r$$$ satisfies both
Unable to parse markup [type=CF_MATHJAX]
&Unable to parse markup [type=CF_MATHJAX]
, which is equivalent to $$$r \ge max(a_i , a_j - b_i)$$$.$$$II.$$$ If we can to take both of them by taking $$$j$$$ first, that means our initial rating $$$r$$$ satisfies both $$$r \ge a_j$$$ & $$$r + b_j \ge a_i$$$, which is equivalent to $$$r \ge max(a_j , a_i - b_j)$$$.
We wonder which constraint is weaker. We already have $$$a_j - b_i > a_j , a_i - b_j > a_i$$$, so we just need to compare $$$I.$$$ $$$a_j - b_i$$$ and $$$II.$$$ $$$a_i - b_j$$$. The smaller, the weaker. Note that $$$I. < II.$$$ <=> $$$a_j - b_i < a_i - b_j$$$ <=> $$$a_i + b_i > a_j + b_j$$$. So, for every pair of adjacent projects, we should determine whether to swap them or not by the value $$$a_i + b_i$$$. In other words, we have the sequence sorted in the order of $$$a_i + b_i$$$.
(I hope thinking this way can inspire and help you.)
(Thank Bekh for better formatting! I'm new to writing comments, but I'll keep trying.)
(Looking forward to others' better, shorter, clearer answers!)
And an amazing thing that I sort the projects with negative b[i] use this also pass.
Complete code https://codeforces.com/contest/1203/submission/58799244
Assume there are two projects:$$$X$$$,$$$Y$$$, with $$$a_x+b_x>a_y+b_y$$$, and our remaining rating is $$$r$$$. If we can't complete $$$Y$$$ after finishing $$$X$$$, that means $$$r+b_x<a_y$$$. Using $$$a_x+b_x>a_y+b_y$$$ and $$$r+b_x<a_y$$$ we can conclude that $$$r+b_y<a_x$$$. This means we can't change the order so as to finish both the projects.
For problem F2,I have a solution in complexity: $$$O(n^2)$$$ .
My solution is similar to the writer's.The only difference is that I let $$$dp[i][j]$$$ be after you choose j in the first i tasks , the maximum rating you have.Then we can simply solve this problem using dynamic programming and the complexity is $$$O(n^2)$$$ .
Here is my code.
Sorry for my poor English.
I also thought of that optimization; code to compare
Can you please elaborate? From your definition of dp state, if you initialise dp[0][0] as rating obtained from the positive part, the dp won't be updated as maximum rating possible is the case where you do not take any negative rating change elements.
It can be updated for sure.
We can do these transitions :
$$$dp[i][j]=dp[i-1][j]$$$
and if $$$j>=1$$$ && $$$dp[i-1][j-1]>=a_i$$$ && $$$dp[i-1][j-1]+b_i>=0$$$ then $$$dp[i][j]=max(dp[i][j],dp[i-1][j-1]+b_i)$$$
At last,we just need to find the maximum number $$$i$$$ that satisfy $$$dp[n][i]>=0$$$
Thanks for the elaboration. I understand it now. Dp[i][j] is the max rating you can have considering first i tasks and must taking j of them. Once you force to take j tasks the updates become clear.
Can anyone please help me to understand the problem E? I dont understand the problem for a while. It would be really helpfull. Thanks in advance. :) sorry for my poor english.
In simple words we have to maximize number of distinct elements in an array either by keeping the element as it is or increasing it by 1 or decreasing it by 1.
Thanks for repeating the question
Thanks a lot understood. :)
Can you please check my submission 58732857 for problem C. It is the same as editorial but it timed out. Is using python such a big problem ?
You can see many participants use Python with AC
Your code looks fine..I guess it might be due to python not sure
Hello. I got AC with ur code when use Python 3. https://codeforces.com/contest/1203/submission/58817204
Try using PyPy3, which is an alternative implementation of Python3. It generally runs much faster.
I actually submitted on PyPy3 and it still errored out but it is getting accepted in Python 3. This doesn't seem right.
There are some changes in the "fractions" module implementation of PyPy3. It's slower. Try the same solution, but import gcd from "math" module rather than "fractions".
fractions.gcd() is deprecated since version 3.5. Use math.gcd()
why the problem F2 Tutorial is
Unable to parse markup [type=CF_MATHJAX]
I use a different cmp function for sort in problem f1: look at my cmp2 in the solution https://codeforces.com/contest/1203/submission/58799244
why do we sort by a + b in decreasing order for negative value in F1
because we want to do a project with high a and low b first before other project
I've tried to explain main idea behind it there
Can someone help me with problem D1 code. Where am i going wrong logically? It's giving me WA on test case 5.
We are in the same boat friend :( Can someone plzz elaborate what to do for passing testcase 5.
Try out this example test: s="caab" t="ab".
Your code matches first "a" (c a a b) and your answer is 1 (either remove "c" or remove "a").
The right answer should be 2 because you could remove "ca" subsegment (c a a b).
I hope this will help.
Your code only takes into consideration the case of matching s2 with s1 as early as possible. You also have to this from the back side of the string s2. than you will have to consider all the possible breaking point of the string s2(break s2 into two parts) find the max difference(no. of characters between them).Here is my code 58800190. I hope this will clear your doubt.
Another interesting example is "aabb" and "ab".
Your code matches as ( a a b b ) and returns 1.
It is possible to match as ( a a b b ) and the answer is 2.
Hi, my code does give the right output for both of your examples, still I am getting WA on test 5.
Code
Hello, I tested your code. It does actually fail on "aabb ab" test case (your answer is 1, expected is 2).
Thanks for the help, yes it does not give the right answer.
I am getting wrong answer on test case 5 but I think it should work correctly. I have used KMP for pattern searching. Can anybody help please?
Do your tester program check if participant find solution though jury can't find it?58820595
There is just comparator and my solution find order though author's answer is NO.
I could check it by myself but I can't see full test case. Where is my mistake?
P.S. Found mistake. Too late to delete this comment.
P.P.S. 58821318 Maybe not found. At each step I checking if r >= ai and r >= 0 after each step. So I think that in test #11 I found correct order to take all projects.
P.P.P.S. Sorry now I really found mistake.
i can't prove gcd(...(gcd(gcd(a[1],a[2]),a[3],)...)a[n])=gcd(a[1],a[2],...a[n]) in problem C
We have $$$a, b, c$$$ let's denote $$$gcd(a, b, c) = g$$$ and $$$gcd(a, b) = z$$$ for convenience
We want to prove that $$$gcd(gcd(a, b), c) = gcd(a, b, c)$$$
After substituting some terms you get $$$gcd(z, c) = g$$$
$$$x|y$$$ reads x divides y and means $$$y\%x = 0$$$ or in english $$$x$$$ is a divisor of $$$y$$$
$$$g|z$$$ and $$$z|a$$$ then $$$g|a$$$
Same way $$$g|z$$$ and $$$z|b$$$ then $$$g|b$$$
and $$$g|c$$$ is true anyway so $$$g|a$$$ and $$$g|b$$$ and $$$g|c$$$ are true
That's for the divisibility. How do you know that it's the greatest?
Assume $$$gcd(a, b, c) = g$$$ and $$$g > z$$$. Since $$$g|a$$$ and $$$g|b$$$. $$$gcd(a, b)$$$ would've given $$$g$$$ not $$$z$$$ which is the greater divisor.
In other words $$$gcd(a, b, c)$$$ is either gonna be $$$gcd(a, b)$$$ or one of its divisors.
and you can keep doing the same to generalize to more then three numbers... prove $$$gcd(a, b, c, d) = gcd(gcd(a, b), c, d) = gcd(z, c, d) = gcd(gcd(z, c), d) ...$$$ and so on.
If something is not clear, please ask.
There are more formal proofs out there on the internet too.
thank you, i will more research
Task F, isn`t unique: https://acmp.ru/index.asp?main=task&id_task=572
How would one go about solving the task u mentioned? I mean the constraints are high, and creating dp array for knapsack would go out of memory.
Two ideas:
In the code in the editorial the first dimension of dp is neg.size() + 1. This simplifies the code, but actually it only uses dp[i] to calculate dp[i+1], so one only ever needs to store two dp rows at a time.
The rows of the dp array will have many repeated values, one might be able to reduce storage by just storing the start and end indexes of each group of repeated values.
Note that I have not tried writing code using either of these ideas.
Can anyone tell me why problem B,s case 2 output is YES and why case -3 output is NO. Thanks in advance. :)
T.C — 2 : -> 10 5 2 10 1 1 2 5
->R1 — sides a1 = 10,b1 = 1 and there is extra 10,1 also possible for opposite sides. ->R2 — sides a2 = 5, b2 = 2 and similar here
as a1.b1 == a2.b2 .Hence Yes.
T.C — 3:
-> 10 5 1 10 5 1 1 1
-> R1 — sides a1 = 10,b1 = 1 and there is extra 10,1 also possible for opposite sides. ->R2 — sides a2 = 5, b2 = 1 and similar here
as a1.b1 != a2.b2 .Hence No.
I have another solution for problem F1(cannot contribute to problem F2, but has a more obvious correctness). First, deal with the projects with positive b values greedily.
Then, calculate the rating after all projects are completed. If it's negative, terminate the program. Otherwise, consider to process the projects in a reversed order, beginning with the calculated "final" rating R. Each time we choose a project i such that R — b_i >= a_i, and update R by R — b_i. So the rating R will be increasing during the reversed process. Then it's easy to determine if it's possible to complete all the tasks.
Very easy to understand the "obvious" correctness as you stated.
Thanks.
Upd:this solution can contribute to F2 in fact. We focus on the projects with negative b values only.
Assume we have finished processed all the projects we selected with a remaining final rating R0. Then consider to process the projects in a reversed order with R0 as the initial rating. We can process a project i iff R-b_i >= a_i, so we can store these available projects in a priority_queue and one by one greedily choose the project with the smallest -b_i(rating change) to process, thus yielding the minimum initial rating required to finish a certain number of projects with final rating R0. It's easy to maintain the available projects with the rating increasing in O(n), and priority_queue is O(nlogn). The final rating R0 ranges from 0 to the biggest a_i+b_i (a larger R0 doesn't make sense since this is sufficient for us to choose the projects freely at the reversed "beginning"), a O(r) range.
The above process doesn't require the real initial rating r, so with this approach we can also answer queries about multiple initial ratings efficiently.
Overall Complexity(single query): O(nrlogn)
Overall Complexity(multiple query): O(nrlogn + nq) (q:number of queries)
Woah. This is just amazing and doesn't even require a proof because it is just so obvious.
orz xk
Hi, I tried solving the question D1, but I was getting WA on test 5. Editorial also suggest the use of brute force. I am not able to figure out if I have missed some boundary condition.
In short, what am I doing: I am calculating all possible indexes for a substring in the main string. After getting all possible indexes, I am just finding the case which removes the most characters from the main string.
The code:
Even mine solution was same using bruteforce and got WA on tc-5. Can't figure out why.
https://codeforces.com/contest/1203/submission/58756135
Hey, I fixed my program. My program was not checking for cases in which the largest substring to be dropped lies between the characters itself.
check out the comment by mivael.
Hi, I just want to know what 1ll in the solution of the 1st problem ?
1ll is the number 1 but of type long long not int. It is used to avoid overflow. because the result of the multiplication will be of type int since the array a is an array of int, and therefore it could overflow. Using 1ll makes the result of the multiplication of type long long rather than int.
Why not cast?
Thank you !
Proof for F1: Take array of the projects with negative $$$b_i$$$ (sorted as given in the tutorial). Let’s complete the projects in this order. If we get stuck at index $$$k$$$ , then:
Let’s assume we have a good order of projects in range $$$[0,k]$$$ so that all of them can be completed. Let the project completed last of this set , the one with index $$$p$$$. ($$$p ≠ k$$$ because of the starting inequality).
$$$a_p+b_p ≥a_k+b_k $$$ ,so $$$ a_p+b_p-b_k≥a_k$$$ (because we ordered them so).
Then after we complete everything except the $$$p^{th}:$$$ $$$r+\sum_{i<k}b_i-b_p+b_k ≥a_p$$$.
After reorder: $$$r+\sum_{i<k}b_i ≥a_p+b_p-b_k≥a_k$$$. That is in contradiction to the one in the beginning.
In problem B i stored the number and occurrence in a map . If the occurrence of any number is odd then this Case would be "NO" else i check from i from 1 to n that a[i]*a[n] and then i++ and n-- . if all the area is equal then it's "YES" . I don't know what is problem it's getting wa. this is my submission 58846470 And sorry for bad english :)
Try out this test case [1,2,2,2,3,3,3,6]. Your algo would return yes but answer is no ans there are no 2 1s or 2 6s to make a rectangle. Just wnsure that the value occurs in pairs. It should pass. Hope it helps!
Thanks ,It helps :)
Thanks,I used it too.
In B: I get a WA on test case 8 here is my code
can someone please elaborates how to do :(
At least your condition (n!=1) is problem
Try (it is impossible to create rectangle)
1
1
1 2 3 4
Thanks,that helps a lot :)
For problem B, I tought that if we sort the array and check that if for every i (1 to (n*2)-1) (i use v[0]*v[n-1] for checking) is equal to v[0]*v[n-1]. Where is my wrong? I couldn't find it. Please help. (I got wrong in 8th test.)
You should check, whether the number of sticks of the same length is even, and in for(ll i=1;i<(n/2)-1;i++) , you should write i<=(n/2)-1, because you would miss a pair. This way, your solution works.
Thanks,it worked!
first, you have to check whether it is posible to create a rectangle or not. your algorithm will not work for following test case: 1 1 2 5 10
Can anyone tell me why my java solution for problem c exceeding time limit?
int overflow in this loop:
eventually i*i will overflow before getting to c
Can someone give me an intuition for the D2 problem? Why are we creating a prefix and a suffix array? What is the purpose of that?
you can imagine that for each letter "c" in the second array, you create the first[c] and the last[c] in the first array in the sense that A[last[c],lA] contains B[c..LB]. Then, you will try to find the largest interval [p,q] such that p>first[c] or q<last[c] for all c in the second array.
why is sorting by $$$a_i + b_i$$$ is optimal for negative projects in F1?
Hey. I have a problem with problem C : https://codeforces.com/contest/1203/submission/58879853
https://codeforces.com/contest/1203/submission/58879873
I get time limit. I've seen that i have the same idea with some users and also with the tutorial. i have seen some codes and I don't find the mistake. Cand you help me?
try to use "long long " replaces "int "
Thank you!This solved the problem :)
Regarding problem C solution provided in the editorial ;
please consider the expression/condition in the following IF block(the following is as given in the solution);
i.e
if (i != g / i) { ++ans; }
.Now when I tried using
if ((i*i) != g ) { ++ans; }
in my solution ; I received a Wrong Answer Verdict.Can someone please help me out where am i going wrong.?
because
if ((i*i) != g ) { ++ans; }
could be overflow becausei
is integer, change it toif ((i*1ll*i) != g ) { ++ans; }
and you will get ACOh Thank You! It worked :)
Can someone help find out why the hell am i getting runtime error,(problem F1) my submisssion https://codeforces.com/contest/1203/submission/58887999
Comparison function must have strict weak ordering. I was doing the same mistake. For more info read here : https://stackoverflow.com/questions/34446443/segmentation-fault-because-of-comparison-function
Got it to pass your code till test case 36. This is your submission with correction. Now you can work on to remove the bug from your implementation.
Thanks a ton brother
Can anyone please explain editorial of problem D2 ?
Let me try to explain my approach. Consider these examples first:
$$$t=bac$$$
$$$t=bac$$$
$$$t=bac$$$
Now we will do $$$ans=max(max,last[i]-first[i-1])$$$ for $$$i \in [1,|t|]$$$.
Link to Code
let s = baabca
for each character of t:
according to you answer is maximum distance between adjacent characters of t:
max_dist = last_occurence["a"] — first_occurence["b"]
(i.e. 6 — 1 = 5) which is wrong in this case, correct answer is 2
If you take last occurrence of $$$b$$$ as 4 then you won't be able to make $$$t$$$. The occurrences should be valid as well.
for s = "abcdefg"
t = "def"
I guess your code is handling all the cases, but your comment ??
What if the string is s= "_hdhsahks_" and our t= "_hhsa_" . Please Explain this test case with your algorithm.
Thankyou so much for your hint.
I have written a blog on the solution to this problem. Here's the link
I hope it is of some help :)
It helped me. :)
In D2 Input
Input
The first line of the input contains one string s consisting of at least 1 and at most $$$2 \times 10^5$$$ lowercase Latin letters.
The first line of the input contains one string t consisting of at least 1 and at most $$$2 \times 10^5$$$ lowercase Latin letters.
maybe the second " First " shouble be "Second" ?
Thanks for the great editorial!
Thank you very much!
can someone explain the 1203D1 solution clearly.
I am getting TLE on 1203D2. Can someone help me out?
My python code
Also, I cant understand the terminology used in the editorial. What does t[i;|t|] mean?
Q- D1 and D2 I am getting wrong answer on test case 5 but I think it should work correctly. I have used KMP for pattern searching. Can anybody help please?
Hey, your program is not checking for max substring to be dropped between the characters.
For example for input:
aabb
ab
output should be 2, but your program would give output as 1
finding ab in string: a a b b
Hey, thanks man you just saved my day.
In problem E, Can someone pls explain how the answer is 10 for below TC and not 8? Can the weight of a boxer be changed more than once? (it is not mentioned so in the question). 8 9 4 9 6 10 8 2 7 1 — 10 boxers' weights.
According to editorial, we have to first sort the array and then for each weight(w) in the array first try to take w-1 (if possible) then w, then w+1. In this particular TC after sorting we get:
weights given :1 2 4 6 7 8 8 9 10
weights taken :1 2 3 5 6 7 8 9 10
We can convert
8 9 4 9 6 10 8 2 7 1 to->
9 10 3 8 5 11 7 2 6 1
WHY NOT SIMPLY THIS IN PROBLEM "E" CAN SOMEBODY EXPLAIN EDITORIAL
the common divisors problem code is still saying that time limit exceeded on test case 3 here is the code below... if any one could find the solutions please tell me why its giving time limit exceeded..... thanks in advance .....
include <bits/stdc++.h>
using namespace std; long long int gcd(long long int a,long long int b) { if(a == b) return a; else if(a>b) return gcd(a-b,b); else if(b>a) return gcd(a,b-a); } int main() { long long int count = 0; long long int n; cin >> n; long long int a[n]; long long int i; for(i = 0;i<n;i++) cin >> a[i]; long long int GCD = a[0]; for(i = 0;i<n;i++) { GCD = gcd(GCD,a[i]); } for(i = 1;i*i<=GCD;i++) { if(GCD % i == 0) { count++; if(GCD/i != i) { count++; } } } cout << count << endl; return 0; }
Instead of going from gcd(a,b) to gcd(a-b,b), try gcd(a%b,b). It is the same thing, essentially, just faster, because if a is much larger than b, you can skip many a-b-b-b...-b by doing a%b.
Imagine if a = $$$10^{12}$$$ and b = $$$1$$$, your gcd(a,b) will have to make $$$10^{12}$$$ recursive calls!
many many thanks to you!!!
Can someone explain me solution to D2 that How are the iterators i and pos working? I did the easy version but don't have a clue how to optimize it. The editorial seems harder than the problem itself
well what i did is
can the problem b be solved using xor??pls. help
Can someone explain why i need DP for F2 in the negative case. Am i not sorting greedily, so i can pick max elements only if i start from the beginning (in the sorted sequence) and take as much as i can . Why do i need DP here if someone could explain with example . Thanks :)
I can't understand why my solution is giving wrong answer for D2(testcase 6).
Can someone help me?
Someone could explain why problem B actually work with the editorial solution?
What is the inner
if
check for in problem C? __ if (g % i == 0) { _ ++ans;_ _ if (i != g / i) {_ _ ++ans;_ _ }_ _ }_F2 is solvable in O(N log N) as it's equivalent to the deadlines and durations scheduling problem: https://cp-algorithms.com/schedules/schedule-with-completion-duration.html
Code: https://codeforces.com/contest/1203/submission/60304846
In problem E in test case 3:
10 8 9 4 9 6 10 8 2 7 1 why the answer is 10? shouldn't it be 9?
Lets see the array in sorted order : 1 2 4 6 7 8 8 9 9 10 10
Um operations selection like this would conclude the answer to be 10 :
1 2 4 5(6 — 1) 6(7 — 1) 7(8 — 1) 8 9 10 11(10 + 1)
There maybe some other permutations for the same but hope this would clear ur query !
Problem E solution with simple explanation: https://www.youtube.com/watch?v=3untA4RsPxs
For problem D2,does anyone know how to implement using binary search, I,m having trouble finding solution for checking whether we can remove substring of length 'm' successfully.
I think that would cost O(n*n*log(n))....
first you will need to calculate two vectors pref and suff of length n each. pref[i] will denote the length of prefix of string t that is a subsequence of string s[0...i]._ similarly suff[i] will store the length of suffix of string t that is a subsequence of string s[i...n-1] (here n is the length of string s). now perform binary search on the answer low=0, high= n . to check if removing a substring of length x is possible or not iterate over i=0 to i= n-x. and if for any i, pref[i-1] + suff[i+x] >= length of t holds true. then it is possible to remove substring of length x starting from index i. if no such i found then x length substring cannot be removed. here is my submission 129840232 sorry for poor english.