Skip to content

Commit f2bc140

Browse files
authored
Hostel Visit
1 parent 772c5bb commit f2bc140

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

Hacker Blocks/Hostel Visit.cpp

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* Hacker Blocks */
2+
/* Title - Hostel Visit */
3+
/* Created by - Akash Modak */
4+
/* Date - 27/10/2020 */
5+
6+
// Dean of MAIT is going to visit Hostels of MAIT. As you know that he is a very busy person so he decided to visit only first "K" nearest Hostels. Hostels are situated in 2D plane. You are given the coordinates of hostels and you have to answer the Rocket distance of Kth nearest hostel from origin ( Dean's place ) .
7+
8+
// Input Format
9+
// First line of input contains Q Total no. of queries and K There are two types of queries:
10+
11+
// first type : 1 x y For query of 1st type, you came to know about the co-ordinates ( x , y ) of newly constructed hostel. second type : 2 For query of 2nd type, you have to output the Rocket distance of Kth nearest hostel till now.
12+
13+
// The Dean will always stay at his place ( origin ). It is gauranted that there will be atleast k queries of type 1 before first query of type 2.
14+
15+
// Rocket distance between two points ( x2 , y2 ) and ( x1 , y1 ) is defined as (x2 - x1)2 + (y2 - y1)2
16+
17+
// Constraints
18+
// 1 < = k < = Q < = 10^5
19+
// -10^6 < = x , y < = 10^6
20+
// Output Format
21+
// For each query of type 2 output the Rocket distance of Kth nearest hostel from Origin.
22+
23+
// Sample Input
24+
// 9 3
25+
// 1 10 10
26+
// 1 9 9
27+
// 1 -8 -8
28+
// 2
29+
// 1 7 7
30+
// 2
31+
// 1 6 6
32+
// 1 5 5
33+
// 2
34+
// Sample Output
35+
// 200
36+
// 162
37+
// 98
38+
// Explanation
39+
// Here , No of queries = n = 9
40+
// k = 3
41+
42+
// We have to print the kth distance from the hotel.
43+
44+
// We are calculating and storing the rocket distance here i.e. (x2-x1)^2 + (y2-y1)^2 … basically the cartesian distance but without the squareroot.
45+
46+
// First integer of each input defines the query type. 1 means take the coordinates input and 2 means display the kth distance so far.
47+
48+
// Iteration 1 :
49+
// First we get 1 10 10
50+
// Distance = 10^2 + 10^2 = 200
51+
// We store it in our data structure. Lets call it A.
52+
// A = { 200 }
53+
54+
// Iteration 2 :
55+
// 1 9 9
56+
// Distance = 9^2 + 9^2 = 162
57+
// A = { 162 , 200 }
58+
59+
// Iteration 3 :
60+
// 1 -8 -8
61+
// Distance = (-8)^2 + (-8)^2 = 168
62+
// A = { 128, 162 , 200 }
63+
64+
// Iteration 4 :
65+
// 2
66+
// A = { 128, 162 , 200 }
67+
// Time to print the 3rd nearest distance ( k=3 )
68+
// Output : 200
69+
70+
71+
// Iteration 5 :
72+
// 1 7 7
73+
// Distance = 7^2 + 7^2 = 98
74+
// A = { 98, 128, 162 , 200 }
75+
76+
// Iteration 6 :
77+
// 2
78+
// A = { 98, 128, 162 , 200 }
79+
// Time to print the 3rd nearest distance ( k=3 )
80+
// Output : 162
81+
82+
// Iteration 7 :
83+
// 1 6 6
84+
// Distance = 6^2 + 6^2 = 72
85+
// A = { 72, 98, 128, 162 , 200 }
86+
87+
// Iteration 8 :
88+
// 1 5 5
89+
// Distance = 5^2 + 5^2 = 50
90+
// A = { 50, 72, 98, 128, 162 , 200 }
91+
92+
// Iteration 9 :
93+
// 2
94+
// A = { 50, 72, 98, 128, 162 , 200 }
95+
// Time to print the 3rd nearest distance ( k=3 )
96+
// Output : 98
97+
98+
99+
#include<bits/stdc++.h>
100+
using namespace std;
101+
102+
int main() {
103+
priority_queue<long long int> pq;
104+
int n,k;
105+
cin>>n>>k;
106+
while(n--){
107+
int q;
108+
cin>>q;
109+
if(q==1){
110+
long long int x,y;
111+
cin>>x>>y;
112+
long long int temp = x*x+y*y;
113+
if(pq.size()<k)
114+
pq.push(temp);
115+
else if(pq.top()>temp){
116+
pq.pop();
117+
pq.push(temp);
118+
}
119+
}
120+
else{
121+
cout<<pq.top()<<endl;
122+
}
123+
}
124+
return 0;
125+
}

0 commit comments

Comments
 (0)