1 solutions
-
-4
考虑作为最小值,当且仅当时成立。即需要考虑数字向左/向右最多扩展到的位置。这可以考虑单调栈来完成。
单调栈完成后,假设左侧单调栈位置为,右侧为,那么应该对答案有的贡献。加上去就可以了。
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; #define ll long long int n, num[maxn], f[maxn], g[maxn]; ll ans; int main() { cin >> n; for(int i = 1;i <= n; ++i) cin >> num[i]; stack<int> s; for(int i = 1;i <= n; ++i) { while(s.size() && num[s.top()] > num[i]) s.pop(); f[i] = s.size() ? s.top() : 0; s.push(i); } while(s.size()) s.pop(); for(int i = n;i >= 1; --i) { while(s.size() && num[s.top()] > num[i]) s.pop(); g[i] = s.size() ? s.top() : (n + 1); s.push(i); } for(int i = 1;i <= n; ++i) ans += 1ll * num[i] * (i - f[i]) * (g[i] - i); cout << ans << '\n'; return 0; }
- 1
Information
- ID
- 10
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 6
- Tags
- (None)
- # Submissions
- 43
- Accepted
- 13
- Uploaded By