uva10755 - Garbage Heap (最大子立方体)

本文提供了一份UVA问题解决的C++实现代码,利用三维数组进行前缀和计算,并通过迭代来更新每个位置的值。该算法适用于解决特定类型的问题,如计算三维空间内特定区域的和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***********************************************
 * Author: fisty
 * Created Time: 2015/1/25 15:49:25
 * File Name   : uva.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define FOR(i, s, t)  for(int i = (s); i <= (t); ++i)
#define MAX_N 30
const long long  INF = 1LL << 60;
typedef long long LL;
typedef pair<int, int> P;
LL S[MAX_N][MAX_N][MAX_N];

void expand(int i, int &b0, int &b1, int &b2){
    b0 = i&1;i >>= 1;
    b1 = i&1;i >>= 1; 
    b2 = i&1;
}

int sign(int b0, int b1, int b2){
    //和是奇数返回1
    return (b0 + b1 + b2) % 2 == 1 ? 1 : -1;
}

LL sum(int x1, int x2, int y1, int y2, int z1, int z2){
    int dx = x2 - x1 + 1;
    int dy = y2 - y1 + 1;
    int dz = z2 - z1 + 1;
    LL s = 0;
    for(int i = 0;i < 8; i++){
        int b0, b1, b2;
        expand(i, b0, b1, b2);
        s -= S[x2-b0*dx][y2-b1*dy][z2-b2*dz] * sign(b0, b1, b2);
    }
    return s;
}
int main() {
    //freopen("in.txt", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        int a, b, c, b0, b1, b2;
        cin >> a >> b >> c;
        memset(S, 0, sizeof(S));
        FOR(x, 1, a) FOR(y, 1, b) FOR(z, 1, c) cin >> S[x][y][z];
        FOR(x, 1, a) FOR(y, 1, b) FOR(z, 1, c) FOR(i, 1, 7){
            expand(i, b0, b1, b2);
            //Debug(b0);
            //Debug(b1);
            //Debug(b2);
            S[x][y][z] = S[x][y][z] + S[x-b0][y-b1][z-b2] * sign(b0,b1,b2);
        }
        LL ans = -INF;
        FOR(x1, 1, a) FOR(x2, x1, a) FOR(y1, 1, b) FOR(y2, y1, b){
            LL M = 0;
            FOR(z, 1, c){
                LL s = sum(x1,x2,y1,y2,1,z);        
                ans = max(ans, s - M);
                M = min(M, s);
            }
        }
        cout << ans << endl;
        if(t)
            cout << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值