Wait Time
InterviewTime limit1sMemory limit256 MB
Reconstruct message timestamps from the log and sum each friend reply wait time, reporting -1 for a friend with an unanswered message.
- Level
Easy2 of 10
- Topics
- Simulation, Hash map
- Solved
- No attempts yet
Problem
You exchange text messages with your friends. You receive so many messages that you want to measure how long your friends wait for your replies.
Your device logs every message you receive and every message you send, in order, using two kinds of entries.
R X: a message arrived from the friend numbered .S X: a message was sent to the friend numbered .
Messages travel instantly. Between two consecutive entries of the kind above, exactly one of the following holds.
- A single
W Xentry sits between them, and they happen seconds apart. - Nothing sits between them, and they happen one second apart.
The rules of message etiquette are always obeyed.
- Every message you send is a reply to a message you already received.
- You send at most one reply to any single message from a friend.
- A friend does not send another message until you reply to the previous one.
The wait time of a message is the time that passes from the moment it arrives to the moment you reply to it. If every message from friend received a reply, the total wait time for friend is the sum of the wait times of those messages. If even one message from friend went unanswered, the total wait time for friend is -1.
Compute the total wait time for each friend.
Input
The first line contains the integer (). Each of the next lines contains one character (W, R, or S), a single space, and an integer (). Those lines are the entries described above, given in the order they happened.
Output
For each friend who sent a message, print one line in the form X T, where is the friend number and is the total wait time for that friend. Print the lines in increasing order of friend number.
Hint
In the first example, friend 2 sends a message at second 0 and friend 3 sends a message at second 1. Friend 2 gets a reply at second 6 and friend 3 gets a reply at second 7.
In the second example, the message from friend 12 arrives at second 0 and is answered at second 13. Friend 23 sends two messages, the first with a wait time of 6 seconds and the second with a wait time of 2 seconds. The message from friend 34 arrives at second 12 and is answered at second 14. The last message from friend 45 never gets a reply.