-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path스타트와 링크.cc
86 lines (81 loc) · 1.99 KB
/
스타트와 링크.cc
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <algorithm>
using namespace std;
int n;
vector<vector<int>> points;
vector<int> t1;
vector<int> t2;
int s1, s2;
int cal(int prev, int nxt) {
int index = 0;
for (int i = 0; i < n/2; i++) {
s1 -= points[prev][t1[i]] + points[t1[i]][prev];
s2 -= points[nxt][t2[i]] + points[t2[i]][nxt];
if (t1[i] != prev) {
s1 += points[nxt][t1[i]] + points[t1[i]][nxt];
}
if (t2[i] != nxt) {
s2 += points[prev][t2[i]] + points[t2[i]][prev];
} else {
index = i;
}
}
return index;
}
bool assignTeam(){
int index = n/2 - 1, prev = t1[index], nxt, index2 = 0;
while (prev >= index + n/2) {
index--;
prev = t1[index];
}
if (index == 0) {
return false;
}
nxt = prev + 1;
index2 = cal(prev, nxt);
t1[index] = nxt;
t2[index2] = prev;
index++;
while (index < n/2) {
nxt += 1;
prev = t1[index];
if (prev != nxt) {
index2 = cal(prev,nxt);
t1[index] = nxt;
t2[index2] = prev;
}
index++;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("input.txt", "r", stdin);
cin >> n;
points.assign(n, vector<int>(n,0));
t1.assign(n/2,0);
t2.assign(n/2,0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> points[i][j];
}
if (i < n/2) {
t1[i] = i;
} else {
t2[i - n/2] = i;
}
}
for (int i = 0; i < n/2; i++) {
for (int j = i + 1; j < n/2; j++) {
s1 += points[t1[i]][t1[j]] + points[t1[j]][t1[i]];
s2 += points[t2[i]][t2[j]] + points[t2[j]][t2[i]];
}
}
int diff = min(1000000000, abs(s1 - s2));
while (assignTeam()) {
diff = min(diff, abs(s1 - s2));
}
cout << diff;
}