Unofficial Editorials February Long Challenge (Part - 2)

Hello Guys,

This is part two of Unofficial Editorials February Challenge, containing editorials for CARPTUN, BROCLK and POINPOLY.

For other editorials, refer CHEFCHR, CHEFPTNT and PERMPAL [here][1].

Problem : [CARPTUN][3]

Problem Difficulty: Easy

Problem Statement:

Given Delay times of N toll booths, speed of cars S, number of cars C and the distance D between each tol booth, determine the delay between First car and Last car.

Solution:

Observation:

Sub-task 1:

In this case, There are only two cars, so we can actually simulate the whole process in O(CN) without getting TLE.

Let time[i][j] denote exit time of ith car from jth toll.

For first car, Time taken by car to exit from ith toll is time[0][i] = time[0][i-1]+D/S+A[i]. (Taking into account the time taken to reach toll and Delay time at ith tunnel.)

For subsequent cars, Time taken by car to exit ith toll would depend upon exit time of previous car. Specifically, time[C][i] = max(time[C][i-1]+D/S, time[C-1][i])+A[i]

Above relationship because when we reach the toll, there might be a car already there, in which case we’ll have to wait till that car completes its delay time. If there’s no car when we reach there, we can start the delay time immediately.

Final Answer would be time[C-1][N-1] - time[0][N-1].

Refer [this][4] solution for details.

Subtask 2:

Now, Number of cars is upto 1e6, which means that O(CN) solution will time out. We need some observations to speed up our solution to O(N).

Observation:

  1. Speed and Distance given in problem is irrelevant.
    Proof: Since each car has to cover D*(N-1) distance to reach from first toll to last toll, all cars will have the same delay D*(N-1)/S due to driving, thus, it would be better to simply ignore this delay.
  2. The Final Answer will be max(A[i])*(C-1).
    Proof: Suppose a car can now move with the infinite speed. Now, the only time each car has to wait will be max(A[i]). This can be proved easily, thus left as an exercise.

Hence, we get the answer max(A[i])*(C-1) in O(N) time.

[Link][5] to my solution.

Link to my


[6].

