-
Notifications
You must be signed in to change notification settings - Fork 30
/
coursera w3.3.cpp
62 lines (54 loc) · 1.19 KB
/
coursera w3.3.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
#include <iostream>
#include <vector>
using namespace std;
using std::vector;
using std::max;
int compute_min_refills(int dist, int tank, vector<int> stops) {
// write your code here
int prev=0;
int k=tank,p=0,i;
int q=stops.size()+1;
stops[q]=dist;
for(i=0;i<q-1;i++)
{
if(tank<stops[i+1]-stops[i])
{
return -1;
}
}
for(int j=0;j<q+1;j++)
{
if(tank<(stops[j]-prev)&&(stops[q]-prev)>=tank)
{
prev=stops[j-1];
p=p+1;
}
else if(tank==(stops[j]-prev)&&(stops[q]-prev)>=tank)
{
prev=stops[j];
p=p+1;
}
if(j!=0)
{
dist=dist-(stops[j]-stops[j-1]);
}
else
{
dist=dist-stops[j];
}
}
return p;
}
int main() {
int d = 0;
cin >> d;
int m = 0;
cin >> m;
int n = 0;
cin >> n;
vector<int> stops(n);
for (size_t i = 0; i < n; ++i)
cin >> stops.at(i);
cout << compute_min_refills(d, m, stops) << "\n";
return 0;
}