QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#77161#5507. InvestorsUCSC_Ravioli#WA 4ms3364kbC++202.6kb2023-02-13 06:59:272023-02-13 06:59:28

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-13 06:59:28]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:3364kb
  • [2023-02-13 06:59:27]
  • 提交

answer

// qdd on Feb 12, 2023

#ifdef qdd
#include <ringo>
#else
#include <bits/stdc++.h>
#define dbg(...)
#define dbgr(x, y)
#endif

using namespace std;

using ll = long long;

#define ALL(x) begin(x), end(x)

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v) is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  bool f = 0;
  for (const T& x : v) (f ? os << ' ' : os) << x, f = 1;
  return os;
}

// repeating elements with same id
template <class T>
vector<int> unique_dc(const vector<T>& a, int start_id) {
  int n = a.size();
  vector<T> t(a);
  sort(t.begin(), t.end());
  t.resize(unique(t.begin(), t.end()) - t.begin());
  vector<int> id(n);
  for (int i = 0; i < n; i++) {
    id[i] = start_id + lower_bound(t.begin(), t.end(), a[i]) - t.begin();
  }
  return id;
}

struct fenwick {
  int n;
  vector<int> t;
  fenwick(int n) : n(n), t(n + 1) {}
  void add(int p, int x) {
    for (; p <= n; p += p & -p) t[p] += x;
  }
  int get(int p) {
    int a = 0;
    for (; p > 0; p -= p & -p) a += t[p];
    return a;
  }
};

void sol() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;
  a = unique_dc(a, 1);
  int mx = *max_element(ALL(a));
  // inv between [0, i-1] and [i,n-1]
  vector<int> inv(n + 1);
  auto build_inverse = [&](int l, int r) {
    if (r - l < 5) {
      for (int k = l; k <= r; k++) {
        inv[k] = 0;
        for (int i = l; i < k; i++) {
          for (int j = k; j <= r; j++) {
            if (a[i] > a[j]) inv[k]++;
          }
        }
      }
      return;
    }
    fenwick L(mx), R(mx);
    for (int i = l; i <= r; i++) {
      R.add(a[i], 1);
    }
    inv[l] = 0;
    for (int i = l + 1; i <= r; i++) {
      R.add(a[i - 1], -1);
      inv[i] = inv[i - 1] + R.get(a[i - 1] - 1) - (L.get(mx) - L.get(a[i - 1]));
      L.add(a[i - 1], 1);
    }
    inv[r + 1] = 0;
  };
  build_inverse(0, n - 1);
  auto global_inverse = [&]() {
    fenwick t(mx);
    int res = 0;
    for (int i = 0; i < n; i++) {
      res += t.get(mx) - t.get(a[i]);
      t.add(a[i], 1);
    }
    return res;
  };
  int ans = global_inverse();
  for (int i = 0; i < k; i++) {
    int p = max_element(ALL(inv)) - inv.begin();
    ans -= inv[p];
    int l = p - 1, r = p;
    while (inv[l] > 0) l--;
    while (inv[r] > 0) r++;
    build_inverse(l, p - 1);
    build_inverse(p, r - 1);
  }
  cout << ans << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    sol();
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3364kb

input:

2
6 1
4 5 6 2 2 1
6 2
4 5 6 2 2 1

output:

2
0

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 3336kb

input:

349
6 2
2 1 2 1 2 1
48 12
42 47 39 39 27 25 22 44 45 13 5 48 38 4 37 6 24 10 42 38 12 37 31 19 44 6 29 17 7 12 7 26 35 24 15 9 37 3 27 21 33 29 34 20 14 30 31 21
48 12
1 43 17 46 17 39 40 22 25 2 22 12 4 11 38 12 4 11 1 5 39 44 37 10 19 20 42 45 2 45 37 20 48 34 16 41 23 18 13 44 47 21 29 4 23 18 16...

output:

1
18
15
145
0
2
1
0
14
13
24
0
0
0
1
0
0
0
0
0
0
0
0
161
3
0
0
1
0
0
0
0
0
0
1
0
3
0
0
1
0
0
1
0
0
1
4
0
0
0
0
0
0
0
0
2
0
2
0
0
8
280
0
0
35
4
0
1
0
0
3
0
0
0
0
0
0
0
4
0
0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
0
2
0
0
0
0
0
0
0
8
1
9
0
0
0
0
1
12
5
0
0
0
7
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
14
1
0
0
0
...

result:

wrong answer 9th lines differ - expected: '13', found: '14'