-
Notifications
You must be signed in to change notification settings - Fork 80
/
PoisonousPlants.cpp
62 lines (49 loc) · 1.56 KB
/
PoisonousPlants.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 <bits/stdtr1c++.h>
#define MAX 100010
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl
using namespace std;
typedef pair<int, int> Pair;
typedef pair<int, Pair> dPair;
set <dPair> S;
vector <Pair> v;
int n, ar[MAX], R[MAX];
int main(){
int i, j, k, l, r, c, d, x, y;
while (scanf("%d", &n) != EOF){
for (i = 0; i < n; i++) scanf("%d", &ar[i]);
S.clear();
for (i = 0; i < n; i++) R[i] = i + 1;
for (i = 0; (i + 1) < n; i++){
S.insert(dPair(ar[i] - ar[i + 1], Pair(i, i + 1)));
}
int counter = 0;
while (!S.empty()){
dPair cur = *(S.begin());
if (cur.first >= 0) break;
counter++;
v.clear();
for (auto it: S){
if (it.first >= 0) break;
l = (it.second).first;
r = (it.second).second;
v.push_back(Pair(r, l));
}
sort(v.begin(), v.end());
int len = v.size();
for (i = len - 1; i >= 0; i--){
l = v[i].second, r = v[i].first;
S.erase(dPair(ar[l] - ar[r], Pair(l, r)));
int next_r = R[r];
if (next_r != n){
S.erase(dPair(ar[r] - ar[next_r], Pair(r, next_r)));
S.insert(dPair(ar[l] - ar[next_r], Pair(l, next_r)));
}
R[l] = next_r;
}
}
printf("%d\n", counter);
}
return 0;
}