CCC '15 J4 - Wait Time

View as PDF

Points: 5
Time limit: 2.0s
Memory limit: 256M

Problem type
Canadian Computing Competition: 2015 Stage 1, Junior #4

You exchange text messages with your friends. Since you receive so many messages, you want to measure how long your friends have to wait for your replies.

Your message device records each received and sent message in order using the following two kinds of entries:

• R indicates a message was received from a friend numbered ;
• S indicates a message was sent to a friend numbered .

Your message device sends and receives messages instantaneously, and for each consecutive pair of entries described above, either

• a single entry W is recorded in between them indicating they occur seconds apart, or
• there is no entry between them and they occur one second apart

Several rules of message etiquette are always followed:

• the only messages you send are replies to messages that you have received;
• you send at most one reply to any message from any particular friend;
• your friends do not send a subsequent message until you have replied to their previous message.

The wait time for a message is the time that passes between when you receive it and the time you reply to it. If a friend received a reply to each message they sent, the total wait time for friend is the sum of all wait times for all messages from friend . Otherwise, the total wait time for friend is .

Your job is to determine the total wait time for each friend.

Input Specification

The input consists of the integer (), followed by lines, where each line consists of one character (W, R, or S), followed by a space, followed by an integer (). These lines are the entries described above (in order).

Output Specification

Output one line for each friend that sent a message in the form where is a friend number and is the total wait time for friend . The lines are in increasing order of the friend numbers.

5
R 2
R 3
W 5
S 2
S 3

2 6
3 6

Explanation of Output for Sample Input 1

Friend sends a message at time and Friend sends a message at time . Friend receives a reply at time and Friend receives a reply at time .

14
R 12
W 2
R 23
W 3
R 45
S 45
R 45
S 23
R 23
W 2
S 23
R 34
S 12
S 34

12 13
23 8
34 2
45 -1

Explanation of Output for Sample Input 2

For Friend , a message is received at time and replied to at time . For Friend , two messages are exchanged, with the first message having a wait time of seconds and the second message having a wait time of seconds. For Friend , a message is received at time and replied to at time . Friend sends a message which is never replied to.

• commented on April 4, 2022, 10:11 p.m.

Can someone explain how the wait time in the first line of the second output be "13" seconds? I am a bit confused.

• commented on Jan. 4, 2023, 5:07 a.m.

From my understanding: You add all of the lines together excluding the two lines that are repetitive. So you will be adding "R 12" "W 2", "R 23", "W 3", "R 45", "W 2", "S 23", "R 34", which will be: (1 + 2 + 1 + 3 + 1 + 2 + 1 + 1 = 12). And when it gets to the "S 12", it turned 13 and which is why the waiting time is 13.

• commented on Nov. 8, 2021, 2:23 p.m.

I'm confused by the example inputs/outputs because in the first when we have R 3, W 5 in between r2 and s2 we get 6, which is what you'd expect if we only looked at the ones in between them but in the second example we get -1 for 45 when there is nothing between the two, shouldn't it be 0?

• commented on Nov. 8, 2021, 11:55 p.m.

If a friend received a reply to each message they sent, the total wait time for friend is the sum of all wait times for all messages from friend . Otherwise, the total wait time for friend is .

In the second sample, friend did not recieve a reply for their second message.

• commented on Dec. 13, 2020, 6:47 p.m.

My friend would find that so useful

• commented on Sept. 12, 2020, 9:37 p.m.

Either you're really popular or your messaging electronic is really bad. You choose.

• commented on Feb. 19, 2017, 4:24 p.m.

In the sample output, it shows "2 6" and "3 6" yet the explanation states that it should be "2 6" and "3 7".

(This is for sample output 1)

• commented on Feb. 19, 2017, 4:47 p.m.

This typo was present on the official PDF as well. (Waterloo double check your work plz -.- )

It has been fixed (at least on our site).

• commented on Nov. 28, 2018, 4:29 a.m.

I don't believe it is a typo. When a reply was sent to friend 3, it was time 7. The only reason the output is "3 6" is because the message from friend 3 was received at time 1 (first message counts as time 0). This logic carries through to the explanation of sample input 2.

• commented on Oct. 11, 2018, 1:12 a.m.

Yeah, on wcipeg it still says:

Explanation of Sample 1 Friend 2 sends a message at time 0 and Friend 3 sends a message at time 1. Friend 2 receives a reply at time 6 and Friend 3 receives a reply at time 7.

so it is probably only fixed here!!!

• commented on Dec. 28, 2019, 11:42 p.m. edited

No fixing required! As pointed out by JJ_G4M3R, it's correct :-)