Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to efficiently map to original indices of an array after modification?

How to efficiently map to original indices of an array after modification?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestiondatabasedata-structuresjson
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13068788
    wrote on last edited by
    #1

    This is a simplified version of the problem.

    We have an array of numbers (between 0 and X-1), at each round, we choose a range of numbers that we compare and only promote the biggest one and eliminate the rest. At each round, we need to output the *original* indices of the numbers that have been eliminated.

    This is best explained using an example:

    Here is the input:

    8 // X represents the size of the array, 2 ≤ X ≤ 100000
    4 // Y represents the number of elimination rounds 1 ≤ Y ≤ X−1
    1 0 3 6 2 4 7 5 // the numbers
    1 3
    2 4
    1 3
    0 1
    

    Output: 4 lines, the original index of the losers at each round

    1 2
    4 5
    3 7
    0
    

    Here is a diagram of how the output was obtained, `[]` means the numbers inside the brackets are the ones being compared.

    1 \[0 3 6\] 2 4 7 5 // output 1 2 because 0 and 3 were eliminated
    1 6 \[2 4 7\] 5 // output 4 5 because 4 and 2 were eliminated
    1 \[6 7 5\] // output 3 7 because 7 and 5 were eliminated
    \[1 7\] // output 0 because 1 was eliminated
    7
    

    The basic idea is that we want to output the original indices of the number eliminated at each round.

    **Approach:**

    Model each number as a pair, with the second entry representing the original index. So we have the following:

    (1,0) \[(0,1) (3,2) (6,3)\] (2,4) (4,5) (7,6) (5,7) // 1 2
    (1,0) (6,3) \[(2,4) (4,5) (7,6)\] (5,7) // 4 5
    (1,0) \[(6,3) (7,6) (5,7)\] // 3 7
    \[(1,0) (7,6)\] // 0
    \[7,6\]
    

    Is this a promising approach? What is the best way to complement it efficiently?

    Is there a better, more efficient approach that is easier to implement?

    V 1 Reply Last reply
    0
    • U User 13068788

      This is a simplified version of the problem.

      We have an array of numbers (between 0 and X-1), at each round, we choose a range of numbers that we compare and only promote the biggest one and eliminate the rest. At each round, we need to output the *original* indices of the numbers that have been eliminated.

      This is best explained using an example:

      Here is the input:

      8 // X represents the size of the array, 2 ≤ X ≤ 100000
      4 // Y represents the number of elimination rounds 1 ≤ Y ≤ X−1
      1 0 3 6 2 4 7 5 // the numbers
      1 3
      2 4
      1 3
      0 1
      

      Output: 4 lines, the original index of the losers at each round

      1 2
      4 5
      3 7
      0
      

      Here is a diagram of how the output was obtained, `[]` means the numbers inside the brackets are the ones being compared.

      1 \[0 3 6\] 2 4 7 5 // output 1 2 because 0 and 3 were eliminated
      1 6 \[2 4 7\] 5 // output 4 5 because 4 and 2 were eliminated
      1 \[6 7 5\] // output 3 7 because 7 and 5 were eliminated
      \[1 7\] // output 0 because 1 was eliminated
      7
      

      The basic idea is that we want to output the original indices of the number eliminated at each round.

      **Approach:**

      Model each number as a pair, with the second entry representing the original index. So we have the following:

      (1,0) \[(0,1) (3,2) (6,3)\] (2,4) (4,5) (7,6) (5,7) // 1 2
      (1,0) (6,3) \[(2,4) (4,5) (7,6)\] (5,7) // 4 5
      (1,0) \[(6,3) (7,6) (5,7)\] // 3 7
      \[(1,0) (7,6)\] // 0
      \[7,6\]
      

      Is this a promising approach? What is the best way to complement it efficiently?

      Is there a better, more efficient approach that is easier to implement?

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Is it a homework? What is your variant of the solution? What does not work in your solution?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups