Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

DELNMS - Editorial

$
0
0

PROBLEM LINKS

Practice
Contest

DIFFICULTY

CHALLENGE

PREREQUISITES

Ad Hoc

PROBLEM

You are given a sequence of N integers. You wish to delete all the numbers in this sequence.

The only operation available to you is to delete some of the integers that follow the folling rules

  • All of them have the same values
  • Their positions are in arithmetic progression
  • The arithmetic progression should only be defined by
    • Start Position
    • Common Difference
  • The AP should cover all the positions that are within the limits and lie on the rught of the Start Position

Also, the delete operation results in the gaps left by the deleted number being filled by moving the numbers to the left.

EXPLANATION

naive solution

It is quite obvious as to what would constitutes the simplest solution to get AC.

for i = N to 1
    print "v = ", i
    print "d = ", 1

The idea is that we can always delete the last number without affecting anything else in the array.

Thus, there is always a way to delete all the numbers within O(N) operations.

dynamic order indices

Once a delete operation is performed, you have to adjust the indices of all the numbers which are on the right of any number which was deleted.

This operation can be performed in O(N) after each delete operation. But, this might be too expensive.

  • We can store the positions at the leaves of a segment tree and
  • Store the number of items in the left subtree of each internal node.

deletion of a node

Delete Operation can be handled by updating all the O(log N) segments encountered on the way of the search for the number.

Now, any delete operation can be handled in O(log N) time, at the tradeoff of increasing the query time of the order index from O(1) to O(log N).

searching the order index

Since we only perform stabbing queries, we will encounter O(log N) internal nodes.

When going to the right child of a root, we must add up the count of nodes in the left subtree of the root, plus one, to count the root.

A large number of solutions use the following ideas along with data structure to maintain the dynamic order indices

  • Store the list of positions that exist for each number in the array.
  • Search for the next candidate number to be deleted

The next candidate for deletion may just be the largest AP of positions stored for that number.

The challenge is also to make sure that each selection of v and t satisfies the criteria in the problem statement.

SETTER'S SOLUTION

Can be found here.

TESTER'S SOLUTION

Can be found here.


Viewing all articles
Browse latest Browse all 40121


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>