Блог пользователя bondchi

Автор bondchi, история, 2 года назад, По-английски

can someone please give a solution to this problem[problem:978A][user:DessertRommet]

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
2 года назад, # |
Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

Play with the indexes this time, store the last index of each element either in a map data structure, or an array will work too as array elements are from 1 to 1000.

then create a list/array/vector of the answer and store all the last index in it. Then sort the list, as the index are in increasing order.

print the size and print the element with respective index.

Like this (pseudo code) ->

int arr[n]; // our input array
int lastIndex[1001]= {0}; // array to store last index of the elements as they will not exceed 1000.

for(int i=0; i<n; i++) lastIndex[arr[i]] = i;

vector<int> ans //create an answer vector, similar as list in java

for(int i=0; i<n; i++) ans.push_back(lastIndex[arr[i]]); // store the last index of each element

sort(ans.begin(), ans.end()); //sort the answer so that the element be in order as the index of element are always in increasing order

print ans.size()  //size of the ans array will be the size;

for(int i=0; i<ans.size(); i++) print arr[ans[i]] // print the array with the index we've sorted

check the submission if needed -> 172281151