Submission #371041

# Submission time Handle Problem Language Result Execution time Memory
371041 2021-02-25T16:34:43 Z valerikk Group Photo (JOI21_ho_t3) C++17
100 / 100
513 ms 512 KB
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

struct Fenwick {
  int n;
  vector <int> t;
  
  void add(int pos, int delta) {
    for (int i = pos + 1; i <= n; i += i & -i) t[i] += delta;
  }

  int get(int pos) {
    int sum = 0;
    for (int i = pos; i > 0; i -= i & -i) sum += t[i];
    return sum;
  }

  int get(int l, int r) {
    return get(r) - get(l);
  }

  Fenwick(int nn) : n(nn) {
    t = vector <int>(n + 1, 0);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector <int> h(n);
  for (int i = 0; i < n; i++) cin >> h[i];
  vector <int> pos(n);
  for (int i = 0; i < n; i++) {
    h[i]--;
    pos[h[i]] = i;
  }
  const int inf = 1e9;
  vector <int> dp(n + 1);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1] = inf;
    Fenwick f(n);
    int cur = 0;
    for (int j = i; j >= 0; j--) {
      cur += f.get(pos[j], n) - f.get(pos[j]);
      f.add(pos[j], 1);
      dp[i + 1] = min(dp[i + 1], dp[j] + cur);
    }
  }
  int ans = dp[n];
  Fenwick f(n);
  for (int i = 0; i < n; i++) {
    ans += f.get(pos[i], n);
    f.add(pos[i], 1);
  }
  cout << ans << endl;
  return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 10 ms 364 KB Output is correct
32 Correct 10 ms 364 KB Output is correct
33 Correct 10 ms 364 KB Output is correct
34 Correct 9 ms 364 KB Output is correct
35 Correct 9 ms 364 KB Output is correct
36 Correct 9 ms 364 KB Output is correct
37 Correct 9 ms 364 KB Output is correct
38 Correct 9 ms 364 KB Output is correct
39 Correct 9 ms 364 KB Output is correct
40 Correct 9 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 10 ms 364 KB Output is correct
32 Correct 10 ms 364 KB Output is correct
33 Correct 10 ms 364 KB Output is correct
34 Correct 9 ms 364 KB Output is correct
35 Correct 9 ms 364 KB Output is correct
36 Correct 9 ms 364 KB Output is correct
37 Correct 9 ms 364 KB Output is correct
38 Correct 9 ms 364 KB Output is correct
39 Correct 9 ms 364 KB Output is correct
40 Correct 9 ms 364 KB Output is correct
41 Correct 454 ms 492 KB Output is correct
42 Correct 467 ms 364 KB Output is correct
43 Correct 477 ms 512 KB Output is correct
44 Correct 513 ms 364 KB Output is correct
45 Correct 484 ms 364 KB Output is correct
46 Correct 467 ms 492 KB Output is correct
47 Correct 448 ms 492 KB Output is correct
48 Correct 452 ms 476 KB Output is correct
49 Correct 447 ms 492 KB Output is correct
50 Correct 448 ms 492 KB Output is correct
51 Correct 456 ms 476 KB Output is correct
52 Correct 449 ms 492 KB Output is correct
53 Correct 456 ms 476 KB Output is correct
54 Correct 450 ms 492 KB Output is correct
55 Correct 460 ms 364 KB Output is correct