SJTU 机试 最小面积子矩阵 压缩+双指针
链接:https://www.nowcoder.com/questionTerminal/8ef506fbab2742809564e1a288358554
来源:牛客网
一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵(矩阵中元素个数为矩阵面积)
输入描述:
每个案例第一行三个正整数N,M<=100,表示矩阵大小,和一个整数K 接下来N行,每行M个数,表示矩阵每个元素的值
输出描述:
输出最小面积的值。如果出现任意矩阵的和都小于K,直接输出-1。
示例1
输入
4 4 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
输出
1
和NOIp 最大加权矩阵一个套路;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 200005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-5 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ int n, m, K; int a[200][200]; bool fg; int pre[maxn]; int dp[maxn]; int minn = inf; void sol() { for (int i = 1; i <= n; i++) { ms(dp); for (int j = i; j <= n; j++) { ms(pre); for (int k = 1; k <= m; k++) dp[k] += a[j][k]; for (int k = 1; k <= m; k++)pre[k] = pre[k - 1] + dp[k]; int l = 1, r = 1; while (1) { if (r > m)break; while (pre[r] - pre[l - 1] < K&&r <= m)r++; if (pre[r] - pre[l - 1] >= K&&r<=m) { minn = min(minn, (j - i + 1)*(r - l + 1)); fg = 1; } while (pre[r] - pre[l - 1] >= K && l <= r) { minn = min(minn, (r - l + 1)*(j - i + 1)); l++; fg = 1; } } } } } int main() { //ios::sync_with_stdio(0); rdint(n); rdint(m); rdint(K); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++)rdint(a[i][j]); } sol(); if (fg == 0)cout << -1 << endl; else { cout << minn << endl; } return 0; }
EPFL - Fighting