Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

### djm03178's blog

By djm03178, history, 19 months ago,

1304A — Two Rabbits

Tutorial
Solution

1304B — Longest Palindrome

Tutorial
Solution

1304C — Air Conditioner

Tutorial
Solution

1304D — Shortest and Longest LIS

Tutorial
Solution

1304E — 1-Trees and Queries

Tutorial
Solution

1304F1 — Animal Observation (easy version)

Tutorial
Solution

1304F2 — Animal Observation (hard version)

Tutorial
Solution: O(nmlogm)
Solution: O(nm)

• +205

 » 19 months ago, # |   +19 Interesting, I was just talking to a friend a few days ago about how I've never seen max-queue used in a Codeforces problem. The solution for F2 is nice.
•  » » 19 months ago, # ^ | ← Rev. 2 →   0 In fact,max-queue or min-queue is used in 372C.
 » 19 months ago, # |   +283 ✓ Nice problems✓ Clear statements✓ Fast editorialGreat effort mate, very nice contest!
•  » » 19 months ago, # ^ |   +21 ✓ Strong pretests (Although some pretest passed code of B failed system test)
•  » » » 19 months ago, # ^ |   +1 Yeah, I think it is generally true for many last contests where each test has many test cases.
•  » » » 19 months ago, # ^ |   +14 In F,I accidently type int suf[51][20005] to suf[21][20005] and I got pretests passed...... So maybe pretests in F is not that strong.
•  » » » » 19 months ago, # ^ |   0 I assume it didn't cause problems in most cases because pre came right after suf generally, and when it was about to read something like suf[50][0] it usually pointed at one of the earlier indices of pre, which isn't used anymore.
 » 19 months ago, # |   +14 Thanks for the quick tutorial <3 Thanks for the contest <3 Have some good days problem setters <3
 » 19 months ago, # | ← Rev. 2 →   0 Editorial of B: Instead of "pick any one of them and put it in the middle.", pick the longest one.
•  » » 19 months ago, # ^ |   +11 The length of the strings are equal, so you can just pick any of them.
•  » » » 19 months ago, # ^ |   0 ohh, I totally forgot that, my bad :)
•  » » » » 19 months ago, # ^ |   0 I too did the thing for longest string XD
•  » » 19 months ago, # ^ |   -8 no need of the longest one because all are same length
 » 19 months ago, # |   +2 So I guess, that I found the best way to have good score in contest.You should test the round, and the Power From MikeMirzayanov will come to you.Thanks MikeMirzayanovAnd thank you for the great contest. I really enjoyed problems.
 » 19 months ago, # |   +5 Can someone explain why this greedy method for problem D is correct? 71148664(topological sort with heap/priority_queue)
•  » » 19 months ago, # ^ | ← Rev. 4 →   0 In fact, you even don't need a heap. Each string like "<>>" "<>>>" can only provide one in the LIS.
•  » » 18 months ago, # ^ | ← Rev. 2 →   0 You can prove the correctness of topo-sort method by observing that the output (order of numbers) you get is the same as the output of editorialist's algo.
 » 19 months ago, # | ← Rev. 3 →   +14 I think i have an easier to code solution to D using two pointers and reverse() function in C++.Here's my submission.I can explain it if you want but it's easy to understand I guess.
