UVA - 10827 Maximum sum on a torus(dp最大子矩阵和)

题目大意:
经典的最大连续和问题的变形,从一串数变成矩阵,且行列首尾相连;

解析:
1. 如何解决首尾相连的问题,可以将矩阵拓展为原来的4倍。
2. 对付矩阵,降维,将多行累加到一行;
 假设最大子矩阵的结果为从第r行到k行、从第i列到j列的子矩阵,如下所示(ari表示a[r][i],假设数组下标从1开始):
  | a11 …… a1i ……a1j ……a1n |
  | a21 …… a2i ……a2j ……a2n |
  |  .     .     .    .    |
  |  .     .     .    .    |
  | ar1 …… ari ……arj ……arn |
  |  .     .     .    .    |
  |  .     .     .    .    |
  | ak1 …… aki ……akj ……akn |
  |  .     .     .    .    |
  | an1 …… ani ……anj ……ann |

 那么我们将从第r行到第k行的每一行中相同列的加起来,可以得到一个一维数组如下:
 (ar1+……+ak1, ar2+……+ak2, ……,arn+……+akn)
由此我们可以看出最后所求的就是此一维数组的最大子断和问题,到此我们已经将问题转化为上面的已经解决了的问题了。

注意:因为原来矩阵是长度为n的,所以枚举r行到k行,那么其中r行到k行的距离不能超过n,还要求出一维数组后,要暴力求解其中长度为n的最大连续子段和,由于这两点没考虑清楚,结果一直没出来。

#include <cstdio>
#include <cstring>
using namespace std;
const int N = 80;
const int INF = 0x3f3f3f3f;
int grid[N*2][N*2];
int tot[N*2][N*2];
int res[N*2];
int n;
int maxSub(int start) {
	int max,dp;
	max = dp = res[start];
	for(int i = start+1; i < start + n; i++) {
		if(dp > 0) {
			dp += res[i];
		}else {
			dp = res[i];
		}
		if(max < dp) {
			max = dp;
		}
	}
	return max;
}
int main() {
	int t;
	scanf("%d",&t);
	while(t--) {
		memset(grid,0,sizeof(grid));
		scanf("%d",&n);
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				scanf("%d",&grid[i][j]);
				grid[n+i][j] = grid[i][n+j] = grid[n+i][n+j] = grid[i][j];
			}
		}
		int len = 2*n;
		memset(tot,0,sizeof(tot));
		for(int i = 0; i < len; i++) {
			for(int j = 0; j < len; j++) {
				if(i == 0) {
					tot[i][j] = grid[i][j];
				}else {
					tot[i][j] = tot[i-1][j] + grid[i][j];
				}
			}
		}
		int max = -INF;
		for(int i = 0; i < len; i++) {
			for(int j = i; j < i + n && j < len; j++) {
				for(int k = 0; k < len; k++) {
					if(i == 0) {
						res[k] = tot[j][k];
					}else {
						res[k] = tot[j][k] - tot[i-1][k];
					}
				}
				for(int k = 0; k < n; k++) {
					int ans = maxSub(k);
					if(max < ans) {
						max = ans;
					}
				}
			}
		}
		printf("%d\n",max);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
extern UFUNEXPORT int UF_MODL_ask_face_data( tag_t face ,/* <I> Face obj_id / int * type ,/ <O> Face type is NX surface type code 16 = cylinder 17 = cone 18 = sphere 19 = revolved (toroidal) 20 = extruded 22 = bounded plane 23 = fillet (blend) 43 = b-surface 65 = offset surface 66 = foreign surface / double point[] ,/ <O,len:3> Point information is returned according to the value of type as follows. Plane = Position in plane Cylinder= Position on axis Cone = Position on axis Sphere = Center position Torus = Center position Revolved = Position on axis / double dir[] ,/ <O,len:3> Direction information is returned according to the value of type as follows. Plane = Normal direction Cylinder= Axis direction Cone = Axis direction Torus = Axis direction Revolved = Axis direction / double box[] ,/ <O,len:6> Face boundary. The coordinates of the opposite corners of a rectangular box with sides parallel to X, Y, and Z axes (Absolute Coordinate System) are returned. The box contains the specified face and is usually close to the minimum possible size, but this is not guaranteed. box[0] = Xmin box[1] = Ymin box[2] = Zmin box[3] = Xmax box[4] = Ymax box[5] = Zmax / double * radius ,/ <O> Face major radius: For a cone, the radius is taken at the point[3] position on the axis. For a torus, the radius is taken at the major axis. / double * rad_data ,/ <O> Face minor radius: only a torus or cone has rad_data as a minor radius. For a cone, rad_data is the half angle in radians. For a torus, rad_data is taken at the minor axis. / int * norm_dir / <O> Face normal direction: +1 if the face normal is in the same direction as the surface normal (cross product of the U- and V-derivative vectors), -1 if reversed. */ ); 详解
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值