题解 | 小美的蛋糕切割

#include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<ll>> a(n+1, vector<ll>(m+1, 0));//a存储输入的数据
    vector<vector<ll>> map(n+1, vector<ll>(m+1, 0));//map存储二维前缀和
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
            map[i][j] = map[i-1][j] + map[i][j-1] - map[i-1][j-1] + a[i][j];
        }
    }
    ll total = map[n][m];
    ll ans = INT_MAX;
    ll tmp = 0;
    for(int i = 1; i <= n; i++){//先横切
        tmp = abs(total - map[i][m] - map[i][m]);
        ans = min(ans, tmp);
    }
    for(int j = 1; j <= m; j++){//再竖切
        tmp = abs(total - map[n][j] - map[n][j]);
        ans = min(ans, tmp);
    }
    cout << ans << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务