•  » » 19 months ago, # ^ |   0 Hey, Can you please explain your approach?
•  » » » 19 months ago, # ^ | ← Rev. 4 →   +22 OK.It can be shown that the minimum LIS cant be less than the length of the longest segments containing only '<'.Now consider the array n,n-1,n-2,...,2,1. Now for every segment of '<' reverse the numbers in that segments for example">><<<>>>"9 8 4 5 6 7 3 2 1You can see that it acquires the minimum LIS.For the maximum LIS it can be shown that the LIS can't be longer than the number of '<' + 1.Now consider the array 1,2,3,...,n-1,n. Now for each segment of '>' reverse the numbers of that segment. For example">><<>><"3 2 1 4 7 6 5 8and this approach requires the maximum LIS.I hope mt explanations were clear.
•  » » » » 19 months ago, # ^ |   +3 LCA?? Did you mean LIS?
•  » » » » » 19 months ago, # ^ | ← Rev. 2 →   0 YES!!SORRY after thinking on problem E LCA was stuck in my head XDSorry it was a mistake. I fixed it now.
•  » » » » 19 months ago, # ^ |   +3 Thanks !! Nice Approach.
•  » » » » 19 months ago, # ^ |   +3 Shouldn't the answer in example for maximum (<<>><) be 1 2 5 4 3 6 7 8. By the way your approach is really good.
•  » » » » » 19 months ago, # ^ |   0 Fixed it.I should have put "<" and ">" string between "" to make it look correct in comment. Thank you.
•  » » » » 19 months ago, # ^ |   0 It's really a nice solution!Thx
•  » » » » 19 months ago, # ^ |   0 Can you please share the intuition behind this method?
•  » » » » 19 months ago, # ^ |   0 Tanx very much.its lot clear.
•  » » 19 months ago, # ^ |   +3 Nice approach.
•  » » 19 months ago, # ^ |   0 Dude really nice solution :-)
•  » » 15 months ago, # ^ |   0 EMEJ Thanks bro really great solution
 » 19 months ago, # | ← Rev. 2 →   0 For D to make LIS exactly k I think, the k must be possible to be represented by sum of length of increasing consecutive groups + 1 and be more than length of max group Example: >>><<>>><>>><><< we have 4 groups << < < <<so k can be 3 (our minimum, because it is maximal length of single increasing group +1) k, can also be 4 and 5 and 6,7 for 3: I first fill rightmost group of size two, and others should be filled up from right to left (to avoid random LIS):  << < < << 12 then I do  << < < << 56 4 3 12 and then on not filled space I follow in increasing order from right to left > > > < < > > > < > > > < > < < 17 16 15 5 6 14 13 12 4 11 10 9 3 8 1 2 7 so we have 5, 6, 14, or 1, 2, 7for k=4: I would do: > > > < < > > > < > > > < > < < 5 6 4 1 2 3 then fill up again: > > > < < > > > < > > > < > < < 17 16 15 5 6 14 13 12 4 11 10 9 1 8 2 3 7 and we have 1,2,3,7need more detailed analysis for how we can combine different groups, but for problem that was given in editorial, I was putting all together by filling up groups from left to right for longest LIS, and from right to left to shortest LIS. So I by offsetting start point we can get different k
 » 19 months ago, # |   0 Question on AOn 71161349 I got WA on test 3 and after I had changed long long into int I got accepted. I wasted 6 resubmissions and a lot of time during the contest thinking my Idea was wrong. This is the good one 71163490.Anyone know what's going on here?
•  » » 19 months ago, # ^ |   0 You can read the checker's comment: "wrong output format Expected integer, but "1.73633e+006" found"You used double so it can be printed in a different way other than pure integer, even if the value can be represented as an integer.
•  » » » 19 months ago, # ^ |   0 Thanks for the answerDoes this have to do with the fact that the statement asks for the output as an integer or is it something from the compiler/the way codeforces evaluates submissions?
•  » » » » 19 months ago, # ^ |   0 The checker used in this problem only accepts integers in integer form, and I think in most (if not all) cases when the problem says 'integer', you must print it in integer form.
•  » » 19 months ago, # ^ | ← Rev. 2 →   0 It's not that you changed long long to int, it's that you changed double to int as well. Your wrong answer shows you printed 1.73633e+006, which it doesn't think is an integer. Changing it to int makes it print correctly. Adding cout << setprecision(0) << fixed; should also do the trick (although you'd also have to add #include ).edit: this was already answered, but I didn't refresh it
•  » » » 19 months ago, # ^ |   0 Does this mean there is some default setprecision value even when the value of the double has no decimals?
•  » » » » 19 months ago, # ^ | ← Rev. 2 →   0 Yes, the default is 6 (source), and fixed-point notation will always print values with that many decimals, even integral values. However, this doesn't apply to 'normal' double output when you're working specifically with integral values.
•  » » » » » 19 months ago, # ^ |   0 If it does not apply to double that has an integral value then why did my program print 1.73633e+006, as the value of t can only be integral? (I check if the % is 0 and only then do the division)
•  » » » » » » 19 months ago, # ^ |   0 Fair, seems like it is limiting itself to 6 digits of precision, as it would rather print 1e6 than 1000000.
 » 19 months ago, # |   +1 In problem div 2 C how are we updating the value of mx and mn? Can anyone explain it please?
