\
路长且艰,任重而道远,什么时候才能成长成真正的程序员呢
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
vector<vector<int>> vec(n, vector<int>(m, 0));
int sum = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cin >> vec[i][j];
sum += vec[i][j];
}
}
vector<int> horizontal(n, 0);
vector<int> vertical(m, 0);
for(int i = 0; i < n; ++i){ // 统计每行价值
for(int j = 0; j < m; ++j){
horizontal[i] += vec[i][j];
}
}
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){//统计每列价值
vertical[i] += vec[j][i];
}
}
int ans = INT_MAX;
int horizontalValue = 0;
int verticalValue = 0;
//遍历水平切
for(int i = 0; i < n; ++i){
horizontalValue += horizontal[i];
ans = min(ans, abs(sum - 2 * horizontalValue));
}
//遍历竖直切
for(int j = 0; j < m; ++j){
verticalValue += vertical[j];
ans = min(ans, abs(sum - 2 * verticalValue));
}
cout << ans << endl;
return 0;
}
标签:int,44,++,vector,购买,开发商,ans,sum,vec From: https://www.cnblogs.com/llllmz/p/18419531