Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.2 kB
2
Indexable
Never
#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
#define se second
#define fi first
using namespace std;

int dx[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int dy[8] = { 0, 0, 1, -1, 1, -1, -1, 1 };

/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

typedef tree <
    pair<int ,int>,
    null_type,
    less<pair<int ,int>>,
    rb_tree_tag,
    tree_order_statistics_node_update> ordered_set;
*/

const int mod = 1e9+7;
const int N = 5e3+10;

ll ar[N];
ll n,m,ii,k;
ll ST[N][20];
ll Jump_LOG[N];
ll idxl[N], idxr[N];

void Build_Sparse()
{
	for(int i=1;i<=n;i++)ST[i][0]=ar[i];

	for(int i=2;i<=n;i++)Jump_LOG[i]=Jump_LOG[i-1]+!(i&(i-1));

	for(int j=1;(1<<j)<=n;j++)
	{
		for(int i=1;(i+(1<<j)-1)<=n;i++)
		{
           ST[i][j]=max(ST[i][j-1],ST[i+(1<<(j-1))][j-1]);
		}
	}
}

ll query(int i,int j)
{
	ll boro_lav=Jump_LOG[j-i+1];
	return min(ST[i][boro_lav],ST[j-(1<<boro_lav)+1][boro_lav]);
}

void solve(int tc)
{
    ll i,j;

    cin>>n;

    stack<pair<ll, ll>>st;

    for(i=1;i<=n;i++)
    {
        cin>>ar[i];

        while(!st.empty() && st.top().fi<=ar[i])
        {
            st.pop();
        }
        if(st.empty())
        {
            idxl[i] = i;
        }
        else
        {
            idxl[i] = st.top().se;
        }
        st.push({ar[i], i});
    }

    while(!st.empty()) st.pop();

    for(i=n;i>=1;i--)
    {
        while(!st.empty() && st.top().fi<=ar[i])
        {
            st.pop();
        }
        if(st.empty())
        {
            idxr[i] = i;
        }
        else
        {
            idxr[i] = st.top().se;
        }
        st.push({ar[i], i});
    }

    Build_Sparse();

    ll l, r, mid, left, right, ans = 0, len, cur, take;

    for(i=1;i<=n;i++)
    {
        len = i-idxl[i]+idxr[i]-i;

        cur = ar[idxl[i]]-ar[i];

        l = 1, r = idxl[i]-1;

        take = -1;

        while(l<=r)
        {
            mid = (l+r)>>1;

            if(query(1, mid)>ar[i])
            {
                l = mid+1;
            }
            else
            {
                r = mid-1;

                take = mid;
            }
        }
        if(take!=-1)
        {
            len += idxl[i]-take;
        }

        ans = max(ans, cur*len);


        //cout<<i<<' '<<cur<<' '<<len<<"\n";



        len = idxr[i]-i+i-idxl[i];

        cur = ar[idxr[i]]-ar[i];

        l = idxr[i]+1, r = n;

        take = -1;

        while(l<=r)
        {
            mid = (l+r)>>1;

            if(query(idxr[i]+1, mid)>ar[i])
            {
                r = mid-1;
            }
            else
            {
                l = mid+1;

                take = mid;
            }
        }
        if(take!=-1)
        {
            len += take-idxr[i];
        }

        ans = max(ans, cur*len);

    }

    cout<<"Case "<<tc<<": "<<ans<<"\n";
}
int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	//pre();

	int tc = 1;
	cin>>tc;

	for(int tt=1;tt<=tc;tt++)
    {
        solve(tt);
    }
	return 0;
}