### Problem : [BROCLK][7]
#### Problem Difficulty: Easy-Medium
#### Prerequisites: Matrix exponentation, Trigonometric identities.
### Problem Statement:
Given a clock with one hand (Don't know how Chef see the time from it :P), which got broken, now move by fixed angle. We are not given this angle, but Length of hand L, and Distance of tip of hand from X-axis after one second.

We are supposed to find distance of tip of hand from x-axis.

### Solution:

We are given following scenario.

![alt text][8]

**Time for some Math**:

We can see that the triangle in figure is a right triangle. Time for [Trigooonmetry][9].

We can see that for given position, $\sin (90 - \theta) = \cos \theta = D/L$.

We can easily see that After T seconds, The angle moved by hand is $T*x$, and thus, Our final answer would be $L*\cos(T*X)$.

Now, How to calculate $\cos(T*X)$ ??

### Important property: $\cos(N*X) = 2*\cos(X)*\cos((N-1)*X) - \cos((N-2)*X)$

Proof can be found [here][10]

### When T is upto $1e6$:
In this case, we can Run a loop from 1 to T aand compute the answer. Refer [this][11].
### When T is around $1e18$:
Now, we cannot run a loop as we will get TLE.

But, observe the above Recurrence Carefully. Doesn't is resemble the well know Fibonacci Sequence. So, we will calculate the Tth value of above recurrence in $O(logT)$ time, getting 100 points, in same manner as we Calculate Nth Number of Fibonacci Sequence, as explained [here][12].

Not so easily, Because above explanation is lacking one point regarding use of Modular inverse, as well as Formation of matrix , which i have intentionally left to as to give you a chance to try even after the contest. DO GIVE A TRY BEFORE RUSHING TO SOLUTION. (This very point kept my solution hanging till the last day.)

[details=Click to view]
 Fraction $d/l$ can also be represented as $d*modInverse(l)$ and also, modulo can be taken at every step to avoid overflow.
[/details]

Link to my 

[13].

Problem : [POINPOLY][14]

Problem Difficulty: Medium

Problem Statement:

Given a convex polygon consisting of N points, Find and print floor(N/10) points lying strictly inside it or report if it’s impossible.

Solution:

Geometry is scary, Polygon is no less. But, Triangle are our friends in this problem. Wanna know how??

See the following picture.

![alt text][15]

This way, We have divided our polygon into a set of triangles. Now, For a triangle, it is easy to determine it a point lie strictly inside the tirangle, using following property. If ABC is the riiangle and P is the point to be checked, then

Area(ABC) = Area(PAB)+ Area(PAC)+Area(PBC) And, None of these three triangle should have area 0 (P will lie on border of triangle in this case.)

Now, Coming to implementing part, How to select points to be checked??

The answer is, Process all triangles one by one and Check points around the corners of triangle (In all 8 directions) and add them to answer if they lie inside.

Now Comes the crux of Solution. Triangle is a convex polygon, thus, all points lying inside a triangle will be adjacent to each other. This means that we can continue adding points into our answer bag, until we get sufficient number of points or there aren’t anymore points inside triangle, thus we will move to next triangle.

We must check surrounding 8 cells, and insert into answer bag, as well as search its neighbors (Just like moving inside a grid). Make sure not to process any point twice.

And that’s all, 100 points to you because you have solved and implemented this solution.

For implementation details, refer my


[16].

This brings end to part 2, Hope you liked them.

Feel free to post your queries.

Enjoy coding!!

  [1]: https://discuss.codechef.com/questions/122738/unofficial-editorials-february-long-challenge-part-1
  [3]: https://www.codechef.com/problems/CARPTUN
  [4]: https://www.codechef.com/viewsolution/17322856
  [5]: https://www.codechef.com/viewsolution/17360209
  [6]: https://www.codechef.com/viewsolution/17360209
  [7]: https://www.codechef.com/problems/CHEFPTNT
  [8]: https://discuss.codechef.com/upfiles/BROCLK.png
  [9]: https://www.khanacademy.org/math/trigonometry/trigonometry-right-triangles/intro-to-the-trig-ratios/a/opposite-adjacent-hypotenuse
  [10]: https://math.stackexchange.com/questions/1017668/prove-that-cosnx-2-cosn-1x-cosx-cosn-2x
  [11]: https://www.codechef.com/viewsolution/17411630
  [12]: https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/
  [13]: https://www.codechef.com/viewsolution/17412144
  [14]: https://www.codechef.com/problems/PERMPAL
  [15]: https://discuss.codechef.com/upfiles/poly.png
  [16]: https://www.codechef.com/viewsolution/17246330
7 Likes

Part 3: Editorial of Chef and Land Labeling will be posted, if you people drop a comment below.

1 Like

Can you please look at my solution and tell why it is not passing the last subtask https://www.codechef.com/viewsolution/17416092

on each iteration i m dividing the angle by 2 and calculate value accordingly ie O(log(time))

Thank You

For BROCLK-

I used simple formula.

Suppose we want to calculate cos(nx), then-

Even n-

Just use cos(nx)=2{cos}^{2}(n/2)x-1 . We know cosx and hence we can find numerator and denominator in logN time.

Odd n-

Notice that denominator portion for cos(nx) (unreduced form-numerator and denominator not in lowest term) is nothing but {L}^{n}. Now we use-

Cos(nx)+cos(x)=2*cos((n+1)/2x)*cos((n-1)/2x)

The RHS of denominator is nothing but case of even n, which we did earlier. The only thing to do is to subtract cos(x). Now, notice that cos(x)=D/L. I already know the final denominator, I just need to know the numerator part. Its nothing but D* DenominatorOf(cos((n+1)/2x)*cos((n-1)/2x)) [I think you guys guessed the denominator by now? :3 :stuck_out_tongue: )

Solution- https://www.codechef.com/viewsolution/17339918

For POINPOLY-

LOL this was a rollercoaster for comedy as I solved it. Initially, I was like “MEh, Geometry. GGWP I give up.”

But, just for fun’s sake, I decided to experiment out and submitted a random solution which passed for 70 points. Then my next few hundred submissions were spent on trying to grab those 30 points.

What I did was-

Assume large N. We know that since no 3 points are collinear, there must be difference of at least 1 in y coordinate for any 3 points, else y=k is a line passing through them. This means, if N is large, the diagonally opposite vertex is kind of “far” from the original one. So just take their mid point - it will lie inside the polygon.

Yeah, thats it, no checks on if point is inside or etc. The only thing to know is “how far” you want, or “how far” will hold good.

The principle assumptions of this approach is-

  1. The polygon is wide enough so that difference of +-0.5 does not affect the answer (in case the (X_1 + X_2) or(Y1+Y2) is odd.
  2. The polygon is convex, not concave.

The only place it failed was a specific case for N=10, which was narrow. For that I had to include a check that for small polygons actually check if it lies inside or not. :confused: Yeah, sad but had to swallow my pride :frowning:

3 Likes

For BROCLK we can also do this,
Lets say i want to calculate

cosnx ,n can be expressed in binary form,
So lets say cos5x=cos(x+4x)

So just calculate cosx,cos2x,…cos(log(n)(x)). ,
And also calculate sinx,sin2x,sin4x,…
(Sin2n=2sinn*cosn (Keep the irrational separate))
Now solving ,

cos(7x):

First cos3x=cosx.cos2x - sinx.sin3x

(Take care that sinx is irrational,but multiplication of two sin is rational)

Sin3x=cosxsin2x + sinxcos2x

After this calculate cos7x=cos(3x+ 4x)

My solution: https://www.codechef.com/viewsolution/17256856

3 Likes

The irrational was the downer for me. I decided to stick to all cos :3

Yeah for me too,bt didnt know anything except these basic formulas

1 Like

Check out my solution. Oh shit, I forgot adding links, right?

Thanks!!!

You sure its not O({log_N}^{2}) ? Because that solution got big TLE on its face.

  1. *How did we reduce the cos(tx)L to p/q form? I am having some problem in understanding this part. Is it by changing for eg 0.56 to 56/100 then dividing by common gcd i.e 14/25?

  2. And we search for a formula for cos(nx) because of exceeded memory to store nx right?

Thanks.

exceeded memory to store nx

What?

How did we reduce the cos(tx)*L to p/q form? I am having some problem in understanding this part. Is it by changing for eg 0.56 to 56/100 then dividing by common gcd i.e 14/25?

Yes. Getting involved with decimals was all you needed to spend an eon on this problem and still be unable to crack it. Way too many round errors.

i have tried both approach dividing into triangles and simply using whole polygon:

  1. Approach 1:simply using polygon

in simply using whole polygon approach first i checked it contain sufficient point or not by using pick’s theorem then calculated maxY and minY and then start sweeping line to adjacent point on boundary of polygon and calculating all points on line by using bresenham line points generating algorithm and checking inclusion also…by this i passed top 6 subcases passed in less time limit and got TLE for last 2.link text

  1. Approach 2:dividing whole polygon into triangle

as said in editorial i divided polygon into triangle and used triangle inclusion test not that Area test for inclusion given in editorial because i feel it will give answer true(for inside) when point is in boundary which will give wrong answer.and similarly find points inside triangle as in previous approach(rotating line point to next point)…but this give large TLE on face only top 2 test case passed.and after testing time taken by traingle inclusion i found out it is taking too much time than fast polygon inclusion test.link text

**After contest over i saw some solutions and find that this question did a comedy with me. it is not a medium problem as tags are saying(binary search…etc.) and can be bypassed easily with some observations.link text
**

Well, real comedy was with me- random getting 70 points XDXD

@taran_1407 in ur second solution for <=1e6 how you deal with fraction parts.

I have represented d/l as d*modInverse(l), added that part in editorial too. Forgot to aad earlier.

1 Like

Well, for this problem, i wrote the solution, and submitted and woah, got 100 points in one go. Frankly, i was sure my implementation was right, but i wasn’t sure if my idea will work in tight bound cases.

And

There’s nothing bad in swallowing pride to ur code as long as ur code gets AC.

Broken clock alone kept me busy whole contest, than all other problems combined. So much it troubled me, that i didn’t get time to explore lucas theorem for more than 10 points, when i could have got 50 because i got the same idea as editorial 2 hours before end of contest.

I too messed up with fractions (includes creating my own fraction class) but kept getting overflow WA. only last day i took modInverse.

I would recommend you to read about modular arithmetic (especially modular multiplicative inverse). If we have a rational p/q, it can also be stated as p*(1/q) and (1/q) is same as modular multiplicative inverse.

I know this will appear confusing at first sight, but first read about modular multiplicative inverse.