forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
153 lines (136 loc) · 3.76 KB
/
C.cpp
File metadata and controls
153 lines (136 loc) · 3.76 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i)
#define FORD(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i)
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
#define DEBUG(X) { auto _X = (X); cerr << "L" << __LINE__ << ": " << #X << " = " << (_X) << endl; }
#define PR(A, n) { cerr << "L" << __LINE__ << ": " << #A << " = "; FOR(_, 1, n) cerr << A[_] << ' '; cerr << endl; }
#define PR0(A, n) { cerr << "L" << __LINE__ << ": " << #A << " = "; REP(_, n) cerr << A[_] << ' '; cerr << endl; }
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int)(x).size())
#define ALL(a) (a).begin(), (a).end()
inline int gcd(int a, int b) {int r; while (b) {r = a % b; a = b; b = r;} return a;}
const int MN = 444;
const int inf = 1000111000;
#define arg __arg
long long c[MN][MN];
long long fx[MN], fy[MN];
int mx[MN], my[MN];
int trace[MN], qu[MN], arg[MN];
long long d[MN];
int front, rear, start, finish;
int N;
void init() {
FOR(i,1,N) {
fy[i] = mx[i] = my[i] = 0;
FOR(j,1,N) c[i][j] = inf;
}
}
void addEdge(int i, int j, long long cost) {
c[i][j] = min(c[i][j], cost);
}
inline long long getC(int i, int j) {
return c[i][j] - fx[i] - fy[j];
}
void initBFS() {
front = rear = 1;
qu[1] = start;
memset(trace, 0, sizeof trace);
FOR(j,1,N) {
d[j] = getC(start, j);
arg[j] = start;
}
finish = 0;
}
void findAugPath() {
while (front <= rear) {
int i = qu[front++];
FOR(j,1,N) if (!trace[j]) {
long long w = getC(i, j);
if (!w) {
trace[j] = i;
if (!my[j]) {
finish = j;
return ;
}
qu[++rear] = my[j];
}
if (d[j] > w) {
d[j] = w;
arg[j] = i;
}
}
}
}
void subx_addy() {
long long delta = inf;
FOR(j,1,N)
if (trace[j] == 0 && d[j] < delta) delta = d[j];
// xoay
fx[start] += delta;
FOR(j,1,N)
if (trace[j]) {
int i = my[j];
fy[j] -= delta;
fx[i] += delta;
}
else d[j] -= delta;
FOR(j,1,N)
if (!trace[j] && !d[j]) {
trace[j] = arg[j];
if (!my[j]) { finish = j; return ; }
qu[++rear] = my[j];
}
}
void enlarge() {
do {
int i = trace[finish];
int next = mx[i];
mx[i] = finish;
my[finish] = i;
finish = next;
} while (finish);
}
int mincost() {
FOR(i,1,N) fx[i] = *min_element(c[i]+1, c[i]+N+1);
FOR(j,1,N) {
fy[j] = c[1][j] - fx[1];
FOR(i,1,N) {
fy[j] = min(fy[j], c[i][j] - fx[i]);
}
}
FOR(i,1,N) {
start = i;
initBFS();
while (finish == 0) {
findAugPath();
if (!finish) subx_addy();
}
enlarge();
}
int res = 0;
FOR(i,1,N) res += c[i][mx[i]];
return res;
}
int32_t main() {
ios::sync_with_stdio(0);
int ntest; cin >> ntest;
while (ntest--) {
int n; cin >> n;
vector<int> a(n);
for (auto& val : a) cin >> val;
sort(ALL(a));
N = 2*n;
init();
REP(i,n) FOR(j,1,N) {
addEdge(i+1, j, llabs(a[i] - j));
}
int t = mincost();
cout << t - n * inf << endl;
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}