Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 16786 | Accepted: 4627 | |
Case Time Limit: 1000MS | Special Judge |
Description
A new idea Bill has recently developed assigns a non-negative integer value to each day of human life.
Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.
Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.
Input
Output
Sample Input
6
3 1 6 4 5 2
Sample Output
60
3 5
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
using namespace std;
typedef long long LL;
const int maxn = 1e5+;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
int a[maxn],sta[maxn];
LL sum[maxn];
int lef[maxn];
int main(){
// freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n)){
ms(sum,); int top=-;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
a[n+]=-;
LL ans=-;
int L,R;
for(int i=;i<=n+;i++){
// cout<<a[i]<<endl;
if(a[i]>sta[top] || top==-){
sta[++top]=a[i];
lef[top]=i;
// printf("%d,(%d,%d)\n",a[i],i,i);
}else if(a[i]<sta[top]){
int ll;
while(a[i]<sta[top]&&top>=){
ll=lef[top];
LL res=(sum[i-]-sum[lef[top]-])*sta[top]*1LL;
if(res > ans){
ans=res;
L=lef[top],R=i-;
}
top--;
}
sta[++top]=a[i];
lef[top]=ll;
// printf("%d,(%d,%d)\n",a[i],ll,i);
}
}
cout<<ans<<endl;
cout<<L<<" "<<R<<endl;
}
return ;
}
uva上能通过的代码
#include<cstdio>
#include<cstring>
#define maxn 1000010
long long sum[maxn];
int num[maxn], l[maxn], r[maxn], n;
void init() {
sum[] = ;
memset(num,-,sizeof(num));
for(int i = ; i <= n; i++) {
scanf("%d", &num[i]);
sum[i] = sum[i-] + num[i];
l[i] = i;
r[i] = i;
} for(int i = ; i <= n; i++) {
while(num[l[i] - ] >= num[i])
l[i] = l[l[i] - ];
}
for(int i = n; i >= ; i--) {
while(num[r[i] + ] >= num[i])
r[i] = r[r[i] + ];
}
} void solve() {
long long Max = ;
int ll = , rr = ;
long long s;
for(int i = ; i <= n; i++) {
s = num[i] * (sum[r[i]] - sum[l[i] - ]);
if(s > Max || (s == Max && rr - ll > r[i] - l[i])) {
Max = s;
ll = l[i];
rr = r[i];
}
}
printf("%lld\n%d %d\n", Max, ll, rr);
} int main() {
int cas = ;
while(scanf("%d", &n) != EOF) {
if(cas)
printf("\n");
cas++;
init();
solve();
}
return ;
}