Log inSign up
Problems / B032

Shelving the boxes by weight

200 ptsCases 15Set by dongbinna
Added October 30.

Statement

N boxes have arrived at a logistics warehouse. Each box has its weight written on it and carries a label of letters on the outside. Two boxes can carry the same label.

A warehouse worker wants to put the boxes that arrived onto a shelf, starting with the lightest one. When two or more boxes weigh the same, the box that reached the warehouse earlier goes up first.

For example, suppose N=3 boxes arrived as in the following table. A smaller number means the box arrived earlier.

Arrival order123
Weight14712583
LabelXHzKTGzBJmEKDM

Putting them up from the lightest one gives the following order on the shelf.

Shelf order123
Weight83125147
LabelEKDMGzBJmXHzKT

Now look at a case with boxes of equal weight mixed in. Suppose N=5 boxes arrived as in the following table.

Arrival order12345
Weight4012401240
Labelabcdefghij

Two boxes weigh 12 and three weigh 40. Among boxes of equal weight the earlier arrival goes first, so the order on the shelf is the following. Of the two boxes weighing 12, box 2 arrived before box 4, so box 2 comes first.

Shelf order12345
Weight1212404040
Labelcdghabefij

Given the information about the N boxes, write a program that prints the weight and the label of each box in the order the boxes go onto the shelf.

Up to 100,000 boxes can arrive. Even with many boxes of equal weight mixed in, the order that puts the earlier arrival up first must not waver.

Input and output examples

Example 1

Input

3
147 XHzKT
125 GzBJm
83 EKDM

Output

83 EKDM
125 GzBJm
147 XHzKT

Example 2

Input

1
153 oPvf

Output

153 oPvf

Example 3

Input

5
3 yvxH
187 csB
96 HsiH
8 rtc
152 ah

Output

3 yvxH
8 rtc
96 HsiH
152 ah
187 csB

Input

  • The first line gives the number of boxes N. N is a natural number less than or equal to 100,000.

  • The next N lines give the information about the boxes, one box per line, in the order they reached the warehouse. Each line gives that box's weight and label, separated by one space.

  • The weight is a natural number from 1 to 200.

  • The label is a string made only of uppercase and lowercase letters, and its length is from 1 to 10.

  • Several boxes can share a weight, and several boxes can share a label.

Output

  • Print the information about the boxes, one box per line, in the order the boxes go onto the shelf. On each line, print that box's weight and label separated by one space.

  • Grading compares the output with the expected value character by character. Without that final blank line, the answer is marked wrong even when the order is correct.

Requirements

  • Read the input from standard input and write the answer to standard output
  • A lighter box comes first
  • Among boxes of equal weight, the one that reached the warehouse earlier comes first. This condition is the heart of this problem
  • On each line, print the weight and the label separated by one space. Do not put a space at the end of the line
  • Grading compares the output with the expected value down to the last character
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 15 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
1
153 oPvf
153 oPvf
3
147 XHzKT
125 GzBJm
83 EKDM
83 EKDM
125 GzBJm
147 XHzKT
5
3 yvxH
187 csB
96 HsiH
8 rtc
152 ah
3 yvxH
8 rtc
96 HsiH
152 ah
187 csB

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 89ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

Nobody yet. You could be the first.