You are not logged in. Please login at www.codechef.com to post your questions!

×

ADADET - Editorial

PROBLEM LINK:

Practice
Contest

Author: Alei Reyes
Primary Tester: Hussain Kara Fallah
Secondary Tester: Kacper Walentynowicz
Editorialist: Hussain Kara Fallah

DIFFICULTY:

Medium

PREREQUISITES:

Basic Geometry , Convex Hull

PROBLEM:

Given N towers on the X axis. Each tower can be imagined a straight vertical segment. Given the position of each tower on the axis and its height (segment length). Towers are given in ascending order of positions, and no 2 towers share the same height.

You have to determine for each tower the rightmost tower with shorter height such that drawing a segment between their tops doesn't intersect any other tower. Intersection with towers exactly at its top is ok. (this case is covered in the sample please check the image in problem description).

N ≤ 500000

EXPLANATION:

This problem is similar to finding the upper convex hull of a set of points, but forcing each tower to be able to shoot only shorter towers made it a little bit tricky.

An important observation here. Consider the ith tower and let's find the smallest j (j>i) such that Hj > Hi. (The closest tower to the ith which is taller than it). It's obvious that we can't draw a segment from the ith to any other shorter tower after the jth one without intersecting with it. (Proof is easy).

Let's process the towers one by one from the rightmost one to the leftmost one. We will stack our towers into piles, each one having towers sorted in decreasing order of height from left to right. Suppose we are processing a certain tower and the last added tower was higher, so the answer of our current tower would be -1 and we will make a new pile and insert it into.

Since each pile will have towers sorted in height from left to right, If we consider having towers forming only one pile, then the segments representing answers of towers in this pile must form the upper convex hull of it. (Check these examples of a pile):

alt text

alt text

Now back to our problem, let's consider this case. Points A,B,C must be added into the same pile. Currently, ans(A) = -1 , ans(B) = A , ans(C) = B

alt text

If our tower was higher than the last added one (and we had only one pile), we should process the pile, and evaluate our current tower with the last added two towers to ensure that we are maintaining the convex hull of this pile, and this can be done easily by checking the cross product of 2 vectors and behaving according to the sign.

Let's insert point D, as you can see the vectors (D->C,C->B) make a counter clockwise turn, so we must pop point C from our pile to maintain convexity.

alt text

The trick here is when we have a tower to process and we have more than one pile (with the highest tower of each shorter than the processed tower), then we should drop the nearest pile as long as we have more than one. After that, we will be left with only one pile and we insert our tower there. Because the head of each pile will have no tower to the left higher than it, otherwise we would insert it in the same pile (or maybe drop the whole pile). So since our current tower is higher than the shot one and the shot one has no higher tower to the left. We can shoot it without intersecting with any other tower.

After processing points {A,B,C,D,E,F} we had two piles {A,B,C,D},{E,F}. When we came to process point G, you can see that we can shoot the head of each pile. The purpose of dropping piles can be shown in this sample. Because point E cannot be shot from point G.

alt text

We insert point G into the pile of D and drop {E,F}

alt text

This solution runs in O(N), each tower is inserted once, and may be popped once and for all.

AUTHOR'S AND TESTER'S SOLUTIONS:

AUTHOR's solution: Here
TESTER's solution: Here

This question is marked "community wiki".

asked 20 Jun '17, 23:07

deadwing97's gravatar image

3★deadwing97
1181234
accept rate: 0%

edited 22 Jun '17, 14:50

admin's gravatar image

0★admin ♦♦
19.8k350498541

Great problem, very much resembles the core idea of monotone chain algorithm, except done with different hulls

(22 Jun '17, 16:28) hikarico5★
1

Maybe :P I am not really familiar with theoretical stuff. Great problem as you said

(22 Jun '17, 16:47) deadwing973★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,657
×647
×173
×74
×23
×20
×11

question asked: 20 Jun '17, 23:07

question was seen: 751 times

last updated: 22 Jun '17, 16:47