hdu1509

求最大的矩阵面积,如图所示。

http://acm.hdu.edu.cn/showproblem.php?pid=1506


比如求r[i]我们不断向右迭代,像这样

a[n+1] = -1;
        for(i = n; i >= 1; i--)

#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <deque>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;


const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const double E = 2.7182818284590452353602874713526;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;

typedef long long LL;
__int64 a[1000010], l[1000010], r[1000010];
int main()
{
    int t, n, i;
    __int64 ans;
    while(cin >> n && n)
    {
        rep(i, 1, n) scanf("%I64d",&a[i]);
        a[0] = -1;
        rep(i, 1, n)
        {
            t = i;
            while(a[t-1] >= a[i]) t = l[t-1];
            l[i] = t;
        }
        a[n+1] = -1;
        for(i = n; i >= 1; i--)
        {
            t = i;
            while(a[t+1] >= a[i]) t = r[t+1];
            r[i] = t;
        }
        ans = 0;
        //cout << l[1] << r[1] << endl;
        rep(i, 1, n)
        {
            //cout << i << " " << 1LL * a[i] * (r[i] - l[i] + 1) << endl;
            ans = max(ans, 1LL * a[i] * (r[i] - l[i] + 1));
        }
        printf("%I64d\n",ans);
        //cout << ans << endl;
    }
    return 0;
}


        {
            t = i;
            while(a[t+1] >= a[i]) t = r[t+1];
            r[i] = t;
        }

因为他右边比他大的那些对应的r值肯定他也是可以延伸到的。


提供两组数据可以思考一下。

4 2 2 5 5 结果10
6 2 5 2 5 5 2  结果12


代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值