当前位置: 代码迷 >> 综合 >> ACM Plan UVa -10755 Garbage Heap (三维子序列求最大和)
  详细解决方案

ACM Plan UVa -10755 Garbage Heap (三维子序列求最大和)

热度:55   发布时间:2023-10-15 12:34:25.0

AC代码

#include <cstdio>
#include <cstring>
long long a[21][21][21];
int main()
{
    // freopen("i.txt", "r", stdin);// freopen("o.txt", "w", stdout);int t; scanf("%d", &t);while(t--){
    int A, B, C; scanf("%d%d%d", &A, &B, &C);memset(a, 0, sizeof(a));for(int i = 1; i <= A; i++)for(int j = 1; j <= B; j++)for(int k = 1; k <= C; k++){
    scanf("%lld", &a[i][j][k]);a[i][j][k] += a[i - 1][j][k] + a[i][j - 1][k] + a[i][j][k - 1]- a[i - 1][j - 1][k] - a[i - 1][j][k - 1] - a[i][j - 1][k - 1]+ a[i - 1][j - 1][k - 1]; }long long max = a[1][1][1];for(int i = 1; i <= A; i++)for(int j = 1; j <= B; j++)for(int k = 1; k <= C; k++)for(int i2 = i; i2 <= A; i2++)for(int j2 = j; j2 <= B; j2++)for(int k2 = k; k2 <= C; k2++){
    long long sum = a[i2][j2][k2] - a[i - 1][j2][k2] - a[i2][j - 1][k2] - a[i2][j2][k - 1]+ a[i - 1][j - 1][k2] + a[i - 1][j2][k - 1] + a[i2][j - 1][k - 1]- a[i - 1][j - 1][k - 1];max = sum > max ? sum : max;}printf("%lld\n", max);if(t) putchar('\n');}
}
  相关解决方案