Submission #508666

# Submission time Handle Problem Language Result Execution time Memory
508666 2022-01-13T14:01:07 Z cig32 Group Photo (JOI21_ho_t3) C++17
64 / 100
5000 ms 98000 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 5030;
const int MOD = 1e9 + 7;
//#define int long long    

int rnd(int x, int y) { // random number generator
  int u= uniform_int_distribution<int>(x, y)(rng); return u;
}

void solve(int tc) {
  int n;
  cin >> n;
  int h[n+1];
  for(int i=1; i<=n; i++) cin >> h[i];
  int pos[n+1];
  for(int i=1; i<=n; i++) pos[h[i]] = i;
  int inv[n+1][n+1];
  for(int i=0; i<=n; i++) {
      for(int j=0; j<=n; j++) {
          inv[i][j] = 0;
      }
  }
  for(int i=1; i<=n; i++) {
      for(int j=i+1; j<=n; j++) {
          if(h[i] > h[j]) {
              for(int k=1; k<=h[j]; k++) inv[k][h[i]]++;
          }
      }
  }
  for(int i=1; i<=n; i++) {
      for(int j=1; j<=n; j++) {
          inv[i][j] += inv[i][j-1];
      }
  }
  int ext_pos[n+1][n+1];
  for(int i=0; i<=n; i++) {
      for(int j=0; j<=n; j++) {
          ext_pos[i][j] = 0;
      }
  }
  int right[n+1][n+1];
  for(int i=0; i<=n; i++) {
      for(int j=0; j<=n; j++) {
          right[i][j] = 0;
      }
  }
  for(int i=n-1; i>=1; i--) {
      for(int j=0; j<=n; j++) {
          right[h[i]][j] = right[h[i+1]][j] + (h[i+1] <= j);
      }
  }
  for(int i=1; i<=n; i++) {
      for(int j=0; j<=n; j++) {
          ext_pos[i][j] = pos[i] + right[i][j];
      }
  }
  for(int i=2; i<=n; i++) {
      for(int j=0; j<=n; j++) {
          ext_pos[i][j] += ext_pos[i-1][j];
      }
  }
  int dp[n+1];
  for(int i=0; i<=n; i++) dp[i] = 1e9;
  dp[0] = 0;
  for(int i=1; i<=n; i++) {
      for(int j=0; j<i; j++) {
          int ref = inv[j+1][i];
          int sz = i-j;
          int mx = sz * (sz-1) / 2;
          int ono = ext_pos[i][j] - ext_pos[j][j];
          int rem = (j+1 + i) * (i - (j+1) + 1) / 2;
          //cout << i << " " << j << " " << mx << " " << ref << " ";
          //cout << ono << " " << rem << "\n";
          dp[i] = min(dp[i], dp[j] + mx - ref + ono - rem);
      }
  }
  cout << dp[n] << "\n";
  /*
  cout << "dp array:\n";
  for(int i=1; i<=n; i++) {
      cout << dp[i] << " ";
  }
  cout << "\n";
  cout << "inv array:\n";
  for(int i=1; i<=n; i++) {
      for(int j=1; j<=n; j++) {
          cout << inv[i][j] << " ";
      }
      cout << "\n";
  }
  */
}

int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
} 
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 316 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 316 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 708 KB Output is correct
22 Correct 2 ms 688 KB Output is correct
23 Correct 1 ms 704 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 2 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 2 ms 716 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 2 ms 700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 316 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 708 KB Output is correct
22 Correct 2 ms 688 KB Output is correct
23 Correct 1 ms 704 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 2 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 2 ms 716 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 2 ms 700 KB Output is correct
31 Correct 52 ms 7796 KB Output is correct
32 Correct 47 ms 7748 KB Output is correct
33 Correct 48 ms 7792 KB Output is correct
34 Correct 48 ms 7816 KB Output is correct
35 Correct 50 ms 7812 KB Output is correct
36 Correct 56 ms 7812 KB Output is correct
37 Correct 47 ms 7732 KB Output is correct
38 Correct 47 ms 7832 KB Output is correct
39 Correct 50 ms 7808 KB Output is correct
40 Correct 48 ms 7748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 316 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 708 KB Output is correct
22 Correct 2 ms 688 KB Output is correct
23 Correct 1 ms 704 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 2 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 2 ms 716 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 2 ms 700 KB Output is correct
31 Correct 52 ms 7796 KB Output is correct
32 Correct 47 ms 7748 KB Output is correct
33 Correct 48 ms 7792 KB Output is correct
34 Correct 48 ms 7816 KB Output is correct
35 Correct 50 ms 7812 KB Output is correct
36 Correct 56 ms 7812 KB Output is correct
37 Correct 47 ms 7732 KB Output is correct
38 Correct 47 ms 7832 KB Output is correct
39 Correct 50 ms 7808 KB Output is correct
40 Correct 48 ms 7748 KB Output is correct
41 Execution timed out 5062 ms 98000 KB Time limit exceeded
42 Halted 0 ms 0 KB -