ZOJ-2642

来源:互联网 发布:淘宝转运服务 编辑:程序博客网 时间:2024/05/22 00:34

参考1985,基本一样的算法。。就不多说了,把求和预处理一下就行

#include<stdio.h>int main(){int n, a[100000], left[100000], right[100000];long long sum[100000];while (scanf("%d", &n) != EOF){int i, temp;for (i = 0; i < n; i++){scanf("%d", &a[i]);left[i] = i;right[i] = i;sum[i] = i ? sum[i - 1] + a[i] : a[i];}for (i = 0; i < n; i++){temp = i;while (temp - 1 >= 0 && a[temp - 1] >= a[i])temp = left[temp - 1];left[i] = temp;}for (i = n - 1; i >= 0; i--){temp = i;while (temp + 1 < n && a[temp + 1] >= a[i])temp = right[temp + 1];right[i] = temp;}long long max = -1, res;int l, r;for (i = 0; i < n; i++){res = a[i] * (sum[right[i]] - (left[i] ? sum[left[i] - 1] : 0));if (res > max){max = res;l = left[i];r = right[i];}}printf("%lld\n%d %d\n", max, l + 1, r + 1);}return 0;}


0 0
原创粉丝点击