Editorial for ICPC NWERC 2011 C - Movie Collection


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
  • Note, since m, r = 100\,000, you cannot update the stack in \mathcal O(m) time
  • Therefore you need some smart data structure to store information
  • Binary indexed tree/Fenwick tree does the job in \mathcal O(\log m) time
  • At every step, take movie x and put it back on -1, -2, -3, \dots

Note that this is also \mathcal O(n^2):

for (int j = 0; j < r; j++) {
  movie = sc.nextInt();
  index = movies.indexOf(movie);
  System.out.print("" + (m - index - 1) + " ");
  movies.removeElementAt(index);
  movies.add(movie);
}

Comments

There are no comments at the moment.