WA 求助
查看原帖
WA 求助
574613
oval_m楼主2022/2/13 23:22

如题

#include<iostream>
#include<stack>
#define ll long long
using namespace std;
ll q[100005];      //前缀和
int zuo[100005],you[100005];            //zuo[i]表示在i左边第一个比i小的数字的下标,没有就是0,you同理
int arr[100005],n;
int main()
{

    // ios::sync_with_stdio(false),cin.tie(0);
    int tt=0;
    while(cin>>n)
    {
        tt++;
        if(tt>1)cout<<"\n";
        for(int i=1;i<=n;i++)cin>>arr[i];
        q[0]=arr[0];
        for(int i=1;i<=n;i++)q[i]=q[i-1]+arr[i];
        stack<int> st;
        for(int i=1;i<=n;i++)
        {
            while(!st.empty() && arr[st.top()]>arr[i])st.pop();
            if(st.empty())zuo[i]=0;
            else zuo[i]=st.top();
            st.push(i);
        }
        while(!st.empty())st.pop();
        for(int i=n;i>0;i--)
        {
            while(!st.empty() && arr[st.top()]>arr[i])st.pop();
            if(st.empty())you[i]=n+1;
            else you[i]=st.top();
            st.push(i);
        }
        ll ans=0,tsum;
        int dr=1,dl=1,r,l;      //将区间设为无穷大或ans设为-1 以防答案就是0
        for(int i=1;i<=n;i++)
        {
            r=you[i]-1;
            l=zuo[i]+1;
            tsum=q[r]-q[l-1];
            if(ans<tsum*arr[i] || (ans==tsum*arr[i] && (r-l<dr-dl || (r-l==dr-dl && l<dl))))    //ans相等取区间小的 区间长度相等取起点靠左的
            {
                ans=tsum*arr[i];
                dr=r;dl=l;
            }
        }
        // if(dr==INT32_MAX)dr=1;
        cout<<ans<<'\n';    
        cout<<dl<<' '<<dr<<'\n';
    }
    return 0;
}
2022/2/13 23:22
加载中...