Submission #3441418


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }

template<class T> struct BIT {
  int n, h;
  V<T> t;

  BIT(int n) : n(n), h(__lg(n) + 1), t(n + 1, (T) 0) {}

  void add(int x, T a) {
    for (int i = x + 1; i < n + 1; i += i & -i) t[i] += a;
  }

  int lower_bound(T s) {
    if (s <= 0) return 0;
    int res = 0;
    for (int k = 1 << h - 1; k; k >>= 1) if (res + k < n + 1 and t[res + k] < s) s -= t[res += k];
    return res + 1;
  }

  T sum(int x) {
    T res = 0;
    for (int i = x; i; i -= i & -i) res += t[i];
    return res;
  }

  T sum(int l, int r) {
    return sum(r) - sum(l);
  }
};

int main() {
  cin.tie(NULL); ios::sync_with_stdio(false);
  int n; cin >> n;
  V<> a(n); for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
  BIT<int> bit(n);
  V<> l(n), r(n);
  for (int i = 0; i < n; i++) {
    l[i] = bit.sum(a[i] + 1, n);
    bit.add(a[i], 1);
  }
  fill(bit.t.begin(), bit.t.end(), 0);
  for (int i = n - 1; i >= 0; i--) {
    r[i] = bit.sum(a[i] + 1, n);
    bit.add(a[i], 1);
  }
  lint res = 0;
  for (int i = 0; i < n; i++) res += min(l[i], r[i]);
  cout << res << '\n';
}

Submission Info

