-
Notifications
You must be signed in to change notification settings - Fork 1
/
wtc_ms.cpp
62 lines (55 loc) · 1.37 KB
/
wtc_ms.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
#define NOTHING_HERE -1
#include <iostream>
#include <stack>
using namespace std;
/*
5 #
4 # #
3 ####
2 ######
1 #######
i 1234567
k 1214111
*/
/// @brief
/// @param arr array with towers [1..n]
/// @param size elements count n
/// @param results array [1..n] with k's, when arr[i] = max(arr[i-results[i]+1], ... arr[i])
/// @return
int *katastrofa(int *arr, int size)
{
stack<int> indexes;
// if all towers are lower than current, when
indexes.push(NOTHING_HERE);
int *results = (int *)malloc((size_t)(size + 1) * sizeof(int));
for (int i = 1; i <= size; i++)
{
// remove all lower / same towers, because there are irrelevants
while (indexes.top() != NOTHING_HERE && arr[indexes.top()] <= arr[i])
indexes.pop();
// because indexes.top() is the index of first higher than arr[i] tower
// if arr[i] is the highest yet then
if (indexes.top() == NOTHING_HERE)
results[i] = i;
else
results[i] = i - indexes.top();
// add current tower
indexes.push(i);
}
return results;
}
int main()
{
int n;
cin>>n;
int* arr = (int*)malloc((size_t)(n+1)*sizeof(int));
for(int i = 1; i<=n; i++){
cin>>arr[i];
}
int* results = katastrofa(arr,n);
for(int i = 1; i<=n; i++){
cout<<results[i]<<endl;
}
free(arr);
free(results);
}