Ants Caught | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Ants Caught

Hi everyone!! Can someone please help me out by providing the code of the below problem in C++. An infinite army of ants is marching on an infinite 2-D plane. Since ants are disciplined, here's how they march: each ant chooses exactly one x coordinate and moves along it in positive y direction, starting from (x, 0). There exists exactly one ant for each x coordinate on that plane and hence there are infinite ants! There are N horizontal barriers lying on this plane. The ith barrier is defined by (xi, yi) and di, which means that the barrier is blocking all ants which want to pass through points lying on line segment connecting (xi, yi) and (xi + di, yi). Once an ant encounters a barrier, it stops moving. Given all the barriers, your task is to find the total number of ants, that will be ever blocked at some point in their march. INPUT The first line contains an integer N which denotes the number of barriers. Next N lines follow, each contains 3 space separated integers, "xi yi di" as explained in problem statement above. Note: The barriers in the input may overlap. OUTPUT Output a single integer, the number of ants that will be ever blocked at some point in their march. CONSTRAINTS 1 ≤ N ≤ 105 1 ≤ xi, yi, di ≤ 109

22nd May 2017, 9:24 AM
pratik ahuja
7 Antworten
+ 14
^_^
22nd May 2017, 9:58 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
@Val is perfectly fine. :>
22nd May 2017, 4:12 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
ValentinHacker r u ok?
22nd May 2017, 9:45 AM
Ahri Fox
Ahri Fox - avatar
+ 4
Anyone can explain?
23rd May 2017, 3:18 PM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 2
I wish I could help but I don't understand this.
22nd May 2017, 4:17 PM
Luyanda
Luyanda - avatar
+ 1
So far my logic is to do find first sigmas of all di s. Then adding n to the sum of all dis.Store it in Sum. Then create an array to store the value of all x posn where the ant caught stuck. In that array i will find how many times a single x element repeats itself. I will subtract this from the sum. That wll be my answr. What say coders??????
22nd May 2017, 10:30 AM
pratik ahuja
+ 1
But I cant figure out how to store the values in that array in which i will store all xi s help me with that
22nd May 2017, 2:11 PM
pratik ahuja