-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPickup_Chicks.cpp
More file actions
64 lines (50 loc) · 1.6 KB
/
Copy pathPickup_Chicks.cpp
File metadata and controls
64 lines (50 loc) · 1.6 KB
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int time, chicks, k, barn;
cout << "Enter total number of chicks and chicks needed affter the barn : ";
cin >> chicks;
cout << "Enter total number of chicks needed affter the barn : ";
cin >> k;
cout << "Enter time and coordinate of barn : ";
cin >> time;
cout << "Enter the coordinate of barn : ";
cin >> barn;
vector<int> velocity;
vector<int> distance;
cout << "Enter the coordinates of chicks and its velocity: ";
for (int i = 0; i < chicks; i++)
{
int x, v;
cin >> x >> v;
distance.push_back(x);
velocity.push_back(v);
}
// count denotes number of chicks that can reach the barn, swaps denotes total swaps needed.
// not_possible are the number of chicks that cannot reach the barn.
int count = 0, swaps = 0, not_possible = 0;
for (int i = chicks - 1; i >= 0; i--)
{
// Distance needed by current chick to cross the barn.
int distance_needed = barn - distance[i];
// Maximum distance the chick can travel in the given time.
int max_distance = velocity[i] * time;
if (distance_needed <= max_distance)
{
count++;
// Important STEP**
if (not_possible > 0)
swaps += not_possible;
}
else
not_possible++;
if (count >= k)
break;
}
if (count >= k)
cout << "Total swaps needed is : " << swaps;
else
cout << "It is not possible for " << k << " chicks to cross the barn.";
return 0;
}