某个面试题
题面:
给定一个非负整数序列,定义区间的分值为=区间和*区间最小值,求出分值最大区间,及最大的分值。
思路:
把每一个数当作最小,看看延伸最左,最右在哪里
单调栈:以左边为例,单调递减
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1000; 4 char w1[N],w2[N]; 5 int l1,l2,f[N][N]; 6 int main() { 7 scanf("%d", &n); 8 for (int i = 1; i <= n; i++) { 9 scanf("%d", &a[i]); 10 } 11 d[0] = d[n + 1] = -1; 12 t = 0; 13 q[0] = 0; 14 for (i = 1; i < n; i++) { 15 while (d[q[tt]] >= d[i]) 16 t--; 17 le[i] = q[tt] + 1; 18 q[++tt] = i; 19 } 20 t = 0; 21 q[0] = n + 1; 22 for (i = n; i >= 1; i--) { 23 while (d[q[tt]] >= d[i]) 24 t--; 25 le[i] = q[tt] + 1; 26 q[++tt] = i; 27 } 28 for (int i = 1; i <= n; i++) { 29 sum[i] = sum[i - 1] + a[i]; 30 } 31 for (int i = 1; i <= n; i++) { 32 ans = max(ans, a[i] * (sum[ri[i]] - sum[le[i] - 1])); 33 } 34 printf("%d\n", ans); 35 } 36 37 }