Submission #625118

# Submission time Handle Problem Language Result Execution time Memory
625118 2022-08-09T12:01:36 Z Abrar_Al_Samit Group Photo (JOI21_ho_t3) C++17
64 / 100
5000 ms 58264 KB
#include<bits/stdc++.h>
using namespace std;

const int MX = 5004;

int n;
int a[MX];
int cost[MX][MX];
int st[MX*4];

void upd(int l, int r, int id, int val, int v) {
  if(l==r) {
    st[v] = val;
    return;
  }
  int mid = (l+r)/2;
  if(id<=mid) upd(l, mid, id, val, v*2);
  else upd(mid+1, r, id, val, v*2+1);
  st[v] = st[v*2]+st[v*2+1];
}
int query(int l, int r, int L, int R, int v) {
  if(l>=L && r<=R) return st[v];
  if(l>R || r<L) return 0;

  int mid = (l+r)/2;
  return query(l, mid, L, R, v*2) + query(mid+1, r, L, R, v*2+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) {
    for(int i=1; i<=n; ++i) {
      upd(1, n, i, 0, 1);
    }
    for(int i=j; i>0; --i) {
      cost[i][j] = inv[i] - query(1, n, 1, alt[i], 1) + query(1, n, alt[i], n, 1);
      if(i!=j) cost[i][j] += cost[i+1][j];
      upd(1, n, alt[i], 1, 1);
    }
  }

  memset(dp, -1, sizeof dp);
  cout<<solve(1)<<'\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  PlayGround();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 1 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 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 1 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 352 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 1 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 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 1 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 352 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 1 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 0 ms 340 KB Output is correct
21 Correct 5 ms 1236 KB Output is correct
22 Correct 5 ms 1236 KB Output is correct
23 Correct 6 ms 1236 KB Output is correct
24 Correct 6 ms 1236 KB Output is correct
25 Correct 6 ms 1148 KB Output is correct
26 Correct 6 ms 1236 KB Output is correct
27 Correct 6 ms 1216 KB Output is correct
28 Correct 6 ms 1244 KB Output is correct
29 Correct 5 ms 1236 KB Output is correct
30 Correct 5 ms 1200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 1 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 352 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 1 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 0 ms 340 KB Output is correct
21 Correct 5 ms 1236 KB Output is correct
22 Correct 5 ms 1236 KB Output is correct
23 Correct 6 ms 1236 KB Output is correct
24 Correct 6 ms 1236 KB Output is correct
25 Correct 6 ms 1148 KB Output is correct
26 Correct 6 ms 1236 KB Output is correct
27 Correct 6 ms 1216 KB Output is correct
28 Correct 6 ms 1244 KB Output is correct
29 Correct 5 ms 1236 KB Output is correct
30 Correct 5 ms 1200 KB Output is correct
31 Correct 110 ms 4852 KB Output is correct
32 Correct 111 ms 4744 KB Output is correct
33 Correct 112 ms 4872 KB Output is correct
34 Correct 110 ms 4788 KB Output is correct
35 Correct 126 ms 4904 KB Output is correct
36 Correct 117 ms 4824 KB Output is correct
37 Correct 114 ms 4812 KB Output is correct
38 Correct 109 ms 4872 KB Output is correct
39 Correct 111 ms 4920 KB Output is correct
40 Correct 112 ms 4772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 1 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 352 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 1 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 0 ms 340 KB Output is correct
21 Correct 5 ms 1236 KB Output is correct
22 Correct 5 ms 1236 KB Output is correct
23 Correct 6 ms 1236 KB Output is correct
24 Correct 6 ms 1236 KB Output is correct
25 Correct 6 ms 1148 KB Output is correct
26 Correct 6 ms 1236 KB Output is correct
27 Correct 6 ms 1216 KB Output is correct
28 Correct 6 ms 1244 KB Output is correct
29 Correct 5 ms 1236 KB Output is correct
30 Correct 5 ms 1200 KB Output is correct
31 Correct 110 ms 4852 KB Output is correct
32 Correct 111 ms 4744 KB Output is correct
33 Correct 112 ms 4872 KB Output is correct
34 Correct 110 ms 4788 KB Output is correct
35 Correct 126 ms 4904 KB Output is correct
36 Correct 117 ms 4824 KB Output is correct
37 Correct 114 ms 4812 KB Output is correct
38 Correct 109 ms 4872 KB Output is correct
39 Correct 111 ms 4920 KB Output is correct
40 Correct 112 ms 4772 KB Output is correct
41 Execution timed out 5064 ms 58264 KB Time limit exceeded
42 Halted 0 ms 0 KB -