#P3972. Movie Rating and Ranking System

    ID: 17220 Type: Default 1000ms 256MiB

Movie Rating and Ranking System

Movie Rating and Ranking System

Little Z has invented a new movie rating system that supports three operations: publishing a new movie, rating a movie, and querying the ranking of a movie by its score. The system works as follows:

  • Publish a new movie (P): When publishing a movie with a list of main actors, if none of these actors have appeared in any previously published movie, the movie is assigned an initial rating of 0. Otherwise, its rating is set to the rating of the most recently published movie which shares at least one common actor with the new movie.
  • Rate a movie (R): When a movie is rated, its new rating becomes the arithmetic average of its current rating and the new given rating.
  • Query ranking (Q): When querying a movie, output its ranking among all published movies. The ranking is determined primarily by the rating (higher ratings get better, i.e. smaller, ranks). If two movies have the same rating, the one published earlier is ranked higher.

Note that the ratings are always in the range \( [0,5] \) and a movie's publication order is the order of its appearance among the operations.

inputFormat

The first line contains an integer \( T \) representing the number of operations. Each of the following \( T \) lines represents an operation in one of the following formats:

  • For publishing a movie: P movie_name k actor1 actor2 ... actork. The integer \( k \) indicates the number of main actors for the movie.
  • For rating a movie: R movie_name new_rating. The new_rating is a floating-point number.
  • For querying a movie: Q movie_name. Output the current ranking of the movie.

outputFormat

For each query operation, output one line containing the rank of the corresponding movie. The highest rated movie is ranked 1. In case of ties in rating, the movie published earlier (i.e. with a lower publication order) is ranked higher.

sample

9
P A 2 alice bob
P B 2 bob charlie
R A 4.5
P C 2 alice dave
R B 5
Q A
R C 3
Q C
Q B
2

1 2

</p>