Submission Time
Task C - 積み木
User risujiroh
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1480 Byte
Status AC
Exec Time 20 ms
Memory 1792 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 70 / 70
Status
AC × 3
AC × 33
AC × 68
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
Subtask1 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 10_small_00.txt, 10_small_01.txt, 10_small_02.txt, 10_small_03.txt, 10_small_04.txt, 10_small_05.txt, 10_small_06.txt, 10_small_07.txt, 10_small_08.txt, 10_small_09.txt, 10_small_10.txt, 10_small_11.txt, 10_small_12.txt, 10_small_13.txt, 10_small_14.txt, 10_small_15.txt, 10_small_16.txt, 10_small_17.txt, 10_small_18.txt, 10_small_19.txt, 10_small_20.txt, 10_small_21.txt, 10_small_22.txt, 10_small_23.txt, 10_small_24.txt, 10_small_25.txt, 10_small_26.txt, 10_small_27.txt, 10_small_28.txt, 10_small_29.txt
Subtask2 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 10_small_00.txt, 10_small_01.txt, 10_small_02.txt, 10_small_03.txt, 10_small_04.txt, 10_small_05.txt, 10_small_06.txt, 10_small_07.txt, 10_small_08.txt, 10_small_09.txt, 10_small_10.txt, 10_small_11.txt, 10_small_12.txt, 10_small_13.txt, 10_small_14.txt, 10_small_15.txt, 10_small_16.txt, 10_small_17.txt, 10_small_18.txt, 10_small_19.txt, 10_small_20.txt, 10_small_21.txt, 10_small_22.txt, 10_small_23.txt, 10_small_24.txt, 10_small_25.txt, 10_small_26.txt, 10_small_27.txt, 10_small_28.txt, 10_small_29.txt, 20_rand_00.txt, 20_rand_01.txt, 20_rand_02.txt, 20_rand_03.txt, 20_rand_04.txt, 20_rand_05.txt, 20_rand_06.txt, 20_rand_07.txt, 20_rand_08.txt, 20_rand_09.txt, 20_rand_10.txt, 20_rand_11.txt, 20_rand_12.txt, 20_rand_13.txt, 20_rand_14.txt, 20_rand_15.txt, 20_rand_16.txt, 20_rand_17.txt, 20_rand_18.txt, 20_rand_19.txt, 20_rand_20.txt, 20_rand_21.txt, 20_rand_22.txt, 20_rand_23.txt, 20_rand_24.txt, 20_rand_25.txt, 20_rand_26.txt, 20_rand_27.txt, 20_rand_28.txt, 20_rand_29.txt, 30_max_0.txt, 30_max_1.txt, 30_max_2.txt, 30_max_3.txt, 30_max_4.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 256 KB
00_sample_01.txt AC 1 ms 256 KB
00_sample_02.txt AC 1 ms 256 KB
10_small_00.txt AC 1 ms 256 KB
10_small_01.txt AC 1 ms 256 KB
10_small_02.txt AC 1 ms 256 KB
10_small_03.txt AC 1 ms 256 KB
10_small_04.txt AC 1 ms 256 KB
10_small_05.txt AC 1 ms 256 KB
10_small_06.txt AC 1 ms 256 KB
10_small_07.txt AC 1 ms 256 KB
10_small_08.txt AC 1 ms 256 KB
10_small_09.txt AC 1 ms 256 KB
10_small_10.txt AC 1 ms 256 KB
10_small_11.txt AC 1 ms 256 KB
10_small_12.txt AC 1 ms 256 KB
10_small_13.txt AC 1 ms 256 KB
10_small_14.txt AC 1 ms 256 KB
10_small_15.txt AC 1 ms 256 KB
10_small_16.txt AC 1 ms 256 KB
10_small_17.txt AC 1 ms 256 KB
10_small_18.txt AC 1 ms 256 KB
10_small_19.txt AC 1 ms 256 KB
10_small_20.txt AC 1 ms 256 KB
10_small_21.txt AC 1 ms 256 KB
10_small_22.txt AC 1 ms 256 KB
10_small_23.txt AC 1 ms 256 KB
10_small_24.txt AC 1 ms 256 KB
10_small_25.txt AC 1 ms 256 KB
10_small_26.txt AC 1 ms 256 KB
10_small_27.txt AC 1 ms 256 KB
10_small_28.txt AC 1 ms 256 KB
10_small_29.txt AC 1 ms 256 KB
20_rand_00.txt AC 12 ms 1280 KB
20_rand_01.txt AC 2 ms 384 KB
20_rand_02.txt AC 4 ms 512 KB
20_rand_03.txt AC 6 ms 768 KB
20_rand_04.txt AC 12 ms 1280 KB
20_rand_05.txt AC 18 ms 1664 KB
20_rand_06.txt AC 20 ms 1792 KB
20_rand_07.txt AC 13 ms 1280 KB
20_rand_08.txt AC 4 ms 512 KB
20_rand_09.txt AC 3 ms 512 KB
20_rand_10.txt AC 12 ms 1280 KB
20_rand_11.txt AC 6 ms 768 KB
20_rand_12.txt AC 6 ms 768 KB
20_rand_13.txt AC 8 ms 896 KB
20_rand_14.txt AC 5 ms 640 KB
20_rand_15.txt AC 2 ms 384 KB
20_rand_16.txt AC 8 ms 896 KB
20_rand_17.txt AC 1 ms 256 KB
20_rand_18.txt AC 19 ms 1792 KB
20_rand_19.txt AC 4 ms 512 KB
20_rand_20.txt AC 3 ms 512 KB
20_rand_21.txt AC 12 ms 1280 KB
20_rand_22.txt AC 12 ms 1280 KB
20_rand_23.txt AC 20 ms 1792 KB
20_rand_24.txt AC 8 ms 896 KB
20_rand_25.txt AC 9 ms 1024 KB
20_rand_26.txt AC 7 ms 768 KB
20_rand_27.txt AC 7 ms 768 KB
20_rand_28.txt AC 16 ms 1536 KB
20_rand_29.txt AC 3 ms 512 KB
30_max_0.txt AC 15 ms 1792 KB
30_max_1.txt AC 15 ms 1792 KB
30_max_2.txt AC 18 ms 1792 KB
30_max_3.txt AC 17 ms 1792 KB
30_max_4.txt AC 15 ms 1792 KB