-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path03_using_vector.cpp
101 lines (80 loc) · 2.17 KB
/
03_using_vector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
TOPIC: Using Vector (with Pair)
Car Sorting Problem
--------------------
We want to book a cab.
Assume that we are standing at origin(0,0) &
we are provides the coordinates (xi,yj) of each cabs as input.
So, we have to sort the cab on the basis of distance from our location (i.e (0,0)).
^
| c1
c4 | (x1,y1)
(x4,y4) | c2
| (x2,y2)
<---------------|---------------->
(0,0)|
| c3
| (x3,y3)
|
v
Approach-I : Using [vector + pair]
Approach-II: Using [vector + custom class]
Lets's solve the above problem using Approach-I
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
bool compare(pair<int,int> p1, pair<int,int> p2)
{
/* The 2D distance between two points A(x₁, y₁) & B((x₂, y₂)) is √[(x₂ - x₁)² + (y₂ - y₁)²]
when, (x₁, y₁) = (0,0)
then, Distance : √[(x₂)² + (y₂)²]
*/
int distance1 = sqrt(p1.first*p1.first + p1.second*p1.second);
int distance2 = sqrt(p2.first*p2.first + p2.second*p2.second);
// If two cabs have same distance, then sort accourding to x coordinates.
if(distance1 == distance2)
{
return p1.first < p2.first;
}
return distance1 < distance2;
}
int main()
{
int n;
cout << "Enter total cabs: ";
cin >> n;
vector<pair<int,int>> cabs;
cout << "Enter cabs coordinates (xi,yj): ";
for(int i=0; i<n; i++)
{
int x,y;
cin >> x >> y;
cabs.push_back(make_pair(x,y));
}
// sort cabs according to distance
sort(cabs.begin(),cabs.end(), compare);
// for each loop
cout << "Cabs [Sorted Order]: \n";
for(auto x:cabs)
{
cout << x.first << " " << x.second << endl;
}
return 0;
}
/*
OUTPUT:
Enter total cabs: 4
Enter cabs coordinates (xi,yj):
1 2
2 2
1 0
3 4
Cabs [Sorted Order]:
1 0
1 2
2 2
3 4
*/