http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1696

10755 - Garbage Heap

Time limit: 3.000 seconds

Garbage Heap

Time limit: 3.000 seconds

Memory limit: 64 megabytes

  Farmer John has a heap of garbage formed in a rectangular parallelepiped.

  It consists of a x b x c  garbage pieces each of which has a value. The value of a piece may be 0, if the piece is neither profitable nor harmful, and may be negative which means that the piece is not just unprofitable, but even harmful (for environment).

  The farmer thinks that he has too much harmful garbage, so he wants to decrease the heap size, leaving a rectangular nonempty parallelepiped of smaller size cut of the original heap to maximize the sum of the values of the garbage pieces in it. You have to find the optimal parallelepiped value. (Actually, if any smaller parallelepiped has value less than the original one, the farmer will leave the original parallelepiped).

Input

       The first line of the input contains the number of the test cases, which is at most 15. The descriptions of the test cases follow. The first line of a test case description contains three integers A, B, and C (1 ≤ A, B, C ≤ 20). The next lines contain  a .b .c numbers, which are the values of garbage pieces. Each number does not exceed 2 ^ 31by absolute value. If we introduce coordinates in the parallelepiped such that the cell in one corner is (1,1,1) and the cell in the opposite corner is (A,B,C), then the values are listed in the order

  The test cases are separated by blank lines.

Output

    For each test case in the input, output a single integer denoting the maximal value of the new garbage heap. Print a blank line between test cases.

Examples

Input

Output

1 2 2 2-1 2 0 -3 -2 -1 1 5

6

AC代码:

 // UVa10755 Garbage heap

 #include<cstdio>

 #include<cstring>

 #include<algorithm>

 #define FOR(i,s,t)  for(int i = (s); i <= (t); ++i)

 using namespace std;

 void expand(int i, int& b0, int& b1, int& b2) {

   b0 = i&; i >>= ;

   b1 = i&; i >>= ;

   b2 = i&;

 }

 int sign(int b0, int b1, int b2) {

   return (b0 + b1 + b2) %  ==  ?  : -;

 }

 const int maxn = ;

 const long long INF = 1LL << ;

 long long S[maxn][maxn][maxn];

 long long sum(int x1, int x2, int y1, int y2, int z1, int z2) {

   int dx = x2-x1+, dy = y2-y1+, dz = z2-z1+;

   long long s = ;

   for(int i = ; i < ; 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() {

   int T;

   scanf("%d", &T);

   while(T--) {

     int a, b, c, b0, b1, b2;

     scanf("%d%d%d", &a, &b, &c);

     memset(S, , sizeof(S));

     FOR(x,,a) FOR(y,,b) FOR(z,,c) scanf("%lld", &S[x][y][z]);

     FOR(x,,a) FOR(y,,b) FOR(z,,c) FOR(i,,){

       expand(i, b0, b1, b2);

       S[x][y][z] += S[x-b0][y-b1][z-b2] * sign(b0, b1, b2);

     }

     long long ans = -INF;

     FOR(x1,,a) FOR(x2,x1,a) FOR(y1,,b) FOR(y2,y1,b) {

       long long M = ;

       FOR(z,,c) {

         long long s = sum(x1,x2,y1,y2,,z);

         ans = max(ans, s - M);

         M = min(M, s);

       }

     }

     printf("%lld\n", ans);

     if(T) printf("\n");

   }

   return ;

 }

uva 10755 - Garbage Heap的更多相关文章

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

    题意:有个长方体由A*B*C组成,每个废料都有一个价值,要选一个子长方体,使得价值最大. 析:我们暴力枚举上下左右边界,然后用前缀和来快速得到另一个,然后就能得到长方体,每次维护一个最小值,然后差就是 ...

  2. 【降维解法:最大字段和->最大子矩阵和->最终版最大子长方体和】【UVA10755】Garbage Heap

    突然感觉刷完这一套专题后 码力有了质的飞跃,fighting 努力会有结果! 最大字段和是一个很经典的问题 O(n)算法 而对于最大子矩阵和 可以思考一个这样的想法 枚举上下边界i,j把i到j这一段的 ...

  3. 【白书训练指南】(UVa10755)Garbage Heap

    先po代码,之后把我那几个不太明了的知识点讲讲,巩固以下.三维的扫描线算法想要掌握还真是有一定的难度的. 代码 #include <iostream> #include <cstri ...

  4. uva 11673 Garbage Remembering Exam (概率)

    题目链接: http://vjudge.net/problem/viewProblem.action?id=42000 该过程为随即过程,因此总期望值等于个单词对应的期望值,即它们wasted的概率 ...

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

    Little Tim is now a graduate,and is thinking about higher studies. However, he first needs to appear ...

  6. UVA 11637 Garbage Remembering Exam

    #include <iostream> #include <stdio.h> #include <cstring> #include <math.h> ...

  7. examine self thrice a day2016

    ----------------------------2016-----------------------  12.31.2016 2016年,感恩一路帮助过我的所有人!       每个人来到世 ...

  8. JVM运行时数据区内容简述

    JVM运行时数据区分为五个部分:程序计数器.虚拟机栈.本地方法栈.堆.方法区.如下图所示,五部分其中又分为线程共享区域和线程私有区域,下面将分别介绍每一部分. 1. PC程序计数器 程序计数器是一块较 ...

  9. .NET:CLR via C#The Managed Heap and Garbage Collection

    Allocating Resources from the Managed Heap The CLR requires that all objects be allocated from the m ...

随机推荐

  1. Nginx目录别名(Alias)支持PHP的配置

    需求:通过 example.com 访问 /var/data/www,但通过 example.com/pa 访问的却是 /var/data/phpmyadmin,即保护phpmyadmin不暴露在ww ...

  2. charles 使用 技巧

    测试的是Android ,App , 在 手机wifi 网络代理设置为 电脑代理. 然后,手机访问的网络 都通过 电脑端的 charles监控!

  3. JS获取Cookie值

    function GetLoginCookie() { var userCookie = getCookie("mycookie"); var loginname = userCo ...

  4. zabbix-agent passive

    http://www.cnblogs.com/mysql-dba/p/5010902.html http://blog.chinaunix.net/uid-29155617-id-4668602.ht ...

  5. Qt 无边框窗体改变大小 完美实现(全部自己实现)

    近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...

  6. Candies---hdu3159(spfa+差分约束)

    题目链接:http://poj.org/problem?id=3159 题意:有n个小孩,m个关系格式是A B C 表示小孩 B 的糖果数最多比小孩A多C个,相当于B-A<=C; 有m个这样的关 ...

  7. linux 安装apahce的configure: error: APR not found. Please read the documentation解决办法

    1.下载所需软件包: 下载apr并配置 wget http://apache.freelamp.com/apr/apr-1.4.2.tar.gz 下载apr ./configure –prefix=/ ...

  8. 12岁的少年教你用Python做小游戏

    首页 资讯 文章 频道 资源 小组 相亲 登录 注册       首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...

  9. MVC项目实践,在三层架构下实现SportsStore-04,实现分页

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  10. php 配置本地自定义域名

    一.设置host文件 二.设置httpd.conf # Virtual hosts Include conf/extra/httpd-vhosts.conf 三.设置httpd-vhosts.conf ...