UVA 10755 Garbage Heap 最大子立方体和

点击打开链接

10755 - Garbage Heap

Time limit: 3.000 seconds

最大子立方体和比最大子矩阵多一维,同样转换为一维,然后求最值。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define inf 1ll<<60//加ll
using namespace std;
ll sum[27][27][27];
ll getsum(int x1,int x2,int y1,int y2,int z)//求从x1到x2,y1到y2在z轴上的数字之和
{
    return sum[x2][y2][z]-sum[x1-1][y2][z]-sum[x2][y1-1][z]+sum[x1-1][y1-1][z];
}
int main()
{
    int t,cas=1,flag;
    scanf("%d",&t);
    flag=t;
    while(t--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=a;i++)
            for(int j=1;j<=b;j++)
                for(int k=1;k<=c;k++)
                {
                    scanf("%lld",&sum[i][j][k]);
                    sum[i][j][k]+=sum[i-1][j][k]+sum[i][j-1][k]-sum[i-1][j-1][k];//预处理得到在k这个平面上前矩阵(i,j)的和
                }
        ll maxx=-inf;
        for(int sx=1;sx<=a;sx++)//x方向开始位置
            for(int ex=sx;ex<=a;ex++)//x方向结束位置
                for(int sy=1;sy<=b;sy++)//y方向开始位置
                    for(int ey=sy;ey<=b;ey++)//y方向结束位置
                    {
                        ll ans=0;
                        for(int sz=1,ez=1;ez<=c;ez++)//遍历z轴
                        {
                            ans+=getsum(sx,ex,sy,ey,ez);
                            maxx=max(maxx,ans);
                            while(ans<0&&sz<=ez)
                            {
                                ans-=getsum(sx,ex,sy,ey,sz);
                                sz++;
                                if(sz<=ez&&ans>maxx)maxx=ans;
                            }
                        }
                    }
        printf("%lld\n",maxx);
        if(cas!=flag)printf("\n");
        cas++;
    }
}

时间: 2024-10-15 14:31:09

UVA 10755 Garbage Heap 最大子立方体和的相关文章

UVa 10755 Garbage Heap (暴力+前缀和)

题意:有个长方体由A*B*C组成,每个废料都有一个价值,要选一个子长方体,使得价值最大. 析:我们暴力枚举上下左右边界,然后用前缀和来快速得到另一个,然后就能得到长方体,每次维护一个最小值,然后差就是最大值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #incl

UVA - 10755 Garbage Heap

题目大意:给出一个三维矩阵,求子矩阵和最大值. 解题思路:现将各个平面上的矩阵记录下矩阵和,然后可以枚举二维上的矩阵,映射成三维去做. #include <cstdio> #include <algorithm> using namespace std; int main() { int T; scanf("%d", &T); while (T--) { int A, B, C; long long tmp, s[25][25][25] = {0}; s

UVA 11637 - Garbage Remembering Exam(组合概率)

UVA 11637 - Garbage Remembering Exam 题目链接 题意:大概意思是,有n个单词,分别打乱放在一个环形的,一个非环形里面,环形的两个单词距离为顺时针逆时针的最小值,非环形的就是位置的差的绝对值,如果有一对单词,在两个里面的距离都是不大于k,那么这单词为无效单词,问平均会出现多少个无效单词 思路:组合概率,假设在非环形形成了一个随机序列,那么我们给它标号1-n,如果我们能分别算出1-n的有效概率,那么就等于算出了无效概率,那么有效概率等于和它距离大于k的那些位置的所

uva 11637 - Garbage Remembering Exam(概率)

题目链接:uva 11637 - Garbage Remembering Exam 题目大意:大白数里有很详细的题意. 解题思路:对于有序的序列来说,考虑每个位置为有效性的概率.C(2?kn?1?x)?A(2k2k)?A(n?1?xn?1?x)A(n?1n?1) x为考虑当前位置,然后与该位置距离小于等于k的位置个数.该位置有效的话,则对应的要将原先邻近的2*k个单词放到另外的位置上. #include <cstdio> #include <cstring> #include &l

(高效算法设计)之高维问题 废料堆 Garbage heap Uva 10755

#include <iostream> #include <algorithm> #define FOR(i,s,p) for(int i=(s);i<=(p);i++) using namespace std; void expand(char i, bool b[]){ b[0] = i & 1; i >>= 1; b[1] = i & 1; i >>= 1; b[2] = i & 1; } // 这里使用了二项式中的思想,

UVA - 11637 Garbage Remembering Exam (组合+可能性)

Little Tim is now a graduate,and is thinking about higher studies. However, he first needs to appear in anexam whose preparation alone includes memorizing the meanings of over 3500words! After going through the list afew times, however, he sees troub

UVA - 11637 Garbage Remembering Exam (组合+概率)

Little Tim is now a graduate,and is thinking about higher studies. However, he first needs to appear in anexam whose preparation alone includes memorizing the meanings of over 3500words! After going through the list afew times, however, he sees troub

数学&#183;概率

相关习题: Uva 11637 Garbage Remembering Exam 代码: 1 #include <algorithm> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <queue> 6 #include <map> 7 #include <set> 8 #include <ctime> 9 #inc

Advanced .NET Debugging: Managed Heap and Garbage Collection(转载,托管堆查内存碎片问题解决思路)

原文地址:http://www.informit.com/articles/article.aspx?p=1409801&seqNum=4 Debugging Managed Heap Fragmentation Earlier in the chapter, we described a phenomenon known as heap fragmentation, in which free and busy blocks are arranged and interleaved on th