Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19452 | Accepted: 6267 |
Description
Input
Output
Sample Input
7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0
Sample Output
8 4000
Hint
Source
#include
#include
#include
#include
#define LL long long
#include
#define LCM(x) memset(x,-1,sizeof(x))
using namespace std;
const int N = 100000+10;
LL rec[N],Left[N],Right[N];
int main() {
int n;
while(scanf("%d",&n),n) {
// LCM(Left);
// LCM(Right); //此用法超时
for(int i=1; i<=n; i++) {
scanf("%lld",&rec[i]);
Left[i]=Right[i]=i;
}
rec[0]=rec[n+1]=-1;
for(int i=1; i<=n; i++) {
while(rec[i]<=rec[Left[i]-1]) {
Left[i]=Left[Left[i]-1];
}
}
for(int i=n; i>=1; i--) {
while(rec[i]<=rec[Right[i]+1]) {
Right[i]=Right[Right[i]+1];
}
}
LL ans,sum;
ans=-1<<30;
for(int i=1; i<=n; i++) {
sum=(Right[i]-Left[i]+1)*rec[i];
ans=max(sum,ans);
}
printf("%lld\n",ans);
}
return 0;
}
需要注意的是用数组模拟单调栈比直接用STL的栈写要快一些,这个157ms,而直接用栈跑出来是266ms。
单调栈写法:
#include
#include
#include
#include
#include
#define LL long long
using namespace std;
const int N = 100000+10;
int stack[N],top;
LL rec[N];
int main() {
int n;
while(scanf("%d",&n),n) {
for(int i=0; i=rec[i]) {
temp=(i-stack[top-1])*rec[stack[top-1]];
ans=max(temp,ans);
cnt=stack[top-1];
top--;
}
stack[top++]=cnt;
rec[cnt]=rec[i];
}
}
printf("%lld\n",ans);
}
return 0;
}