// https://oj.uz/problem/view/NOI14_orchard
#include <bits/stdc++.h>
#define int int64_t
#define coutV(x) for (const auto& e: x) {cerr << e << " ";} cerr << "\n"
using namespace std;
int N, M;
int totalApples;
vector<vector<int>> orc;
int query(int i1, int j1, int i2, int j2) {
return orc[i2][j2] + orc[i1][j1] - orc[i1][j2] - orc[i2][j1];
}
signed main() {
#ifndef __APPLE__
cin.tie(0);
ios_base::sync_with_stdio(0);
#endif
cin >> N >> M;
orc = vector<vector<int>>(N + 1, vector<int>(M + 1, 0));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> orc[i + 1][j + 1];
orc[i + 1][j + 1] += orc[i + 1][j] + orc[i][j + 1] - orc[i][j];
}
}
totalApples = orc[N][M];
#ifdef __APPLE__
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= M; j++) {
cerr << orc[i][j] << " ";
} cerr << "\n";
} cerr << "\n";
#endif
int minCost = INT64_MAX;
for (int i1 = 0; i1 < N; i1++) {
for (int j1 = 0; j1 < M; j1++) {
for (int i2 = i1 + 1; i2 <= N; i2++) {
for (int j2 = j1 + 1; j2 <= M; j2++) {
int q = query(i1, j1, i2, j2);
int cost = (totalApples + (i2 - i1) * (j2 - j1)) - 2 * q;
minCost = min(minCost, cost);
}
}
}
}
cout << minCost << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
760 KB |
Output is correct |
2 |
Correct |
186 ms |
652 KB |
Output is correct |
3 |
Correct |
185 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1074 ms |
23800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1060 ms |
3448 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
488 KB |
Output is correct |
2 |
Correct |
234 ms |
760 KB |
Output is correct |
3 |
Correct |
233 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1067 ms |
6392 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |