djm03178's blog

By djm03178, history, 5 years ago, In English

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)
  • Vote: I like it
  • +205
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +19 Vote: I do not like it

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.

»
5 years ago, # |
  Vote: I like it +283 Vote: I do not like it

✓ Nice problems

✓ Clear statements

✓ Fast editorial

Great effort mate, very nice contest!

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +21 Vote: I do not like it
    • ✓ Strong pretests (Although some pretest passed code of B failed system test)
    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Yeah, I think it is generally true for many last contests where each test has many test cases.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +14 Vote: I do not like it

      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.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

»
5 years ago, # |
  Vote: I like it +14 Vote: I do not like it

Thanks for the quick tutorial <3 Thanks for the contest <3 Have some good days problem setters <3

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Editorial of B: Instead of "pick any one of them and put it in the middle.", pick the longest one.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    The length of the strings are equal, so you can just pick any of them.

»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

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 MikeMirzayanov

And thank you for the great contest. I really enjoyed problems.

»
5 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Can someone explain why this greedy method for problem D is correct? 71148664

(topological sort with heap/priority_queue)

  • »
    »
    5 years ago, # ^ |
    Rev. 4   Vote: I like it 0 Vote: I do not like it

    In fact, you even don't need a heap. Each string like "<>>" "<>>>" can only provide one in the LIS.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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.

»
5 years ago, # |
Rev. 3   Vote: I like it +14 Vote: I do not like it

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.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey, Can you please explain your approach?

    • »
      »
      »
      5 years ago, # ^ |
      Rev. 4   Vote: I like it +22 Vote: I do not like it

      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 1

      You 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 8

      and this approach requires the maximum LIS.

      I hope mt explanations were clear.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        LCA?? Did you mean LIS?

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          YES!!

          SORRY after thinking on problem E LCA was stuck in my head XD

          Sorry it was a mistake. I fixed it now.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Thanks !! Nice Approach.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        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.

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Fixed it.I should have put "<" and ">" string between "" to make it look correct in comment.

          Thank you.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        It's really a nice solution!Thx

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Can you please share the intuition behind this method?

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Tanx very much.its lot clear.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Nice approach.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    EMEJ Thanks bro really great solution

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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, 7

for 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,7

need 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

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Question on A

On 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?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks for the answer

      Does 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?

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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 <iomanip>).

    edit: this was already answered, but I didn't refresh it

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Does this mean there is some default setprecision value even when the value of the double has no decimals?

      • »
        »
        »
        »
        5 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        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.

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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)

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Fair, seems like it is limiting itself to 6 digits of precision, as it would rather print 1e6 than 1000000.

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

In problem div 2 C how are we updating the value of mx and mn? Can anyone explain it please?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Whats wrong with this code failing test 3 3rd token :
https://codeforces.me/contest/1304/submission/71185623

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      if ans is zero that means there is some customer who can't be satisfied so why not to break .

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

»
5 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

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 <i,j>

We can find out the max value by simple RMQ algorithms on array P[i-1]

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Nice Problemset and editorial. I really Enjoy the contest. Thanks to problem setter.djm03178

»
5 years ago, # |
  Vote: I like it +5 Vote: I do not like it

I believe there's something wrong with the second question's answer as if it wants the longest palindromic string then for the case like

5 2 oo ox xo xx xx

The 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 like

3 2 xx xx xx

The output from the code is "2 , xx" and not "6 , xxxxxx" . If I'm wrong anywhere please let me know .

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

What's the problem wtih my code?

https://codeforces.me/contest/1304/submission/71196899

My method as similar sa the editorial.

Just he use the arrays.Please tell me where my code is wrong.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 .

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    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

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks for the response. I understood your point. My bad !

»
5 years ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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<store.size());
        string temp1 = inp[store[i]];
        reverse(temp1.begin(), temp1.end());
        out.append(temp1);
    }

I tried printing i, and it printed out a large value.

for(i=store.size()-1; i>=0; i--){
        if(i<0 || i>=store.size())
        {
            cout<<"OOH!: "<<i<<'\n';
            break;
        }
        string temp1 = inp[store[i]];
        reverse(temp1.begin(), temp1.end());
        out.append(temp1);
    }

OUTPUT:

OOH!: 4294967295

It's really puzzling and I would appreciate any help. Thanks!

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    i is declared to be long long

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      size() returns an unsigned integer, so if the value is 0u then subtracting 1 from it will make it overflow.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

for problem D. How to find a sequence for fixed length k LIS?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.me/contest/1304/submission/71262563 I'd be very thankful.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    __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.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I'm not understanding why I'm getting WA in Problem C My Submission. Can anyone help?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You break out of for loop where input is read. So you don't read the entire input for a test case!

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem C, can someone explain how the condition mn-k <= R and mx+k >= L is working?? Thanks in advance :)

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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."

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you please explain about updating mn and mx

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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).

»
5 years ago, # |
  Vote: I like it -27 Vote: I do not like it

for solution of (B) — Longest Palindrome:

input:
3 3
aba
aba
aba
given output:
3
aba

but, it should be

9
abaabaaba
  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    It says all strings are distinct, so it's not a valid case.

»
5 years ago, # |
  Vote: I like it -21 Vote: I do not like it

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

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +24 Vote: I do not like it

    Please, read my comment right above yours.

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Please anyone can help me to figure out what wrong with my submission. I struggle in many hours on test case 8, problem E.

»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Honestly, for Problem D

is 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 ?! :/)

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.