Submission #601514

# Submission time Handle Problem Language Result Execution time Memory
601514 2022-07-22T06:35:32 Z pakhomovee Group Photo (JOI21_ho_t3) C++17
100 / 100
530 ms 293952 KB
//  by pakhomovee
#include <iomanip>
#include <iostream>
#include <bitset>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <cmath>
#include <unordered_map>
#include <cassert>
#include <queue>
#include <unordered_set>
#include <chrono>

using namespace std;
using namespace chrono;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#define all(a) a.begin(), a.end()
#define double long double
#define ll long long
#define dbg(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FEL(a, x) for (auto& a : x)
#define REV reverse
#define asrt assert
#define ll long long
#define unique1(x) x.resize(unique(all(x)) - x.begin())

const double eps = 1e-7;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> pos(n);
    for (int& i : a) {
        cin >> i;
        --i;
    }
    for (int i = 0; i < n; ++i) {
        pos[a[i]] = i;
    }
    vector<int> dp(n + 1, 2e9);
    int cost[n][n];
    int inv[n][n];
    int rinv[n][n];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cost[i][j] = inv[i][j] = rinv[i][j] = 0;
        }
    }
    vector<int> withBigger(n, 0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            if (a[j] > a[i]) {
                ++withBigger[a[i]];
            }
        }
    }
    for (int j = 0; j < n; ++j) {
        int curr = 0;
        for (int i = j - 1; i >= 0; --i) {
            if (pos[j] < pos[i]) {
                ++curr;
            }
            inv[j][i] = curr;
        }
    }
    for (int j = 0; j < n; ++j) {
        int curr = 0;
        for (int i = j - 1; i >= 0; --i) {
            if (pos[j] > pos[i]) {
                ++curr;
            }
            rinv[j][i] = curr;
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            int need = cost[i][j - 1];
            int ps = pos[j];
            need += rinv[j][i];
            cost[i][j] = need;
        }
    }
    for (int i = 0; i < n; ++i) {
        int sum = 0;
        for (int j = i; j < n; ++j) {
            sum += withBigger[j];
            sum -= inv[j][i];
            if (j != n - 1) {
                cost[i][j] += sum;
            }
            /*for (int k = n - 1; k >= 0; --k) {
                if (a[k] >= i && a[k] <= j) {
                    ++cnt;
                }
                if (a[k] > j) {
                    cost[i][j] += cnt;
                }
            }*/
        }
    }
    dp[0] = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = i; j >= 1; --j) {
            dp[i] = min(dp[i], dp[j - 1] + cost[j - 1][i - 1]);
        }
    }
    cout << dp.back();
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    t = 1;
    while (t--) {
        solve();
    }
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:85:17: warning: unused variable 'ps' [-Wunused-variable]
   85 |             int ps = pos[j];
      |                 ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 700 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 1 ms 700 KB Output is correct
30 Correct 1 ms 700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 700 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 1 ms 700 KB Output is correct
30 Correct 1 ms 700 KB Output is correct
31 Correct 8 ms 7764 KB Output is correct
32 Correct 7 ms 7832 KB Output is correct
33 Correct 8 ms 7764 KB Output is correct
34 Correct 7 ms 7848 KB Output is correct
35 Correct 7 ms 7764 KB Output is correct
36 Correct 7 ms 7764 KB Output is correct
37 Correct 7 ms 7764 KB Output is correct
38 Correct 7 ms 7764 KB Output is correct
39 Correct 7 ms 7764 KB Output is correct
40 Correct 8 ms 7764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 700 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 1 ms 700 KB Output is correct
30 Correct 1 ms 700 KB Output is correct
31 Correct 8 ms 7764 KB Output is correct
32 Correct 7 ms 7832 KB Output is correct
33 Correct 8 ms 7764 KB Output is correct
34 Correct 7 ms 7848 KB Output is correct
35 Correct 7 ms 7764 KB Output is correct
36 Correct 7 ms 7764 KB Output is correct
37 Correct 7 ms 7764 KB Output is correct
38 Correct 7 ms 7764 KB Output is correct
39 Correct 7 ms 7764 KB Output is correct
40 Correct 8 ms 7764 KB Output is correct
41 Correct 530 ms 293592 KB Output is correct
42 Correct 501 ms 293704 KB Output is correct
43 Correct 518 ms 293828 KB Output is correct
44 Correct 510 ms 293828 KB Output is correct
45 Correct 497 ms 293732 KB Output is correct
46 Correct 512 ms 293948 KB Output is correct
47 Correct 508 ms 293944 KB Output is correct
48 Correct 501 ms 293940 KB Output is correct
49 Correct 497 ms 293940 KB Output is correct
50 Correct 501 ms 293944 KB Output is correct
51 Correct 493 ms 293948 KB Output is correct
52 Correct 502 ms 293836 KB Output is correct
53 Correct 528 ms 293948 KB Output is correct
54 Correct 493 ms 293952 KB Output is correct
55 Correct 498 ms 293944 KB Output is correct