NOJ1538——[1538] K-Submartix
来源:互联网 发布:爱奇艺网络大电影 编辑:程序博客网 时间:2024/06/06 03:24
- 问题描述
- You are given a matrix consisting of some integer, its size is n*m.
You should find a minimum area submatrix that the sum of the submatrix (sum of all elements in the submatrix) is not less k. - 输入
- The input contains multiple test cases. Each test case begins with n, m (1 <= n, m <= 250) and k (-10^9 <= k <= 10^9) on line. Next n lines contain m integer each matrix . The input ends once EOF is met.
- 输出
- Print a single integer -- the area of the minimum obtained submatrix. If we cannot obtain a matrix of sum is not less k, print -1.
- 样例输入
4 4 101 2 3 45 6 7 89 10 11 1213 14 15 161 3 1510 -5 102 2 101 22 3
- 样例输出
13-1
- 提示
无
- 来源
wk@NJUPT
这题就是让你求一个满足矩阵元素和大于等于k,然后面积最小的矩阵,输出最小面积,如果不存在,就输出-1,
我的思路是,想办法把二维的问题化成一维。所以我需要一个数组sum[i][j],表示第j列从第1行到第i行的元素和。
之后我们枚举矩阵的上下界,利用前缀和的思想,另开一维数组array[],存的就是前i列的和,接下来就是去判断,然后求最小值了。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=270;
int sum[maxn][maxn];
int array[maxn];
int mat[maxn][maxn];
int main()
{
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(sum,0,sizeof(sum));
array[0]=0;
int area=0x3f3f3f3f;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&mat[i][j]);
sum[i][j]=sum[i-1][j]+mat[i][j];
}
for(int up=1;up<=n;up++)
for(int down=up;down<=n;down++)
{
for(int i=1;i<=m;i++)
{
array[i]=array[i-1]+sum[down][i]-sum[up-1][i];
if(array[i]<k)//也算是个剪枝了,之前没写直接TLE
continue;
for(int j=i-1;j>=0;j--)
if(array[i]-array[j]>=k)
{
area=min(area,(i-j)*(down-up+1));
break;
}
}
}
if(area==0x3f3f3f3f)
printf("-1\n");
else
printf("%d\n",area );
}
return 0;
}
- NOJ1538——[1538] K-Submartix
- NBUT 1538 Submartix
- k——means
- ZCMU—K
- k近邻算法——k-d 树的实现
- 算法杂货铺——k均值聚类(K-means)
- 算法杂货铺——k均值聚类(K-means)
- 算法杂货铺——k均值聚类(K-means)
- 算法杂货铺——k均值聚类(K-means)
- 算法杂货铺——k均值聚类(K-means)
- 算法杂货铺——k均值聚类(K-means)
- 算法杂货铺——k均值聚类(K-means)
- H—K——
- opencv——k-means
- 字典学习方法—K-SVD
- 贪心算法—Problem K
- 动态规划—Problem K
- 图算法—Problem K
- N!Again hdu 2674 c++
- 今年,遇到就业最难季,应该好好规划自己了
- NOJ——[1264] Flandre\'s First Escape
- POJ1679——The Unique MST
- 利用Dom读取和修改XML文件,并处理获取路径中空格(%20)问题
- NOJ1538——[1538] K-Submartix
- POJ1502——MPI Maelstrom
- NOJ1383——[1383] Palindrome String
- NOJ——[1480] 懒惰的风纪委Elaine
- BEDTools简介、安装与部分工具使用简介
- NOJ——[1143] Irrigation of Farmland
- POJ3254 状压DP入门
- NOJ——[1378] 战斗吧!皇后们!
- NOJ——[1176] Exchange Rate