•  » » 19 months ago, # ^ |   0 Look mx and mn are nothing but maximum and minimum possible range we can go for every new visitor. so we are keeping track of the range with every old visitor and checking that if a new visitor can be satisfied i.e if his temperature range lies inside the old updated range(mx,mn).
•  » » » 19 months ago, # ^ |   0 i dont undestand these constraints mn−K≤R and L≤mx+K Shouldn't it be mn−K≤L and R≤mx+K, such that the customer's time is inclusive.Please help me understand the meaning of these constraints.
•  » » » » 18 months ago, # ^ |   0 If in any case minimum is greater than the upper limit of temperature or maximum is smaller than the lower limit, then it is not possible that the given desired range lies between our minimum and maximum and hence we cannot reach that temperature range.
 » 19 months ago, # |   0 Whats wrong with this code failing test 3 3rd token :https://codeforces.com/contest/1304/submission/71185623
•  » » 19 months ago, # ^ |   0 when i am running wrong token individually it is showing the correct output(jury output) but its still failing please checkout token 6 also .
•  » » 19 months ago, # ^ |   0 In case of "NO" ;you are not taking all t,l,h values. You have set ans=0 and came out of loop using break. Just remove the break statement.
•  » » » 19 months ago, # ^ |   0 if ans is zero that means there is some customer who can't be satisfied so why not to break .
•  » » » » 19 months ago, # ^ |   0 Even though you know that there is some customer who can't be satisfied, you should take the all the values, otherwise when your program proceeds on the next test case, instead of taking the correct inputs, your program will take the customers that can't be satisfied from the previous test case (because you didn't take it before) and fail.
 » 19 months ago, # | ← Rev. 3 →   0 There is another solution for F(maybe only a bit different).We don't need to calculate sum(animal[i][j…j+k−1]) for each j.First let us solve max(1,j-k+1)<=x<=j,then the other part can be solved using the same idea.we define P[i][j] as dp[i][j]-sum(animal[i+1][1....j-k+1])then dp[i][j] is updated by max(P[i-1][max(1,j-k+1).....j]+sum(animal[i…i+1][j…j+k−1])+sum[i-1][1...j-1]).Since sum(animal[i…i+1][j…j+k−1])+sum[i-1][1...j-1] is a constant for each We can find out the max value by simple RMQ algorithms on array P[i-1]
 » 19 months ago, # |   0 Nice Problemset and editorial. I really Enjoy the contest. Thanks to problem setter.djm03178
 » 19 months ago, # |   +5 I believe there's something wrong with the second question's answer as if it wants the longest palindromic string then for the case like5 2 oo ox xo xx xxThe answer can be "10 , xoxxooxxox" but the answer given by the above code is "6 , oxxxxo" which I don't believe is longest one . Even for the test case like3 2 xx xx xxThe output from the code is "2 , xx" and not "6 , xxxxxx" . If I'm wrong anywhere please let me know .
•  » » 19 months ago, # ^ |   0 The problem clearly states that there are n distinct strings of m length. So you can't take one string more than once.
•  » » » 19 months ago, # ^ |   0 thanks I got it
 » 19 months ago, # |   0 What's the problem wtih my code?https://codeforces.com/contest/1304/submission/71196899My method as similar sa the editorial.Just he use the arrays.Please tell me where my code is wrong.
•  » » 19 months ago, # ^ |   0 your t1 is not the time between two adj people, a data like 3 0 1 1 1 4 4 4 6 7 7 your ans is YES ,but NO is right .
•  » » » 19 months ago, # ^ |   0 Thanks fo you.You're pretty good.
 » 19 months ago, # |   0 Can somebody please tell me why I am getting Runtime Error on pretest 3 on this submission for problem C?There is an assert statement which verifies whether the visit time of customers are in non decreasing order. Runtime error seems to suggest that there is a test case in which visit time of customers are not in non decreasing order. When I removed the assert statement, I got WA. See this.And, when I myself sorted the customers according to their visit time, I got "Accepted" verdict. See this.
