benchmarks.wiki / Public workspace

Humanity's Last Code Exam / 2021_I / Spider Walk

Problem

Answer published by the source. Consult the official source to check your work against its answer.

question title

Spider Walk

question content

## Problem Statement Charlotte the spider sits at the center of her spiderweb, which consists of a series of silken straight strands that go from the center to the outer boundary of the web. Charlotte’s web also has bridges, each of which connects two adjacent strands. The two endpoints of a bridge always have the same distance to the center of the spiderweb. When Charlotte has finished a late-night feasting in the center and wants to retreat to some corner, she walks to the edge on autopilot. To do this, she picks a starting strand, and walks along it until she meets the first bridge on that strand. She will cross the bridge and go to the other strand, and then keeps walking outwards until she meets another bridge. Then she will cross that bridge, and repeat this process, until there are no more bridges on the current strand, and then she will walk to the end of the current strand. Note that Charlotte must cross all the bridges that she meets. Charlotte’s favorite corner to sleep in during the daytime is at the end of strand ss. For each possible starting strand, she wants to know the minimum number of bridges to add to the original web in order to end at ss. Charlotte can add a bridge at any point along the strand, as long as the added bridge does not touch any other bridge. The two endpoints of any added bridge must have the same distance to the center of the spiderweb, and the bridge must connect two adjacent strands. ## Input The first line of input has three integers nn, mm, and ss, where nn (3n200,000)(3 \leq n \leq 200,000) is the number of strands, mm (0m500,000)(0 \leq m \leq 500,000) is the number of bridges, and ss (1sn)(1 \leq s \leq n) is Charlotte’s favorite strand. Strands are labeled from 1 to nn in counterclockwise order. Each of the remaining mm lines contains two integers dd and tt describing a bridge, where dd (1d109)(1 \leq d \leq 10^9) is the bridge’s distance from the center of the spiderweb and tt (1tn)(1 \leq t \leq n) is the first strand of the bridge in counterclockwise order. Specifically, if 1t<n1 \leq t < n, then the bridge connects strands tt and t+1t+1. If t=nt = n, then the bridge connects strands 1 and nn. All bridge distances dd are distinct. ## Output Output nn lines, where the ii-th line is the minimum number of bridges Charlotte needs to add in order to end at strand ss after walking on autopilot from strand ii. ## Sample Input 1
Plain-text mathematical notation (without MathML)
## Problem Statement

Charlotte the spider sits at the center of her spiderweb, which consists of a series of silken straight strands that go from the center to the outer boundary of the web. Charlotte’s web also has bridges, each of which connects two adjacent strands. The two endpoints of a bridge always have the same distance to the center of the spiderweb.

When Charlotte has finished a late-night feasting in the center and wants to retreat to some corner, she walks to the edge on autopilot. To do this, she picks a starting strand, and walks along it until she meets the first bridge on that strand. She will cross the bridge and go to the other strand, and then keeps walking outwards until she meets another bridge. Then she will cross that bridge, and repeat this process, until there are no more bridges on the current strand, and then she will walk to the end of the current strand. Note that Charlotte must cross all the bridges that she meets.

Charlotte’s favorite corner to sleep in during the daytime is at the end of strand s. For each possible starting strand, she wants to know the minimum number of bridges to add to the original web in order to end at s. Charlotte can add a bridge at any point along the strand, as long as the added bridge does not touch any other bridge. The two endpoints of any added bridge must have the same distance to the center of the spiderweb, and the bridge must connect two adjacent strands.

## Input

The first line of input has three integers n, m, and s, where n (3≤n≤200,000) is the number of strands, m (0≤m≤500,000) is the number of bridges, and s (1≤s≤n) is Charlotte’s favorite strand. Strands are labeled from 1 to n in counterclockwise order. Each of the remaining m lines contains two integers d and t describing a bridge, where d (1≤d≤10⁹) is the bridge’s distance from the center of the spiderweb and t (1≤t≤n) is the first strand of the bridge in counterclockwise order. Specifically, if 1≤t<n, then the bridge connects strands t and t+1. If t=n, then the bridge connects strands 1 and n. All bridge distances d are distinct.

## Output

Output n lines, where the i-th line is the minimum number of bridges Charlotte needs to add in order to end at strand s after walking on autopilot from strand i.

## Sample Input 1

Original LaTeX notation
## Problem Statement

Charlotte the spider sits at the center of her spiderweb, which consists of a series of silken straight strands that go from the center to the outer boundary of the web. Charlotte’s web also has bridges, each of which connects two adjacent strands. The two endpoints of a bridge always have the same distance to the center of the spiderweb.

When Charlotte has finished a late-night feasting in the center and wants to retreat to some corner, she walks to the edge on autopilot. To do this, she picks a starting strand, and walks along it until she meets the first bridge on that strand. She will cross the bridge and go to the other strand, and then keeps walking outwards until she meets another bridge. Then she will cross that bridge, and repeat this process, until there are no more bridges on the current strand, and then she will walk to the end of the current strand. Note that Charlotte must cross all the bridges that she meets.

Charlotte’s favorite corner to sleep in during the daytime is at the end of strand \(s\). For each possible starting strand, she wants to know the minimum number of bridges to add to the original web in order to end at \(s\). Charlotte can add a bridge at any point along the strand, as long as the added bridge does not touch any other bridge. The two endpoints of any added bridge must have the same distance to the center of the spiderweb, and the bridge must connect two adjacent strands.

## Input

The first line of input has three integers \(n\), \(m\), and \(s\), where \(n\) \((3 \leq n \leq 200,000)\) is the number of strands, \(m\) \((0 \leq m \leq 500,000)\) is the number of bridges, and \(s\) \((1 \leq s \leq n)\) is Charlotte’s favorite strand. Strands are labeled from 1 to \(n\) in counterclockwise order. Each of the remaining \(m\) lines contains two integers \(d\) and \(t\) describing a bridge, where \(d\) \((1 \leq d \leq 10^9)\) is the bridge’s distance from the center of the spiderweb and \(t\) \((1 \leq t \leq n)\) is the first strand of the bridge in counterclockwise order. Specifically, if \(1 \leq t < n\), then the bridge connects strands \(t\) and \(t+1\). If \(t = n\), then the bridge connects strands 1 and \(n\). All bridge distances \(d\) are distinct.

## Output

Output \(n\) lines, where the \(i\)-th line is the minimum number of bridges Charlotte needs to add in order to end at strand \(s\) after walking on autopilot from strand \(i\).

## Sample Input 1

Code

7 5 6
2 1
4 3
6 3
8 7
10 5

## Sample Output 1

Code

2
1
1
1
0
1
2

## Sample Input 2

Code

4 4 2
1 1
2 2
3 3
4 4

## Sample Output 2

Code

1
1
0
1

platform

atcoder

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import