-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiendoimatran.cpp
59 lines (51 loc) · 1.23 KB
/
biendoimatran.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int a[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
int sumRow[n], sumCol[n];
memset(sumRow, 0, sizeof(sumRow));
memset(sumCol, 0, sizeof(sumCol));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
sumRow[i] += a[i][j];
sumCol[j] += a[i][j];
}
int maxSum = 0;
for (int i = 0; i < n; ++i)
{
maxSum = max(maxSum, sumRow[i]);
maxSum = max(maxSum, sumCol[i]);
}
int count = 0;
int i = 0, j = 0;
while (i < n && j < n)
{
int tmp = min(maxSum - sumRow[i], maxSum - sumCol[j]);
a[i][j] += tmp;
sumRow[i] += tmp;
sumCol[j] += tmp;
count += tmp;
if (sumRow[i] == maxSum)
i++;
if (sumCol[j] == maxSum)
j++;
}
cout << count << "\n";
}
}