• POJ2559/HDU1506 Largest Rectangle in a Histogram (cartesian tree)


    Die datenstruktur ist erataunlich!

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    
    #else
    
    #define D_e_Line ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 100007;
    
    int n;
    
    struct node{
        int val, pri, fa, l, r;
    
        bool operator < (const node &com)const{
    		return val < com.val;
    	}
    }t[N];
    
    int root;
    long long ans;
    
    inline int Build(){
    	R(i,1,n){
    		int k;
    		for(k = i - 1; t[k].pri > t[i].pri; k = t[k].fa);
    		t[i].l = t[k].r;
    		t[k].r = i;
    		t[i].fa = k;
    		t[t[i].l].fa = i;
    	}
    	return t[0].r;
    }
    
    inline long long DFS(int x){
    	if(!x) return 0;
    	long long width = DFS(t[x].l) + DFS(t[x].r) + 1;
    	long long tmp = t[x].pri * width;
    	ans = Max(ans, tmp);
    	return width;
    }
    int main(){
        while(scanf("%d", &n) && n){
        	t[0].l = t[0].r = t[0].fa = t[0].pri = t[0].val = 0;
            R(i,1,n){
            	t[i].val = i;
                io >> t[i].pri;
                t[i].l = t[i].r = t[i].fa = 0;
            }
            
            root = Build();
            
            ans = 0;
            
            DFS(root);
            
            printf("%lld
    ", ans);
        }
        return 0;
    }
    

  • 相关阅读:
    团队-科学计算器-模块测试过程
    结对-贪吃蛇-最终程序
    课后作业-阅读任务-阅读提问-3
    结对-贪吃蛇游戏-测试过程
    《团队-科学计算器-团队一阶段互评》
    结对-贪吃蛇项目-结对项目总结
    团队-科学计算器-开发文档
    《团队-科学计算器-模块测试过程》
    《团队-科学计算器-模块开发过程》
    结对-贪吃蛇-最终程序
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11202007.html
Copyright © 2020-2023  润新知