Matrix Swapping II

本文探讨了如何通过交换矩阵的列来最大化矩阵的特性,具体涉及到计算矩阵中所有全为1矩形的最大面积。
部署运行你感兴趣的模型镜像

Matrix Swapping II

Time Limit: 9000/3000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 906Accepted Submission(s): 602


Problem Description
Given an N * M matrix with each entry equal to 0 or 1. We can find some rectangles in the matrix whose entries are all 1, and we define the maximum area of such rectangle as this matrix’s goodness.

We can swap any two columns any times, and we are to make the goodness of the matrix as large as possible.

Input
There are several test cases in the input. The first line of each test case contains two integers N and M (1 ≤ N,M ≤ 1000). Then N lines follow, each contains M numbers (0 or 1), indicating the N * M matrix

Output
Output one line for each test case, indicating the maximum possible goodness.

Sample Input
3 4 1011 1001 0001 3 4 1010 1001 0001

Sample Output
4 2 Note: Huge Input, scanf() is recommended.



和前面的求最大子1矩阵类似。只是这部不用求左右界限了,而是求出每个高度出现的次数

#include <stdio.h>
#include <string.h>
int m, n, h[1001], num[1001];
char matrix[1001];
int main() {
	//freopen("in.txt", "r", stdin);
	while(scanf("%d %d",&m, &n) != EOF) {
		getchar();
		memset(h,0,sizeof(int)*n);
		int max = 0;
		for(int i=1; i<=m; i++) {
			gets(matrix);
			memset(num,0,sizeof(int)*(i+1));
			for(int j=0; j<n; j++) {
				if(matrix[j] == '1')
				h[j]++;
				else
				h[j] = 0;
				num[h[j]]++;
				for(int k=1; k< h[j];k++) {
					num[k]++;
				}
			}
			for(int k=1; k<=i; k++) {
			if(max < num[k] * k) max = num[k] * k;
		}
	}

	printf("%d\n",max);

}
return 0;
}


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

帮我看一下我的代码对吗 def swap(permutation, i, j): """ Return a new permutation where the elements at indices i and j are swapped. Parameters: permutation (list or np.ndarray): Current assignment. i, j (int): Indices to swap. Returns: (list or np.ndarray): New permutation with i and j exchanged. """ # create a copy or modify in place to swap positions i and j new_permutation=permutation new_permutation[i]=permutation[j] new_permutation[j]=permutation[i] return new_permutation def delta_i_j(permutation, i, j, weights, distance): """ Compute the change in objective value if we swap facilities i and j. Parameters: permutation (list or np.ndarray): Current assignment. i, j (int): Indices of facilities to swap. weights (np.ndarray): Flow matrix. distance (np.ndarray): Distance matrix. Returns: float: Change in cost after swapping i and j. """ # compute the fitness difference directly or with the O(n) delta formula old_cost=fitness(permutation,weights,distance) new_permutation=swap(permutation,i,j) new_cost=fitness(new_permutation,weights,distance) return new_cost-old_cost def best_swap(weights, distance, permutation, current_fitness, best_fitness, tabu_matrix, itr): """ Identify the best neighbor of the current solution while respecting Tabu Search rules. Parameters: weights (np.ndarray) : Flow matrix of the QAP. distance (np.ndarray) : Distance matrix of the QAP. permutation (list or np.ndarray) : Current assignment of facilities to locations. current_fitness (float) : Cost of the current solution. best_fitness (float) : Best cost found so far (global best). tabu_matrix (np.ndarray) : Matrix that stores, for each possible swap, the iteration number until which it is tabu. itr (int) : Current iteration index. Returns: i, j (int): Indices of the selected swap. delta (float): Change in cost for the swap. is_best (bool): True if this move improves the global best solution. """ # iterate over all pairs (i, j) # compute delta for each swap # skip tabu moves unless aspiration criterion is met # keep track of the best candidate n=len(permutation) best_delta=float('inf') best_i, best_j = 0, 0 is_best=False for i in range(n): for j in range(i+1,n): if tabu_matrix[i][j]<itr: if current_fitness+delta_i_j(permutation,i,j,weights,distance)<best_fitness: new_permutation=swap(permutation,i,j) delta=delta_i_j(permutation,i,j,weights,distance) return i,j,delta,True else: delta=delta_i_j(permutation,i,j,weights,distance) if delta<best_delta: best_delta=delta best_i, best_j=i,j return best_i,best_j,best_delta,is_best def tabu_search(weights, distance, tabu_tenure, tmax, diversification, u): """ Main Tabu Search routine. Parameters: weights (np.ndarray): Flow between locations. distance (np.ndarray): Distance between locations. tabu_tenure (int): Number of iterations a move remains tabu. tmax (int): Total number of iterations. diversification (bool): Enable diversification strategy. u (int): Number of iterations after which diversification triggers. Returns: best_fitness (float): Best objective value found. best_permutation (list or np.ndarray): Best assignment found. fitness_history (list): Cost of each visited solution. best_history (list): Best cost at each iteration. """ # initialize permutation and tabu matrix # initialize diversification structures if enabled # repeat for tmax iterations: # choose move (best swap or diversification) # apply move and update current fitness # update best solution if improved # update tabu matrix # update diversification bookkeeping if enabled # initialize permutation and tabu matrix n = len(weights) permutation = np.random.permutation(n) # 计算当前适应度 current_fitness = fitness(permutation,weights,distance) # 初始化最佳适应度 best_fitness = current_fitness # 初始化最佳排列 best_permutation = permutation.copy() # 初始化禁忌矩阵 tabu_matrix = np.zeros((n, n), dtype=int) for itr in range(tmax): # 找到最佳交换 i, j, delta, is_best = best_swap(weights, distance, permutation, current_fitness, best_fitness, tabu_matrix, itr) # 更新排列 permutation = swap(permutation, i, j) # 更新当前适应度 current_fitness += delta # 更新禁忌矩阵 tabu_matrix[i][j] = itr + tabu_tenure tabu_matrix[j][i] = itr + tabu_tenure # 更新最佳适应度和最佳排列 if is_best: best_fitness = current_fitness best_permutation = permutation.copy() # 多样化策略 if diversification and itr % u == 0: np.random.shuffle(permutation) current_fitness = fitness(permutation,weights, distance ) return best_permutation, best_fitness, # return final best solution and histories # ------------------------------------------------ # Diversification: # implement a mechanism that forces rarely-used # swaps to be tried after u iterations # ------------------------------------------------
最新发布
10-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值