-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseat.cpp
55 lines (44 loc) · 1016 Bytes
/
seat.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
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pi>;
bool cmp(pi a, pi b) {
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
void solve() {
int n, m;
cin >> n >> m;
vpi a(n * m);
map<int, set<int>> mp;
for(int i = 0; i < n * m; i++) {
int x;
cin >> x;
a[i] = {x, i};
if (mp.find(x) != mp.end()) mp[x].insert(i);
else mp[x] = {};
}
sort(a.begin(), a.end(), cmp);
vpi coords(n * m);
int curr = 0;
for (int i = 0; i < n * m; i++) {
coords[a[i].second] = {i, a[i].first};
}
int ans = 0;
vector<bool> used(n * m);
for (int i = 0; i < n * m; i++) {
int ch = coord[i];
for (int j = (ch / m) * m; j < ch; j++) {
ans += used[j];
}
used[ch] = true;
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}