-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhinh_chu_nhat_lon_nhat.cpp
More file actions
74 lines (74 loc) · 1.37 KB
/
hinh_chu_nhat_lon_nhat.cpp
File metadata and controls
74 lines (74 loc) · 1.37 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
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <string>
using namespace std;
long long mod = 1e9 + 7;
#define ll long long
void slove()
{
ll n;
cin >> n;
vector<ll> a(n);
for (auto &x : a)
cin >> x;
stack<ll> stk;
vector<ll> l(n), r(n);
for (ll i = 0; i < n; i++)
{
if (stk.empty())
{
stk.push(i);
continue;
}
while (!stk.empty() && a[i] < a[stk.top()])
{
r[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
while (!stk.empty())
{
r[stk.top()] = n;
stk.pop();
}
for (ll i = n - 1; i >= 0; i--)
{
if (stk.empty())
{
stk.push(i);
continue;
}
while (!stk.empty() && a[i] < a[stk.top()])
{
l[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
while (!stk.empty())
{
l[stk.top()] = -1;
stk.pop();
}
ll res = -1;
for (ll i = 0; i < n; i++)
res = max(res, a[i] * (r[i] - l[i] - 1));
cout << res << "\n";
}
int main()
{
int t = 1;
cin >> t;
while (t--)
{
slove();
}
return 0;
}