codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1623B.md (3645B)


      1 Source: https://codeforces.com/problemset/problem/1623/B
      2 
      3 B. Game on Ranges
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Alice and Bob play the following game. Alice has a set $S$ of disjoint ranges of integers, initially containing only one range $[1, n]$. In one turn, Alice picks a range $[l, r]$ from the set $S$ and asks Bob to pick a number in the range. Bob chooses a number $d$ ($l \le d \le r$). Then Alice removes $[l, r]$ from $S$ and puts into the set $S$ the range $[l, d - 1]$ (if $l \le d - 1$) and the range $[d + 1, r]$ (if $d + 1 \le r$). The game ends when the set $S$ is empty. We can show that the number of turns in each game is exactly $n$.
     22 
     23 After playing the game, Alice remembers all the ranges $[l, r]$ she picked from the set $S$, but Bob does not remember any of the numbers that he picked. But Bob is smart, and he knows he can find out his numbers $d$ from Alice's ranges, and so he asks you for help with your programming skill.
     24 
     25 Given the list of ranges that Alice has picked ($[l, r]$), for each range, help Bob find the number $d$ that Bob has picked.
     26 
     27 We can show that there is always a unique way for Bob to choose his number for a list of valid ranges picked by Alice.
     28 
     29 Input
     30 
     31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.
     32 
     33 The first line of each test case contains a single integer $n$ ($1 \le n \le 1000$).
     34 
     35 Each of the next $n$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$), denoting the range $[l, r]$ that Alice picked at some point.
     36 
     37 Note that the ranges are given in no particular order.
     38 
     39 It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the ranges for each test case are from a valid game.
     40 
     41 Output
     42 
     43 For each test case print $n$ lines. Each line should contain three integers $l$, $r$, and $d$, denoting that for Alice's range $[l, r]$ Bob picked the number $d$.
     44 
     45 You can print the lines in any order. We can show that the answer is unique.
     46 
     47 It is not required to print a new line after each test case. The new lines in the output of the example are for readability only.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 4
     55 1
     56 1 1
     57 3
     58 1 3
     59 2 3
     60 2 2
     61 6
     62 1 1
     63 3 5
     64 4 4
     65 3 6
     66 4 5
     67 1 6
     68 5
     69 1 5
     70 1 2
     71 4 5
     72 2 2
     73 4 4
     74 ```
     75 
     76 Output
     77 
     78 ```
     79 1 1 1
     80 
     81 1 3 1
     82 2 2 2
     83 2 3 3
     84 
     85 1 1 1
     86 3 5 3
     87 4 4 4
     88 3 6 6
     89 4 5 5
     90 1 6 2
     91 
     92 1 5 3
     93 1 2 1
     94 4 5 5
     95 2 2 2
     96 4 4 4
     97 ```
     98 
     99 Note
    100 
    101 In the first test case, there is only 1 range $[1, 1]$. There was only one range $[1, 1]$ for Alice to pick, and there was only one number $1$ for Bob to pick.
    102 
    103 In the second test case, $n = 3$. Initially, the set contains only one range $[1, 3]$.
    104 
    105 * Alice picked the range $[1, 3]$. Bob picked the number $1$. Then Alice put the range $[2, 3]$ back to the set, which after this turn is the only range in the set.
    106 * Alice picked the range $[2, 3]$. Bob picked the number $3$. Then Alice put the range $[2, 2]$ back to the set.
    107 * Alice picked the range $[2, 2]$. Bob picked the number $2$. The game ended.
    108 
    109 In the fourth test case, the game was played with $n = 5$. Initially, the set contains only one range $[1, 5]$. The game's turn is described in the following table.
    110 
    111 |  |  |  |  |
    112 | --- | --- | --- | --- |
    113 | Game turn | Alice's picked range | Bob's picked number | The range set after |
    114 | Before the game start |  |  | $ \{ [1, 5] \} $ |
    115 | 1 | $[1, 5]$ | $3$ | $ \{ [1, 2], [4, 5] \}$ |
    116 | 2 | $[1, 2]$ | $1$ | $ \{ [2, 2], [4, 5] \} $ |
    117 | 3 | $[4, 5]$ | $5$ | $ \{ [2, 2], [4, 4] \} $ |
    118 | 4 | $[2, 2]$ | $2$ | $ \{ [4, 4] \} $ |
    119 | 5 | $[4, 4]$ | $4$ | $ \{ \} $ (empty set) |