•  » » 19 months ago, # ^ |   +3 if you output NO and return ,the function is over,but your input didn't finish. i think its the problem.why not finish the read and then deal with the problem
•  » » » 19 months ago, # ^ |   0 Thanks for the response. I understood your point. My bad !
 » 19 months ago, # | ← Rev. 5 →   0 In problem E, The simple path from $a$ to $b$ without using the added edge. The simple path from $a$ to $x$ without using the added edge, plus the added edge, plus the simple path from $y$ to $b$ without using the added edge. The simple path from $a$ to $y$ without using the added edge, plus the added edge, plus the simple path from $x$ to $b$ without using the added edge. What is the difference between the second and the third case? For example, in the case below: 5 1 2 1 4 2 3 4 5 1 3 5 2 4 k We can see that the three paths are as follow: $2 \to 1 \to 4$ $2 \to 3 \to 5 \to 4$ $2 \to 1 \to 4 \to 5 \to 3 \to 2 \to 1 \to 4$ But in this path, noticing that the edge (1, 2) and (1, 4) are used twice, we can ignore the influence they bring to the parity of the total length, thus the path becomes： $2 \to 3 \to 5 \to 4$ And the second and the third path become the same. So what's the meaning of considering the third case? Or did I forget to consider some other situation?
•  » » 19 months ago, # ^ |   0 The third path became useless because you 'noticed' that they have duplicated edges. You don't need to check if they have such edges if you just calculate both the second and the third path.
•  » » » 19 months ago, # ^ | ← Rev. 3 →   0 [deleted]
 » 19 months ago, # | ← Rev. 2 →   0 Can anyone please look into My code for Problem C. My logic is similar to the editorial but it shows WA for the 32nd token of test 3.
 » 19 months ago, # |   -8 In B,I got some penalties.Because of my bad Wifi and terrible English!
 » 19 months ago, # |   0 71201513 Could someone please debug the run-time error I'm getting on this solution? Apparently the error happens in the last loop, where the assert condition fails. for(i=store.size()-1; i>=0; i--){ assert(i>=0 && i=0; i--){ if(i<0 || i>=store.size()) { cout<<"OOH!: "<
•  » » 19 months ago, # ^ |   0 i is declared to be long long
•  » » » 19 months ago, # ^ |   +2 size() returns an unsigned integer, so if the value is 0u then subtracting 1 from it will make it overflow.
•  » » » » 19 months ago, # ^ |   0 Thanks a lot!
 » 19 months ago, # |   0 for problem D. How to find a sequence for fixed length k LIS?
 » 19 months ago, # |   0 My solution to the problem E is the same as author's, even my implementation is quite similar to his but I have repeatedly got TLE on test 2. I've generated lots of huge testcase to check but it runs all the way fine, no probs. And test 2 is a small test, so I tried to generate as similar to the testcase as possible but no issues found so I desperately ask for your help on my case: https://codeforces.com/contest/1304/submission/71262563 I'd be very thankful.
•  » » 19 months ago, # ^ |   0 __builtin_clz(0) returns an undefined value (it is well-defined for non-zero arguments). Thus, when H[u] equals to 0 (in this case, u == 1) something unpredictable might happen under the hood.
•  » » » 19 months ago, # ^ |   0 Oh damn I didn't even notice the case when I started using this function instead of log2(). Thank you so much for your help ^^
 » 19 months ago, # |   0 I'm not understanding why I'm getting WA in Problem C My Submission. Can anyone help?
•  » » 19 months ago, # ^ |   0 You break out of for loop where input is read. So you don't read the entire input for a test case!
•  » » » 19 months ago, # ^ |   0 Thanks a lot nweeks! I didn't consider the scenario.
 » 19 months ago, # |   0 In problem E, we need to determine the root to use LCA. But as there is no info about that and all the edges are bidirectional, how are we going to determine the root? Sorry to bother you.
•  » » 19 months ago, # ^ | ← Rev. 2 →   0 The LCA is just a way to find distances easily, the root you choose does not change the distance, so you can pick any root you want.
•  » » » 19 months ago, # ^ |   0 Oh, I see! Thanks a lot.
 » 19 months ago, # |   0 In the solution of Div 2 : CMy Accepted SolutionRuntime ErrorOn declaring an if condition in loop I am getting error and if I declare it outside the loop it is accepted. Idk why is that happening. Kindly help. Thanks in advance :)
•  » » 19 months ago, # ^ |   0 Got the error. When I return the solve() function before all inputs of a particular TC then next input of that TC will be stored in initial variables i.e n, m and so on. So silly of me.
 » 19 months ago, # |   0 thanks, problemsetters, nice contest
»
19 months ago, # |
0

can you fix the time complexity to pass in all the test cases.

# include<bits/stdc++.h>

typedef long long ll; using namespace std;

