답안 #625124

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
625124 2022-08-09T12:14:51 Z Abrar_Al_Samit Group Photo (JOI21_ho_t3) C++17
100 / 100
707 ms 67728 KB
#include<bits/stdc++.h>
using namespace std;

const int MX = 5004;

int n;
int a[MX];
int cost[MX][MX];
int bit[MX];

void add(int id, int delta) {
  for(int i=id; i<=n; i=i|(i+1)) {
    bit[i] += delta;
  }
}
int sum(int r) {
  int ret = 0;
  for(int i=r; i>=0; i=(i&(i+1))-1) {
    ret += bit[i];
  }
  return ret;
}
int sum(int l, int r) {
  return sum(r) - sum(l-1);
}


int dp[MX];
int solve(int i) {
  if(i>n) return 0;
  int &ret = dp[i];
  if(ret!=-1) return ret;
  ret = INT_MAX;

  for(int j=i; j<=n; ++j) {
    ret = min(ret, solve(j+1)+cost[i][j]);
  }
  return ret;
}
int alt[MX];
void PlayGround() {
  cin>>n;
  for(int i=1; i<=n; ++i) {
    cin>>a[i];
    alt[a[i]] = i;
  }

  int inv[n+1] = {};
  for(int i=1; i<=n; ++i) {
    for(int j=alt[i]-1; j>0; --j) if(a[j]>i) {
      ++inv[i];
    }
  }

  for(int j=1; j<=n; ++j) {
    memset(bit, 0, sizeof bit);
    for(int i=j; i>0; --i) {
      cost[i][j] = inv[i] - sum(1, alt[i]) + sum(alt[i], n);
      if(i!=j) cost[i][j] += cost[i+1][j];
      add(alt[i], 1);
    }
  }

  memset(dp, -1, sizeof dp);
  cout<<solve(1)<<'\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  PlayGround();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 1236 KB Output is correct
22 Correct 1 ms 1236 KB Output is correct
23 Correct 1 ms 1236 KB Output is correct
24 Correct 1 ms 1236 KB Output is correct
25 Correct 2 ms 1236 KB Output is correct
26 Correct 1 ms 1236 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 1 ms 1236 KB Output is correct
29 Correct 2 ms 1236 KB Output is correct
30 Correct 1 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 1236 KB Output is correct
22 Correct 1 ms 1236 KB Output is correct
23 Correct 1 ms 1236 KB Output is correct
24 Correct 1 ms 1236 KB Output is correct
25 Correct 2 ms 1236 KB Output is correct
26 Correct 1 ms 1236 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 1 ms 1236 KB Output is correct
29 Correct 2 ms 1236 KB Output is correct
30 Correct 1 ms 1236 KB Output is correct
31 Correct 14 ms 4820 KB Output is correct
32 Correct 12 ms 4820 KB Output is correct
33 Correct 13 ms 4824 KB Output is correct
34 Correct 15 ms 4820 KB Output is correct
35 Correct 17 ms 4900 KB Output is correct
36 Correct 14 ms 4948 KB Output is correct
37 Correct 12 ms 4908 KB Output is correct
38 Correct 12 ms 4784 KB Output is correct
39 Correct 12 ms 4820 KB Output is correct
40 Correct 15 ms 4840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 1236 KB Output is correct
22 Correct 1 ms 1236 KB Output is correct
23 Correct 1 ms 1236 KB Output is correct
24 Correct 1 ms 1236 KB Output is correct
25 Correct 2 ms 1236 KB Output is correct
26 Correct 1 ms 1236 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 1 ms 1236 KB Output is correct
29 Correct 2 ms 1236 KB Output is correct
30 Correct 1 ms 1236 KB Output is correct
31 Correct 14 ms 4820 KB Output is correct
32 Correct 12 ms 4820 KB Output is correct
33 Correct 13 ms 4824 KB Output is correct
34 Correct 15 ms 4820 KB Output is correct
35 Correct 17 ms 4900 KB Output is correct
36 Correct 14 ms 4948 KB Output is correct
37 Correct 12 ms 4908 KB Output is correct
38 Correct 12 ms 4784 KB Output is correct
39 Correct 12 ms 4820 KB Output is correct
40 Correct 15 ms 4840 KB Output is correct
41 Correct 585 ms 67572 KB Output is correct
42 Correct 600 ms 67516 KB Output is correct
43 Correct 624 ms 67576 KB Output is correct
44 Correct 609 ms 67540 KB Output is correct
45 Correct 591 ms 67728 KB Output is correct
46 Correct 678 ms 67528 KB Output is correct
47 Correct 707 ms 67620 KB Output is correct
48 Correct 648 ms 67648 KB Output is correct
49 Correct 642 ms 67648 KB Output is correct
50 Correct 615 ms 67596 KB Output is correct
51 Correct 660 ms 67528 KB Output is correct
52 Correct 632 ms 67580 KB Output is correct
53 Correct 619 ms 67648 KB Output is correct
54 Correct 606 ms 67580 KB Output is correct
55 Correct 635 ms 67652 KB Output is correct