-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgasStation.cpp
97 lines (84 loc) · 1.72 KB
/
gasStation.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <climits>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("gasStation.in");
ofstream fout("gasStation.out");
struct gasStation
{
int pos;
int rad;
bool operator<(const gasStation &temp)
{
return pos + rad < temp.pos + temp.rad;
}
};
int station(int wantPos, vector<gasStation> a)
{
int size = a.size();
for (int i = size - 1; i >= 0; --i)
{
if (a[i].pos - a[i].rad <= wantPos)
{
return i;
}
}
return -1;
}
int main()
{
while (true)
{
int l = 0, g;
fin >> l >> g;
if (l == 0)
{
break;
}
vector<gasStation> a(g);
for (int i = 0; i <= g - 1; ++i)
{
fin >> a[i].pos >> a[i].rad;
}
sort(a.begin(), a.end());
int nowPos = 0, ans = 0, last = -1;
while (nowPos < l)
{
int res = station(nowPos, a);
if (res == last)
{
ans = -1;
break;
}
else if (res == -1)
{
ans = -1;
break;
}
else
{
last = res;
nowPos = a[res].pos + a[res].rad;
++ans;
}
}
if (ans == -1)
{
fout << "-1\n";
}
else
{
fout << g - ans << '\n';
}
}
return 0;
}