题解 | #小美的蛋糕切割#
小美的蛋糕切割
https://www.nowcoder.com/practice/15aa2c407c8840e09e2532313fb6809d
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
static int N = 1010;
static int n,m;
static long[][] a = new long[N][N], s = new long[N][N];
static void solve() {
n = in.nextInt(); m = in.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
a[i][j] = in.nextInt();
s[i][j] = a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
}
}
long s1 = 0, s2 = 0;
long ans = Long.MAX_VALUE;
for(int i=1;i<=m;i++) {
s1 = s[n][i];
s2 = s[n][m] - s[n][i];
ans = Math.min(ans,Math.abs(s1-s2));
}
for(int i=1;i<=n;i++) {
s1 = s[i][m];
s2 = s[n][m] - s[i][m];
ans = Math.min(ans,Math.abs(s1-s2));
}
out.println(ans);
}
public static void main(String[] args) {
solve();
out.flush();
}
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static class FastReader {
static BufferedReader br;
static StringTokenizer st;
FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
String str = "";
while (st == null || !st.hasMoreElements()) {
try {
str = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
st = new StringTokenizer(str);
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
汤臣倍健公司氛围 390人发布

