QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#782570 | #9566. Topology | LtyFime | TL | 0ms | 0kb | C++14 | 807b | 2024-11-25 20:30:47 | 2024-11-25 20:30:49 |
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e6 + 5;
int t[N];
int lowbit(int x) { return x & -x; }
void add(int x, int y) {
while (x) t[x] += y, x -= lowbit(x);
}
int query(int x, int n) {
int ans = 0;
while (x <= n) ans += t[x], x += lowbit(x);
return ans;
}
int p[N], q[N], f[N];
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> p[i], q[p[i]] = i, t[i] = 0;
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans += query(p[i], n);
add(p[i], 1);
}
for (int i = 2; i <= n; i++) {
f[i] = f[i - 1];
f[i] = max(f[i], f[i - 2] + (q[i - 1] > q[i]));
}
cout << ans - f[n] << '\n';
}
int main() {
int T; cin >> T;
while (T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
4 1 1 2
output:
0