dkg7888's blog

By dkg7888, history, 2 years ago, In English

How to solve this problem.
I saw it in a contest conducted on 13 March 8:00PM-11:00PM IST .

  • Vote: I like it
  • +11
  • Vote: I do not like it

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

I didn't implement this solution and don't know if this will work or not.

Construct a segment tree where the nodes have a frequency array. Using this type of segment tree we can find the frequencies of all letters in the range [l,r] in $$$O(logN)$$$ time.

Now once we know the frequencies ,we can quickly judge if that sub segment can be re arranged as a palindrome.

If a palindrome is possible, find the compressed version of the palindrome. ( For example the compressed version of this string aaabbccccd will be a:3 b:2 c:4 d:1 ). After you know the compressed version you will have to lazily propagate the changes i.e. new frequencies . Since the length of the compressed string can be at max 53 (palindrome like abcde...yzazy.....dcba) and lazy propagation works in logN , this approach will hopefully pass.

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

    Thanks.

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

    Implemented the same and passed 90% of the testcases rest TLED. But this can be done 100% with this approach.

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

      You will be shocked to know that I used brute force and 90% passed

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

        I think that the testcases were very weak for 5th and 6th problem .

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

          Can u tell your approach for problem 5?

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

            let mx[i] = maximum character in the string s[i....n] Notice that since we need to reverse only 1 substring so we should reverse the string starting at index i and ending at index j , s.t. (s[i]!=mx[i] and s[j]=mx[i] and i<j and i is as small as possible ) Now value of i can be found in O(N) and value of all j's can be found in O(N) as well.
            Time complexity is O(N^2) passed because of Weak TC
            So , now we can check for all j by traversing on the length x of the new string and comparing character which will be at x'th position for the new strings .New string are string formed after reversing substring from i to j .

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

              First I thought of O(n^2) solution but didn't implemented as it would give tle

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

                There is O(NlogN) as well which uses string Hashing and Binary search . Let there be 2 values l,m in the vector containing j , so now we can binary search the first element that differs after reversing s[i...l] and s[i...m] , hash value can be used to check whether string of length mid are equal or not .
                P.S.: This one is what I thought at first , but I just went for O(N^2) solution at first

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

      Were you able to solve the 4th problem ?
      Problem Statement:
      Given a tree consisting of N nodes , find minimum no. of edges to be removed to get atleast one component of size K for all(1<=K<=N).
      1<=N<=1000

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

        I was only able to solve 2 problems with all the test cases passed

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

          How the subsequence with product's unit digit is k would be solved???¿????

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

            It's a Dynamic programming problem , for the ith element there are two option only , either this element can be included or it can't be .
            dp[i][j] denotes no. of subsequences considering first i element that have value at unit digit equal to j .
            Transition are : dp[i+1][(j*(a[i]%10))%10]+=dp[i][j]; (i.e. current element is included )
            dp[i+1][j]+= dp[i][j]; (i.e. current element isn't included )

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

              Hey did you solved that problem in which chalk was to be divided into pieces by k children's in which we had to find the minimized maximum chalk length

              and also for this subsequence with product's unit digit is k can you please provide the code.

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

                Minimum maximum chalk length can be solved by binary search on answers

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

                  I found some of the test cases were giving faulty results. For ex, N = 3 and K = 0 with Arr = { 2 , 6 , 0 } should return value as 6 but the judge was considering value = 3 for such case.

                  Also N = 2 and K = 2 with Arr = {7 , 17} . The return value should be 8 but the judge was expecting 7.

                  I tried using maps but was getting caught up in test cases like this.

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

                  for n = 2 k = 2 a = {7,17} return value is 7. 7,17 7,10,7 (17 = 7+10) k = 1 7,5,5,7 (10 = 5+5) k = 2

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

                They were not allowing to use max heap(priority queue). So I had to use vector & sort it again & again that's why it resulted in TLE. Still, 72% of the test cases passsed.

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

                  U should have just included it's header file . It might have worked .

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

        Yes it can be done by applying dp on the flattened tree.

        First flatten the tree using euler tour and then take dp[i][num_removed] which means we have covered all the subtrees with exit time less than i and total nodes removed is num_removed.
        LHS means Left hand side of the equation

        Like for the first example given there euler tour is something like
        n=6, edges={0,1},{0,2},{2,3},{2,5},{3,4}
        euler tour: 0 1 2 3 4 5
        (here coincidence is in_time is same as node number(not necessary))
        initially

        $$$dp[i][j]=1e9$$$

        for all

        $$$i,j$$$

        initialise

        $$$dp[0][0]=0$$$

        if we take the first subtree at that start at time 0
        then transition

        $$$if(i==0){ add=-1;}$$$

        (I rooted the tree at node 0)}

        $$$dp[i+out_time[node_at[i]]+1][k+sub_t_size[node_at[i]]]=min(LHS,dp[i][k]+1+add);$$$

        if we not cut current subtree that start at time i

        $$$dp[i+1][k]=min(LHS,dp[i][k]);$$$

        and ith ans is

        $$$min(dp[n][i],dp[n][n-i])$$$

        //dp[n-i] is excluded when i=n

        in[],out[] times and node_at[] can be calculated with simple DFS

        70% test cases passed

        The test cases which were not passing because of some validator error like this, incorrect and is shown even when right ans is outputted

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

          What is LHS ?

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

            Plz check the lastest edit and LHS is left hand side

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

              Okkk .
              So in this solution of yours u r considering that if we delete an edge from i->j such that j is a descendent of i , we don't need to delete any other edge in the subtree of j ?

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

                Right if we deleted an edge , that would delete a complete subtree and we won't be able to delete any other edge in the deleted subtree

                And state defining choices are we delete a subtree ofcouse by deleting an edge or not

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

                  Ok , thanks

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

                  Hey plz look at the images in spoiler and
                  can you tell why even correct output is marked incorrect?
                  and
                  one more thing I wanna add is when I downloaded the testcases even they were downloading not the same question

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

                  U returned a vector of size n+1 rather than of the the size n , maybe that's the reason . I have no idea about testcases , I too faced same issue.

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

Can someone help me with this

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

Did anyone get a mail regarding placements from codekaze ? Could you mention your national rank, graduation year and graduation year rank if you did receive a mail regarding placements?

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

    They asked to fill Google form National rank 497 Graduation year rank 104

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

    I haven't received any details about placements from codekaze.

    National rank: 393

    Graduation rank: 252

    Pre-final year

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

is there any way i can test my approach for this and other problem that were asked in the contest.(I cannot took part in the contest but want to upsolve)?.

Thanks in advance.

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

Has anyone received Amazon gift card?

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

    I have not received it yet . I have filled the google form which they have send but not received the voucher till now what should I Do.