poj 2082Terrible Sets(单调栈)

    xiaoxiao2021-04-12  35

    #include<cstdio> #define MAX_N 50050 #define MAX(x,y) ((x)>(y)?(x):(y)) using namespace std; int stack[MAX_N],l[MAX_N],r[MAX_N],d[MAX_N],w[MAX_N]; int n; int cal() { int tot=0; for(int i=0;i<n;i++) { while(tot>0&&d[stack[tot-1]]>=d[i]) tot--; l[i]=tot==0?0:(stack[tot-1]+1); stack[tot++]=i; } tot=0; for(int i=n-1;i>=0;i--) { while(tot>0&&d[stack[tot-1]]>=d[i]) tot--; r[i]=tot==0?n:stack[tot-1]; stack[tot++]=i; } long long res=0; for(int i=0;i<n;i++) res=MAX(res,d[i]*(w[r[i]]-w[l[i]])); return res; } int main() { while(~scanf("%d",&n)&&(n!=-1)) { int temp; w[0]=0; for(int i=0;i<n;i++) { scanf("%d%d",&temp,&d[i]); w[i+1]=w[i]+temp; } printf("%d\n",cal()); } }
    转载请注明原文地址: https://ju.6miu.com/read-667365.html

    最新回复(0)