十年OI一场空,不开long long 见祖宗。
#include <iostream> #include <queue> #include <map> #include <set> #include <cmath> #include <cstring> #include <algorithm> #include <iomanip> #include <stack> #include <numeric> #include <ctime> #include <string> #include <bitset> #include <unordered_map> #include <unordered_set> using namespace std; using ll = long long; const ll N = 1e3 + 5, mod = 1e9 + 7, inf = 2e18; int n, m, q; ll a[N][N]; void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } } //遍历行 ll ans = inf; for (int i = 1; i < m; i++) { ll x = a[n][i]; ll y = a[n][m] - x; ans = min(ans, abs(x - y)); } for (int i = 1; i < n; i++) { ll x = a[i][m]; ll y = a[n][m] - x; ans = min(ans, abs(x - y)); } cout << (ans == inf ? a[1][1] : ans); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; //cin >> t; while (t--) { solve(); } return 0; }