QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#782570#9566. TopologyLtyFimeTL 0ms0kbC++14807b2024-11-25 20:30:472024-11-25 20:30:49

Judging History

你现在查看的是最新测评结果

  • [2024-11-25 20:30:49]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-25 20:30:47]
  • 提交

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

result: