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

×

# PROBLEM LINK:

Author: Dmytro Berezin
Tester: Shang Jingbo and Gerald Agapov
Editorialist: Devendra Agarwal

SIMPLE

# PREREQUISITES:

Basic Dynamic Programming

# PROBLEM:

Given Frog's location on the X axis and they can communicate their message to another frog only if the distance are less than or equal to K , now you need to answer if given two frogs can communicate or not. Assumption : Frog's are cooperative in nature and will transfer the message without editing it :D .

# Quick Explanation

Find for each frog the maximum distance which his message can reach. Two Frogs can only communicate if their maximum distance of communication are same.

Reason
You can easily proof by contradiction. Let us assume that there are two frogs 1 and 2 which can communicate but their maximum distances are different. You can easily contradict this , proof is left as an exercise for the reader.

# Explanation

Only Challenge left is to calculate the maximum distance which each frog can message.

The First point to note is that one of the optimal strategy of each frog(say f) will be to send message to it's nearest frog(say f1) and then it will be the responsibility of the nearest frog to carry it further.

One Line Proof : Frog's reachable from the f in the direction of f1 is also reachable from f1.
Another point to note is that the frog on the extreme positive side of X axis(i.e Maximum A[i] , say A[j]) can communicate till A[j] + K.

Using these observation , one can use a simple dp to calculate the maximum distance . But how ?

Sort A[i]'s but do not loose the index of frog's while sorting. Let the sorted array be Frog[] . Now if Frog[i] can communicate to Frog[i+1] , then Frog[i] can communicate as mcuh distance as Frog[i+1] can communicate.

Pseudo Code

Pre-Compute ( A , K ):
sort(A,A+N);        //Sorted in Decreasing Order of X .
Max_Distance[A[0].ind]=A[0].v+K;
for(int i=1;i<N;i++)
if((A[i-1].x-A[i].x)<=K)
Max_Distance[A[i].ind] = Max_Distance[A[i-1].ind];
else
Max_Distance[A[i].ind] = A[i].x + K;

Answer ( x , y ):
if ( Max_Distance[x] == Max_Distance[y] ):
return "Yes"
else
return "No"


Complexity:

O(N*log(N)), N * logN for sorting N integers.

# AUTHOR'S and TESTER'S SOLUTIONS:

Author's solution to be uploaded soon.
Tester's solution

This question is marked "community wiki".

asked 14 Jul '14, 15:01

46273842
accept rate: 0%

0★admin ♦♦
19.1k348495534

I know i solved it with more complexity added but i have used the Segment tree . http://www.codechef.com/viewsolution/4217571