int main(){ int t; cin>>t; while(t--){ int x,y,a,b,count=0; cin>>x>>y>>a>>b;

while(x!=y && x<y){
x+=a;
y-=b;
count++;
if(x>y){
count=-1;
}
}
cout<<count<<endl;
}

}

 » 19 months ago, # |   0 In problem C, can someone explain how the condition mn-k <= R and mx+k >= L is working?? Thanks in advance :)
•  » » 19 months ago, # ^ |   0 That condition in other words is "there is at least one intersected temperature between the range of their preferred temperature and the range of available temperature."
•  » » » 14 months ago, # ^ |   0 can you please explain about updating mn and mx
•  » » » » 14 months ago, # ^ |   0 You want to maximize the range. In other words, you want to minimize mn and maximize mx, respectively. So, you want to set mn as mn — K and mx as mx + K, but L and R might not be in this range. This means that mn cannot be smaller than L and mx cannot be larger than R. Conclusively, mn should become max(mn — K, L) and mx should become min(mx + K, R).
•  » » » » » 14 months ago, # ^ |   0 Got it.Thank you.
 » 19 months ago, # |   -27 for solution of (B) — Longest Palindrome: input: 3 3 aba aba aba  given output: 3 aba but, it should be 9 abaabaaba 
•  » » 19 months ago, # ^ | ← Rev. 2 →   +8 It says all strings are distinct, so it's not a valid case.
 » 19 months ago, # |   -21 Hi, your tutorial is very good but i have one doubt with the solution of B(Longest palindrome). for example if my input is : input: 3 3 tat cat tat output: tat i think right answer is : tattat
•  » » 19 months ago, # ^ |   +24 Please, read my comment right above yours.
 » 18 months ago, # | ← Rev. 2 →   0 Please anyone can help me to figure out what wrong with my submission. I struggle in many hours on test case 8, problem E.
 » 17 months ago, # |   0 I've managed to understand E (1-Trees and Queries)'s solution with this helpful blog entry on LCA (https://codeforces.com/blog/entry/22325). However, I can't see why we add 2 to the final length computed by lca_len. Can someone explain?
•  » » 16 months ago, # ^ |   0 Did you get it now? I am still not able to get it.
•  » » » 16 months ago, # ^ |   0 Hey @Gagandeep98, I solved my question with help from the author himself (@djm03178).Here is his answer: "Basically, if you reach there, a and b are not the same; instead, par[a][0] and par[b][0] are the same. This means both a and b are direct children of another node. Therefore, they need to move two more edges to meet each other."
•  » » » » 16 months ago, # ^ |   0 Thanks bro. Got it now
 » 14 months ago, # | ← Rev. 3 →   0 Honestly, for Problem Dis comment ne bacha liye, Editorial me to chutiya kaat diya.English Translation:For Problem D, this comment saved my ass. Editorial is really messy and not clear.P.S.: If anyone got Editorial's explanation for Problem D, Please Explain. (Also what is perm[] array in editorial ?! :/)
•  » » 14 months ago, # ^ |   0 I don't know which part you think is messy. The comment you linked is identical to the editorial, even the explanation's logic and the solution's outputs are the same. Maybe the comment's implementation is a little easier.Anyways, perm means permutation sequence the problem is asking you to find. perm[i] is the i-th number of the answer.
»
8 months ago, # |
Rev. 2   0

I was trying B...and i got errors, so i saw editorial and understood my mistake..while i was searching for a better approach in the submissions section(98779014), I came through this code.I noticed that we did not insert any string in the set...yet we searched if the string is present in the set...could someone please explain me why?

# include<bits/stdc++.h>

using namespace std; int main() { set a; int n,m; cin>>n>>m; string s,t,x,mid,ans=""; while(n--) { cin>>s; t=s; reverse(t.begin(),t.end()); if(s==t) mid=t; else if(a.count(t)) ans+=t; a.insert(s); } x=ans; reverse(x.begin(),x.end()); ans+=mid+x; cout<<ans.size()<<"\n"<<ans;

}

This is my first comment, sorry if I was dumb ;-;. I found it myself...Thankyou

 » 6 months ago, # |   0 can someone please help me debug the error in the code to the problem E 1-trees and queries https://codeforces.com/problemset/submission/1304/109939587 here is the link to the submission
•  » » 6 months ago, # ^ |   0 so I corrected the error and the correct link to the submission is here https://codeforces.com/contest/1304/submission/109943734