D. Laying Cables

Codeforces
IDCF10097D
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it was decided to place the main server in the largest city, and to connect any other city j to the city k that has bigger population than j and is the closest to it (if there are many such cities, the largest one should be chosen). City k is called a parent of city j in this case. Unfortunately, the Ministry of Communications got stuck in determining from where and to where the Internet cables should be laid, and the population of the country is suffering. So you should solve the problem. For every city, find its parent city. The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of cities. Each of the next n lines contains two space-separated integers xi and pi (1 ≤ xi,  pi ≤ 109) — the coordinate and the population of the i-th city. Output n space-separated integers. The i-th number should be the parent of the i-th city, or  - 1, if the i-th city doesn't have a parent. The cities are numbered from 1 by their order in the input. ## Input The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of cities.Each of the next n lines contains two space-separated integers xi and pi (1 ≤ xi,  pi ≤ 109) — the coordinate and the population of the i-th city. ## Output Output n space-separated integers. The i-th number should be the parent of the i-th city, or  - 1, if the i-th city doesn't have a parent. The cities are numbered from 1 by their order in the input. [samples]
**Definitions** Let $ n \in \mathbb{Z} $ be the number of cities. For each city $ i \in \{1, \dots, n\} $, let $ (x_i, p_i) \in \mathbb{R} \times \mathbb{R}^+ $ denote its coordinate and population, with all $ x_i $ distinct and all $ p_i $ distinct. Let $ C = \{(i, x_i, p_i) \mid i = 1, \dots, n\} $ be the set of cities indexed by input order. **Constraints** 1. $ 1 \leq n \leq 200000 $ 2. $ 1 \leq x_i, p_i \leq 10^9 $ for all $ i $ 3. All $ x_i $ are pairwise distinct. 4. All $ p_i $ are pairwise distinct. **Objective** For each city $ i $, define its *parent* $ \text{parent}(i) $ as follows: - If $ p_i $ is the maximum population among all cities, then $ \text{parent}(i) = -1 $. - Otherwise, let $ P_i = \{ j \mid p_j > p_i \} $ be the set of cities with strictly greater population. Among all $ j \in P_i $, select the one minimizing $ |x_j - x_i| $. If there are multiple such $ j $ with the same minimal distance, choose the one with the *largest* $ p_j $. Then $ \text{parent}(i) = j $ (the input index of the chosen city). Output an array $ [\text{parent}(1), \text{parent}(2), \dots, \text{parent}(n)] $.
API Response (JSON)
{
  "problem": {
    "name": "D. Laying Cables",
    "description": {
      "content": "One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10097D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of cities.  \nFor each city $ i \\in \\{1, \\dots, n\\} $, let $ (x_i, p_i) \\in \\mathbb{R} \\times \\mathbb{R}^+ $ denote its coordinate and populatio...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments