• 连续区域最大矩形面积 POJ 1964 2082 2559 2796 3250 3494


    /*
    
    初始的想法如下:
     维护一个栈,使得:
    h[stack[0]] < h[stack[1]] < h[stack[2]] < ... < h[stack[top]]
    
    先在h[]的两端加上哨兵,h[0] = -1 , h[n+1] = -1。
    
    初始时,stack[0] = 0 , top = 0 。
    
    当计算第i个柱子的L值时,
    先从栈顶依次弹出比h[i]高或等于的柱子。
    此时,满足:(从栈的维护过程可以得知)
    h[stack[top]]=h[i]
    并且:
    h[(stack[top]+1)...(stack[top + 1])]均>=h[i]
    也就是L[i] = stack[top]+1
    
    继续维护这个栈的结构,把i入栈。
    
    对R的处理也是类似。
    
    在这个过程中,实际上求L[i]=L[L[i]-1] 加速了求取的过程!
    采用了并查集中的路径压缩
    
    在实际操作中,我们是对每一个high[i]进行枚举,L[i]保存的是以high[i]为高度时,左边可以达到的最小值 
    R[i]保存的是以high[i]为高度时,右边可以达到的最大值。 
    */
    void RectangularArea(int n)
    {
    	vector L(n+2,0);
    	vector R(n+2,0);
    	high[0] = high[n + 1] = -1;               //初始化边界,防止越界判错
    
    	for (int i = 1; i <= n; i ++)            //把L[], R[]赋值为本身
    	{ L[i] =i; R[i] = i;}
    
    	for (int i = 1; i <= n; i ++)
    		while(high[L[i] - 1] >= high[i])    //确定l[i]的最高左位置
    			L[i] = L[L[i] - 1];
    
    	for (int i = n; i >= 1; i --)
    		while (high[R[i] + 1] >= high[i])   //确定r[i]的最高右位置
    			R[i] = R[R[i] + 1];
    
    	int ans = 0;
    	for (int i = 1; i <= n; i ++)
    		ans = max(high[i] * (Sigma[R[i]] - Sigma[L[i]-1] ), ans); //得到最大的连续矩形面积(单位长度是1)
    	printf("%d\n", ans);
    }

    Welcome to my blog : www.lxlsosi.tk

     

    POJ 1964

    #include <iostream>
    #include <vector>
    using namespace std;

    /*
    这个问题可以简化成为更简单的问题,即只考虑等边长的情况!!
    在通常情况下,把条件设简单,可以得到到更好的结果。
    */
    int Sigma[1005];
    long long  high[1005];
    int n,m;
    long long ans=0;
    void RectangularArea(int n)
    {
        vector<int> L(n+2,0);
        vector<int> R(n+2,0);
        high[0] = high[n + 1] = -1;              //初始化边界,防止越界判错

        for (int i = 1; i <= n; i ++)            //把L[], R[]赋值为本身
        { L[i] =i; R[i] = i;}

        for (int i = 1; i <= n; i ++)
            while(high[L[i] - 1] >= high[i])    //确定l[i]的最高左位置
                L[i] = L[L[i] - 1];

        for (int i = n; i >= 1; i --)
            while (high[R[i] + 1] >= high[i])   //确定r[i]的最高右位置
                R[i] = R[R[i] + 1];

        for (int i = 1; i <= n; i ++)
            ans = max(high[i] * (Sigma[R[i]] - Sigma[L[i]-1] ), ans); //得到最大的连续矩形面积(单位长度是1)
    }
    int main()
    {
        //freopen("poj1964.in","r",stdin);
        int p;
        cin>>p;
        for(int l=0;l<p;l++)
        {

            cin>>m>>n;
            ans=0;
            memset(Sigma,0,sizeof(Sigma));
            memset(high,0,sizeof(high));
            for(int i=1;i<=n;i++) Sigma[i]=1+Sigma[i-1];
            for(int i=0;i<m;i++)
            {
                char temp;
                for(int i=1;i<=n;i++)
                {
                    scanf("%c",&temp);
                    cin>>temp;
                    if(temp=='F') high[i]+=1;
                    else high[i]=0;
                }
                RectangularArea(n);
            }
            cout<<3*ans<<endl;
        }
        return 0;
    }

    POJ 2082

    #include <iostream>
    #include <stack>
    #include <vector>
    using namespace std;
    struct Rect{
        int w;
        int h;
    };
    int Sigma[50005];
    int high[50005];
    int n;
    void RectangularArea(int n)
    {
        vector<int> L(n+2,0);
        vector<int> R(n+2,0);
        high[0] = high[n + 1] = -1;               //初始化边界,防止越界判错

        for (int i = 1; i <= n; i ++)            //把L[], R[]赋值为本身
        { L[i] =i; R[i] = i;}

        for (int i = 1; i <= n; i ++)
            while(high[L[i] - 1] >= high[i])    //确定l[i]的最高左位置
                L[i] = L[L[i] - 1];

        for (int i = n; i >= 1; i --)
            while (high[R[i] + 1] >= high[i])   //确定r[i]的最高右位置
                R[i] = R[R[i] + 1];

        int ans = 0;
        for (int i = 1; i <= n; i ++)
            ans = max(high[i] * (Sigma[R[i]] - Sigma[L[i]-1] ), ans); //得到最大的连续矩形面积(单位长度是1)
        printf("%d\n", ans);
    }
    int main()
    {
        //  freopen("poj2082.in","r",stdin);
        while(cin>>n && n!=-1)
        {
            vector<Rect> C(n);
            for(int i=0;i<n;i++){
                scanf("%d%d",&C[i].w,&C[i].h);
            }
            // 初始化Sigma 和 high
            memset(Sigma,0,sizeof(Sigma));
            memset(high,0,sizeof(high));
            for(int i=1;i<=n;i++)
            {
                Sigma[i]=C[i-1].w+Sigma[i-1];
                high[i]=C[i-1].h;
            }
            RectangularArea(C.size());
        }
        return 0;
    }

    POJ 2559

    #include <iostream>
    #include <vector>
    using namespace std;

    /*
    这个问题可以简化成为更简单的问题,即只考虑等边长的情况!!
    在通常情况下,把条件设简单,可以回得到更好的结果。
    */
    int Sigma[100005];
    long long  high[100005];
    int n;
    void RectangularArea(int n)
    {
        vector<int> L(n+2,0);
        vector<int> R(n+2,0);
        high[0] = high[n + 1] = -1;              //初始化边界,防止越界判错

        for (int i = 1; i <= n; i ++)            //把L[], R[]赋值为本身
        { L[i] =i; R[i] = i;}

        for (int i = 1; i <= n; i ++)
            while(high[L[i] - 1] >= high[i])    //确定l[i]的最高左位置
                L[i] = L[L[i] - 1];

        for (int i = n; i >= 1; i --)
            while (high[R[i] + 1] >= high[i])   //确定r[i]的最高右位置
                R[i] = R[R[i] + 1];

        long long  ans = 0;
        for (int i = 1; i <= n; i ++)
            ans = max(high[i] * (Sigma[R[i]] - Sigma[L[i]-1] ), ans); //得到最大的连续矩形面积(单位长度是1)
        cout<<ans<<endl;
        //    printf("%d\n", ans);
    }
    int main()
    {
        //freopen("poj2559.in","r",stdin);
        while(cin>>n && n!=0)
        {
            // 初始化Sigma 和 high
            memset(Sigma,0,sizeof(Sigma));
            memset(high,0,sizeof(high));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&high[i]);
                Sigma[i]=1+Sigma[i-1];
            }
            RectangularArea(n);
        }
        return 0;
    }

    POJ 2796

    #include <iostream>
    #include <vector>
    using namespace std;

    /*
    测试数据竟然有一组是1 0 。。。。太无耻了!!
    */
    long long Sigma[100005];
    long long  high[100005];
    int n;
    int l,r;
    long long  ans=0;
    void RectangularArea(int n)
    {
        vector<int> L(n+2,0);
        vector<int> R(n+2,0);
        high[0] = high[n + 1] = -1;              //初始化边界,防止越界判错

        for (int i = 1; i <= n; i ++)            //把L[], R[]赋值为本身
        { L[i] =i; R[i] = i;}

        for (int i = 1; i <= n; i ++)
            while(high[L[i] - 1] >= high[i])    //确定l[i]的最高左位置
                L[i] = L[L[i] - 1];

        for (int i = n; i >= 1; i --)
            while (high[R[i] + 1] >= high[i])   //确定r[i]的最高右位置
                R[i] = R[R[i] + 1];

        for (int i = 1; i <= n; i ++)
        {
            if(high[i] * (Sigma[R[i]] - Sigma[L[i]-1] )>ans)
            {
                l=L[i];
                r=R[i];
            }
            ans = max( high[i]*(Sigma[R[i]] - Sigma[L[i]-1]) , ans); //得到最大的连续矩形面积(单位长度是1)
        }
    }
    int main()
    {
        //freopen("poj2796.in","r",stdin);
        while(cin>>n)
        {
            l=1;r=1;
            // 初始化Sigma 和 high
            ans=0;
            memset(Sigma,0,sizeof(Sigma));
            memset(high,0,sizeof(high));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&high[i]);
                Sigma[i]=Sigma[i-1]+high[i];
            }
            RectangularArea(n);
            cout<<ans<<endl;
            cout<<l<<" "<<r<<endl;
        }
        return 0;
    }

    POJ 3250

    #include <iostream>
    #include <vector>
    using namespace std;

    long long num[80005];
    long long  high[80005];
    int n;
    long long  ans=0;
    #define INF 1000000005
    void RectangularArea(int n)
    {
        vector<int> L(n+2,0);
        high[0]=INF; high[n + 1] = INF+1;             
        for (int i = 1; i <= n; i ++)                
        { L[i] =i-1; num[i]=0;}

        for (int i = 1; i <= n; i ++)
        {
             if( high[i]<high[L[i]] ) { num[i]+=num[L[i]]+1; }
            else{
                while( high[i] >= high[L[i]])        
                    L[i] = L[L[i]];
                num[i]+=num[L[i]]+1;
            }
        }
        for (int i = 1; i <= n; i ++)
        {
            ans += num[i];
        }
    }
    int main()
    {
        //freopen("poj3250.in","r",stdin);
        cin>>n;

        ans=0;
        memset(high,0,sizeof(high));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&high[i]);
        }
        RectangularArea(n);
        cout<<ans-n<<endl;

        return 0;
    }

    POJ 3494

    #include <iostream>
    #include <vector>
    using namespace std;

    /*
    这个问题可以简化成为更简单的问题,即只考虑等边长的情况!!
    在通常情况下,把条件设简单,可以回得到更好的结果。
    */
    int Sigma[2005];
    long long  high[2005];
    int n,m;
    long long ans=0;
    void RectangularArea(int n)
    {
        vector<int> L(n+2,0);
        vector<int> R(n+2,0);
        high[0] = high[n + 1] = -1;              //初始化边界,防止越界判错

        for (int i = 1; i <= n; i ++)            //把L[], R[]赋值为本身
        { L[i] =i; R[i] = i;}

        for (int i = 1; i <= n; i ++)
            while(high[L[i] - 1] >= high[i])    //确定l[i]的最高左位置
                L[i] = L[L[i] - 1];

        for (int i = n; i >= 1; i --)
            while (high[R[i] + 1] >= high[i])   //确定r[i]的最高右位置
                R[i] = R[R[i] + 1];

        for (int i = 1; i <= n; i ++)
            ans = max(high[i] * (Sigma[R[i]] - Sigma[L[i]-1] ), ans); //得到最大的连续矩形面积(单位长度是1)
    }
    int main()
    {
        //freopen("poj3494.in","r",stdin);

        while(cin>>m>>n)
        {
            ans=0;
            memset(Sigma,0,sizeof(Sigma));
            memset(high,0,sizeof(high));
            for(int i=1;i<=n;i++) Sigma[i]=1+Sigma[i-1];
            for(int i=0;i<m;i++)
            {
                int temp;
                for(int i=1;i<=n;i++)
                {
                    scanf("%d",&temp);
                    if(temp==1) high[i]+=1;
                    else high[i]=0;
                }
                RectangularArea(n);
            }
            cout<<ans<<endl;
        }
        return 0;
    }

  • 相关阅读:
    Should I expose synchronous wrappers for asynchronous methods?
    .NET Memory Allocation Profiling with Visual Studio 2012
    Should I expose asynchronous wrappers for synchronous methods?
    Patterns for Asynchronous MVVM Applications: Commands
    WPF/SL: lazy loading TreeView
    Reusable async validation for WPF with Prism 5
    Entity Framework Code First
    ConsoleHelper 类
    [Forward]Sweeping the IDisposable minefield
    Enums and Lookup Tables with EF Code First
  • 原文地址:https://www.cnblogs.com/sosi/p/2063296.html
Copyright © 2020-2023  润新知