forked from itsyadavRajkumar/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackWindow.cpp
More file actions
63 lines (49 loc) · 1.33 KB
/
stackWindow.cpp
File metadata and controls
63 lines (49 loc) · 1.33 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
// An efficient C++ program to find
// maximum of all minimums of
// windows of different sizes
#include <bits/stdc++.h>
#include<stack>
using namespace std;
int MOD = 1000000007;
void printMaxOfMin(vector<int> &arr, int n) {
vector<int> left(n, -1), right(n, n);
// for every i find the Next smaller element to left and right
// Left
stack<int> st;
for (int i = 0; i < n; i++) {
while (st.size() && arr[i] < arr[st.top()]) st.pop();
if (st.size()) left[i] = i - st.top();
else left[i] = i + 1;
st.push(i);
}
while (st.size()) st.pop();
// Right
for (int i = n - 1; i >= 0; i--) {
while (st.size() && arr[i] <= arr[st.top()]) st.pop();
if (st.size()) right[i] = st.top() - i;
else right[i] = n - i;
st.push(i);
}
for ( auto i : right) cout << i << " ";
cout << endl;
for ( auto i : left) cout << i << " "; cout << endl;
// for(int i=0; i<n; i++) cout << left[i] << " : " << right[i] << endl;
// for each i, contribution is (Left * Right) * Element
int res = 0;
for (int i = 0; i < n; i++) {
long long prod = (left[i] * right[i]) % MOD;
prod = (prod * arr[i]) % MOD;
res = (res + prod) % MOD;
}
cout << res;
}
// Driver program
int main() {
int t , n ; cin >> t >> n ;
while ( t-- > 0 ) {
vector<int> arr( n , 0 );
for ( int i = 0 ; i < n ; i ++) cin >> arr[i];
printMaxOfMin(arr, n);
}
return 0;
}