0

I need help with choosing right data structure to my exercise.

For input I have been given number of operations that should be executed(t) and after that indexed sequence of natural numbers seperated with a space. So for example:

3 1 2 3

This means that there will be 3 operations executed on sequence {1,2,3}.

There is also defined a pointer, which shows current position. Operations on that sequence that I should implement are:

  • R -> removing element c on index PTR+1 and moving PTR forward c times

  • X -> inserting right after element c, which is on index PTR (so inserting on PTR+1), element with value of c-1 and of course moving PTR forward c times.

My job is to find ending sequence after performing operations R and X t times so that if its element is even then do the R, else do the X. At the start PTR shows first element(if exists) and it should be all in cycle.

For given example at the start of post the output should be:

0 0 3 1

I know that it might sound confusing, so let me show you how this should work step by step.

t = 1

Starting sequence: 1 2 3

Actual position: PTR -> 1

Operation: X, c=1

Ending sequence: 1 0 2 3

Ending position: PTR -> 0

t = 2

Starting sequence: 1 0 2 3

Actual position: PTR -> 0

Operation: R, c=2

Ending sequence: 1 0 3

Ending position: PTR -> 1

t = 3

Starting sequence: 1 0 3

Actual position: PTR -> 1

Operation: X, c=1

Ending sequence: 1 0 0 3

Ending position: PTR -> 0

The solution is a sequence from PTR in right direction. So output should be: 0 0 3 1

As for circumscriptions:

  • starting length of C sequence up to 10^7

  • number of t operations up to 10^7

  • moving PTR to right up to 10^9 times

I have created my algorithm, which is based on circular linked list. Works, but it's too slow for some tests. I'd be more than grateful if someone could help me in finding the best data structure.

I've got also a hint from my teacher that I should use binary list, but to be honest I didn't find anything about this structure on the internet! Maybe also someone knows this thing and show me where can I search for information about it? I'd apreciate any help.

dominosam
  • 7
  • 4
  • At each step, what determines whether you perform **X** or **R**? – Prune Jan 02 '18 at 22:40
  • I'm not sure what "binary list" means -- it's not a data structure term with which I'm familiar. Can you ask your teacher? I'm worried that there's a translation error in this phrase. – Prune Jan 02 '18 at 22:42
  • `t times so that if its element is even then do the R, else do the X`, `t` starts at `1`, and will be odd, so **X** is performed. At `2`, it's **even**, so do **R**. – IMCoins Jan 02 '18 at 22:42
  • @Prune https://www.geeksforgeeks.org/binary-tree-set-1-introduction/ – IMCoins Jan 02 '18 at 22:43
  • @IMCoins I'm quite familiar with a binary *tree*; the term in question is "binary *list*". – Prune Jan 02 '18 at 22:49
  • This is the same thing. A binary tree is the same as a binary list. Just look at some stuff [like this one](https://books.google.fr/books?id=aJQILlLxRmAC&pg=PA431&lpg=PA431&dq=binary+list+python&source=bl&ots=CB4JDESdWB&sig=yaHV0kHfzi6fkw9zPj6EdVXdLIA&hl=fr&sa=X&ved=0ahUKEwjd1_HZrrrYAhWJKlAKHYRHBogQ6AEIUzAF#v=onepage&q=binary%20list%20python&f=false). His question is algorithm-wise, so yes, I believe this is it. – IMCoins Jan 02 '18 at 22:55
  • @Prune It's just like IMCoins wrote. It depends if the element is even .I mean it is true that I translated it, but i think direct translation from polish "lista binarna" is binary list. – dominosam Jan 02 '18 at 23:07
  • That document does *not* contain a structure called a "binary list". There are binary-partitioning algorithms that work on a list, but that's not the same thing. Applying a binary search to a list does not make it a "binary list". – Prune Jan 02 '18 at 23:08
  • @IMCoins So if it should be binary tree, how would it look like? Depending on what I should put elements into it? – dominosam Jan 02 '18 at 23:08
  • I'm actually writting a solution using **arrays** that are better using modulos arithmetic. I'm doing this in python only to show the logic. – IMCoins Jan 02 '18 at 23:10
  • @IMCoins https://hackage.haskell.org/package/binary-list suggests that a binary list is something completely different than a binary tree. (But not in any way that applies to this problem.) – btilly Jan 02 '18 at 23:12

3 Answers3

1

Use a circular doubly linked list. It has O(1) insert and remove. (Perhaps this is what your instructor meant by "binary list.")

Fun fact: you can reduce the memory utilization of a doubly linked list with an XOR trick with code here. Lower memory utilization will mean better speed for large lists due to better cache behavior. There's also a SO Q&A on XOR linked lists, which itemizes some of its drawbacks.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
  • Thank you very much, Sir! I feel like it might be the solution. I'll start to implement this as soon as possible. – dominosam Jan 02 '18 at 23:37
0

The data structure depends very much on those available in your chosen implementation language. In most, simply use an array, and adjust the index with the modulus operator. That will give you the needed circularity.

This assumes that your chosen language has basic insert & delete methods, or slicing to facilitate doing it on your own.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

R -> removing element c on index PTR+1 and moving PTR forward c times

Looks like a single-linked list: PTR points to a list element, we need to remove the next element and advance PTR pointer c times.

X -> inserting right after element c, which is on index PTR (so inserting on PTR+1), element with value of c-1 and of course moving PTR forward c times.

Also looks like a single-linked list. PTR points to a list element, we insert next element with value c-1 and advance PTR pointer c times.

@dominosam The answer you marked as correct is wrong in fact. There is no need to traverse back the list, so all you need is a circular single-linked list like described, for example, on Wikipedia.

Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33