(14 Jul '14, 15:16) 2★
11

I did the problem using union find algorithm

(14 Jul '14, 15:16)

In the editorial it is given as ,any two frogs which have same maximum distances can communicate with each other. i am not able to clearly understand this statement.for example let us take this sample test case:

5 1 1

0 1 5 6 8

2 4

the second frog has a max distance of 1,and the fourth frog also has a maximum distance of 1.but they cant communicate with each other ...can sumone clarify??

(14 Jul '14, 18:17) 3★

@prem_93 : Maximum distance of communication of frog 2 is 1+1(=k) = 2 , whereas frog 4 has maximum distance of 6+1 = 7 , so they cannot communicate.

(14 Jul '14, 18:44)

@devuy11 ...can u plz take the pain of explaining how max distance is calculated??

(14 Jul '14, 19:30) 3★

@prem_93 : There is no pain in explaining :D.Let's first sort each frog with it's X-Cordinate.Look closely at the last frog,he can communicate till Last_Frog_Position + K,Now look at the second last frog,If the message sent by the second last frog can reach last frog , then we can assume that the last frog will communicate the message of second last frog without editing :D , otherwise second_last_frog can send it up to second_last_frog position + K . Point to observe is that in calculation of maximum distance of second last frog , only last frog is needed . I hope you can extent it from here.

(14 Jul '14, 19:42)

thnks that helped...i actually misunderstood the maximum distance of a specific frog as the maximum distamce itz message can reach..for example in the test case which i have given the fourth frog can transmit itz message over a maximum distance of 1 unit.but the max distance here the editorial specifies the maximum coordinate ...right....also my solution with complexity O(nlogn) is giving tle...how should i correct it??prob link: http://www.codechef.com/viewsolution/4319695

(14 Jul '14, 19:57) 3★

@prem_93 : right :) . Your code seems to be tough one for me to understand :( .

(14 Jul '14, 20:13)

my algorithm is as follows:

1) used merge sort to sort the x co-ordinates.

2)after sorting, i found the points in which the communication path is disconnected.ie)the x coordinates for which , the distance between them and the next cordinate (in the sorted order) is greater than k. i stored these values in a new array callect disconnect.

3)now given x and y(nos of the frogs) i calculate their x coordinates and check if there is any value "v" in disconect array satisfying the condition:pos(x)<=v<pos(y)...if yes...then i output "no" and "Yes" if otherwise.

(14 Jul '14, 20:36) 3★

Please explain in a more readable manner. The English is sort of broken and I cannot understand the solution properly :(

(14 Jul '14, 22:16)

1) i ued merge sort to sort the x coordinates.

2)then,i took the new sorted array and traversed through each element ,finding the elements for which this condition is satisfied: arr[i+1]-arr[i]>k.i stored these in a new array (let it be p array).

3)now in each query I take in the values of x and y(frog nos),and find their respective positions(x coordinates)...let them be pos(x) and pos(y)...now i go through the "p" array and check if there is any value in it that satisfies this condition:pos(x)<=value<pos(y). if there is such a value the answer is "NO" and "YES" if otherwise.

(15 Jul '14, 11:19) 3★

For example: INPUT:

5 1 1

0 1 5 6 8

2 4

1)here after sorting the array is:0 1 5 6 8

2)in this case my "p" array would contain the elements 1 6..bcoz (5-1>k) and(8-6>k).

3) now in the query i have x=2,y=4...pos(2)=1 and pos(4)=6. now there is an element(1) in my p array which satisfies the condition 1<=1<6 and hence my answer would be "NO".

(15 Jul '14, 11:24) 3★

Hi, how do you prove by contradiction that both frogs can only communicate if they can reach the same maximal distance?

(18 Jul '14, 02:55) 2★

@kuruma - suppose frog at x coordinate = 1 can send message to maximum x=10, this means all frogs between x=1 and x=10 can send message to x=10. And this means frog at x=1 cannot send message to frog at x>10(because max distance he can send message is 10) .. And suppose frog at x=15 can send message to maximum x=20 and frog at x=10 is able to send message to x=15.. so the maximum distance frog at x=10 can send msg is x=20 and maximum distance frog at x=15 can send msg is x=20. that means frog at x=10 and x=15 can communicate (because all frog between x=10 and x=20 can communicate)

(18 Jul '14, 03:56)

PROOFBYCONTRADICTION

STATEMENT:Twofrogs which have same maximal distances cannot communicate with each other.

let the common maximal distance be Y.

let position(frog'1') be a and pos(frog'2') be b.

letY>=b>=a.

Since,1stfrog can communicate till"Y",it should also mean that it can communicate to all the frogs whose positions are in the interval [a,Y].Since"b"is a position which satisfies this condition it can communicate with 2nd frog but this contradicts our statement and hence we have proved it false.

thus this proves that 2 frogs with same max distance can communicate.

(18 Jul '14, 10:08) 3★

@kuruma you can also prove the statement:

"Two frogs which have differnet maximal distances can communicate with each other." to be false in a similar way.

And from these 2 proofs you can conclude that "Two frogs can communicate with each other only if their maximal distances are the same."

(18 Jul '14, 10:14) 3★

@ShangJingbo and @GeraldAgapov can u please provide the test case for which my code gave TLE?? http://www.codechef.com/viewsolution/4327814

(30 Jul '14, 15:31)

max distance= position of frog + k

(10 Jan '15, 16:05) 2★
showing 5 of 18 show all

38 Answers:
 6 alternatively, make an another array containing indices, sort them using the first array. then assign some common number to those frogs who can communicate. ( In sample test case, indices becomes 0 1 3 2 4 and we have to check if v[1]-v[0]<=k) if yes then assign some common value to them ). Link to code: http://ideone.com/nGyLo9 answered 14 Jul '14, 16:56 156●1●5●11 accept rate: 0%
 3 I solved the problem in two ways: Using Segment Trees, Using concept of connected component as pointed out by @brobear1995 But I haven't thought of a DP solution...I always miss out on DP.. :( answered 14 Jul '14, 17:10 3★akumar3 141●6 accept rate: 0%
 1 Methodology Used: 1. Keep the input array 2. Create a duplicate array of it. 3. Sort the duplicate array. 4. Use the input index to get the element from original array, use Binary search ti find the index of that element in the duplicate sorted array. 5. Iterate through the starting index to the last index and check for <=K while iterating (a[i+1]-a[i]<=K). 6. If reaches to the end, output Yes else No. 7. Giving WA. #include #include #define ll long long #define fore(i,x) for(int i=0;ihigh) return -1; int mid=(low+high)/2; if(e==inp[mid])return mid; if(e>N>>K>>P; fore(i,N) {cin>>inp[i];dupinp[i]=inp[i];} sort(dupinp,dupinp+N); bool flag=true; while(P--) { cin>>A>>B; if(A==B)cout<<"Yes"<K) {flag=false;break;} } if(!flag)cout<<"No"<
 1 I copied the original array A into another array B and sorted it in increasing order, while maintaining the index.Then I copied the elements which fail the condition mentioned. for(i=0;ik) vec.push_back(B[i]);  Then I used lower_bound to check the position of both the elements (A[x-1],A[y-1]) in the vector vec. If abs(posx-posy)>=1, the answer will be no else it will be yes. It worked. http://www.codechef.com/viewsolution/4191094 answered 15 Jul '14, 11:03 5★rtheman 69●3 accept rate: 0%
 0 Hi , can u plz tell me for which Test case I am getting WA . http://www.codechef.com/viewsolution/4277673 Thanks rajesh answered 14 Jul '14, 16:44 16●2 accept rate: 0%
 0 Same thing I do but still getting TLE I sorted them using quick sort and then go through min coordinate to max coordinate and note down every starting point after breaking point say if coordinates are 0 8 5 3 12 and distance is 3 than i stored 0 12 in new array than i take input from user and get coordinate of there position and look for whether they belong to same interval or not if yes than answer is yes else no. Link to solution Please tell me what wrong in my code answered 14 Jul '14, 17:31 111●1●1●8 accept rate: 18%
 0 Sir, i have used this code and getting correct answer for all the test cases which i have tried....i am still getting WA...please help where is the error in the code?? http://www.codechef.com/viewsolution/4254933 answered 14 Jul '14, 17:33 1 accept rate: 0%
 0 I solved it using segment tree . answered 14 Jul '14, 17:55 4★anuj95 288●2●6●14 accept rate: 12%
 0 Sir, I have used my code for most of the test cases and getting the desired output.....still I am getting WA....Kindly tell the respective cases for which my code was wrong....Thank you http://www.codechef.com/viewsolution/4297541 answered 14 Jul '14, 21:21 1●1 accept rate: 0% 1 You are assuming that a[q-1] < a[m-1] which may not be true . Even if you are able to debug your code , You will get a TLE as the worst case complexity of your algorithm is O(N*P) where N <= 10^5 and P <= 10^5 . (15 Jul '14, 16:30) anuj954★
 0 Hello All, I used the following method: 1. Treat all positions of Frogs as node in a graph. 2. create a undirected path for nodes whose distance is distance is less than K. 3. Used Breadth first search to find if target node is reachable or not. Getting WA. Please anyone tell me what's wrong in the logic, i'll be really helpful. Thanks!!! answered 14 Jul '14, 21:47 1●1 accept rate: 0% how did you store the graph?? Using adjacency matrix or List? (17 Jul '14, 00:33) saroj3★ I used Adjacency list to store the graph (10 Aug '14, 14:54)
 toggle preview community wiki:
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:

×14,850
×1,847
×1,034
×23
×22

question asked: 14 Jul '14, 15:01

question was seen: 13,870 times

last updated: 23 Jun, 00:55