思路:二维矩阵前缀和,暴力枚举最小值
cpp
#include <bits/stdc++.h>
using namespace std;
const int M = 110;
int g[M][M];
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
cin >> g[i][j];
g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];
}
}
int res = INT_MAX;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
for(int x = i; x <= n; x++){
for(int y = j; y <= m; y++) {
int sum = g[x][y] - g[i - 1][y] - g[x][j - 1] + g[i - 1][j - 1];
if(sum >= k) {
res = min(res, (x - i + 1) * (y - j + 1));
}
}
}
}
}
if(res == INT_MAX) {
cout << -1 << endl;
}else {
cout << res << endl;
}
return 0;
}
cpp
#include <iostream>
using namespace std;
const int N = 60;
int nums[N];
int dp[N][2];
int main() {
int n = 0;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> nums[i];
}
for(int i = 1;i <= n; i++){
dp[i][0] = 1;
dp[i][1] = 1;
for(int j = 1;j < i; j++){
if(nums[i] > nums[j]) dp[i][1] = max(dp[i][1], dp[j][0]+1);
if(nums[i] < nums[j]) dp[i][0] = max(dp[i][0], dp[j][1]+1);
}
}
cout << max(dp[n][0], dp[n][1]) << endl;
return 0;
}