• HDU5479 Colmerauer 单调栈+暴力优化


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

    思路:

    bestcoder 84

    贡献:所有可能的子矩阵的面积和

    1 //len1:子矩阵所有长的和
    2 for(int i=1;i<=L;i++){
    3     for(int j=1;j<=R;j++){
    4         len1+=i+j-1;//-1是因为1*1单元格也是鞍点
    5     }
    6 }
     1 // #pragma comment(linker, "/STACK:102c000000,102c000000")
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <sstream>
     6 #include <string>
     7 #include <algorithm>
     8 #include <list>
     9 #include <map>
    10 #include <vector>
    11 #include <queue>
    12 #include <stack>
    13 #include <cmath>
    14 #include <cstdlib>
    15 #include <conio.h>
    16 using namespace std;
    17 #define clc(a,b) memset(a,b,sizeof(a))
    18 #define inf 0x3f3f3f3f
    19 #define lson l,mid,rt<<1
    20 #define rson mid+1,r,rt<<1|1
    21 const int N = 1010;
    22 const int M = 1e6+10;
    23 const long long MOD = 1LL<<32;
    24 #define LL long long
    25 #define LB long double
    26 #define mi() (l+r)>>1
    27 double const pi = acos(-1);
    28 const double eps = 1e-8;
    29 void fre() {
    30     freopen("in.txt","r",stdin);
    31 }
    32 
    33 int a[N][N];
    34 int l[N][N],r[N][N],u[N][N],d[N][N];
    35 stack<int>st;
    36 LL ans;
    37 int n,m;
    38 void calc(){
    39     for(int i=1;i<=n;i++)
    40         for(int j=1;j<=m;j++){
    41             int L=j-l[i][j],R=r[i][j]-j,U=i-u[i][j],D=d[i][j]-i;
    42             LL tem=(LL)L*R*U*D*(L+R)*(U+D)/4;
    43             ans=(ans+(LL)a[i][j]*tem)%MOD;
    44         }
    45     printf("%I64d
    ",ans);
    46 }
    47 
    48 int main(){
    49     int T;
    50     scanf("%d",&T);
    51     while(T--){
    52         scanf("%d%d",&n,&m);
    53         for(int i=1;i<=n;i++){
    54             for(int j=1;j<=m;j++){
    55                 scanf("%d",&a[i][j]);
    56             }
    57         }
    58         while(!st.empty()) st.pop();
    59         for(int i=1;i<=n;i++){
    60             a[i][0]=a[i][m+1]=-inf;
    61             while(!st.empty()) st.pop();st.push(0);
    62             for(int j=1;j<=m;j++){
    63                 while(!st.empty()&&a[i][st.top()]>a[i][j]) st.pop();
    64                 l[i][j]=st.top();
    65                 st.push(j);
    66             }
    67             while(!st.empty()) st.pop();st.push(m+1);
    68             for(int j=m;j>=1;j--){
    69                while(!st.empty()&&a[i][st.top()]>a[i][j]) st.pop();
    70                r[i][j]=st.top();
    71                st.push(j);
    72             }
    73         }
    74         for(int j=1;j<=m;j++){
    75             a[0][j]=a[n+1][j]=inf;
    76             while(!st.empty()) st.pop();st.push(0);
    77             for(int i=1;i<=n;i++){
    78                 while(!st.empty()&&a[st.top()][j]<a[i][j]) st.pop();
    79                 u[i][j]=st.top();
    80                 st.push(i);
    81             }
    82             while(!st.empty()) st.pop();st.push(n+1);
    83             for(int i=n;i>=1;i--){
    84                 while(!st.empty()&&a[st.top()][j]<a[i][j]) st.pop();
    85                 d[i][j]=st.top();
    86                 st.push(i);
    87             }
    88         }
    89         ans=0;
    90         calc();
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    JavaTPoint 移动开发教程【翻译完成】
    JavaTPoint 数据库教程【翻译完成】
    JavaTPoint PHP 中文教程【翻译完成】
    JavaTPoint .NET 中文教程【翻译完成】
    JavaTPoint 大数据和云计算中文教程【翻译完成】
    python基础学习笔记二:字符串相关知识
    python基础学习笔记(一):基本介绍
    python 基础学习三:数据结构对比
    python 基础学习五:关键字
    python基础学习笔记四:PEP8 Python 编码规范
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5716115.html
Copyright © 2020-2023  润新知