Submission #602237

# Submission time Handle Problem Language Result Execution time Memory
602237 2022-07-22T18:25:06 Z SamAnd Group Photo (JOI21_ho_t3) C++17
100 / 100
647 ms 196336 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 5003;

void por()
{
    int n;
    cin >> n;
    vector<int> v;
    for (int i = 1; i <= n; ++i)
        v.push_back(i);

    do
    {
        bool z = true;
        for (int i = 0; i < n - 1; ++i)
        {
            if (v[i] >= v[i + 1] + 2)
            {
                z = false;
                break;
            }
        }
        if (z)
        {
            for (int i = 0; i < n; ++i)
                cout << v[i] << ' ';
            cout << "\n";
        }
    } while (next_permutation(all(v)));
}

int n;
int a[N];

int b[N];
int l[N][N], r[N][N];

int dp[N];

void solv()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    for (int i = 1; i <= n; ++i)
    {
        memset(b, 0, sizeof b);
        for (int j = 1; j < i; ++j)
            b[a[j]]++;
        for (int j = 1; j <= n; ++j)
            l[a[i]][j] = l[a[i]][j - 1] + b[j];

        memset(b, 0, sizeof b);
        for (int j = i + 1; j <= n; ++j)
            b[a[j]]++;
        for (int j = 1; j <= n; ++j)
            r[a[i]][j] = r[a[i]][j - 1] + b[j];
    }

    for (int x = 1; x <= n; ++x)
    {
        int s = l[x][n] - l[x][x];
        dp[x] = dp[x - 1] + s;
        for (int y = x - 1; y >= 1; --y)
        {
            s += l[y][n] - l[y][x];
            s += r[y][x] - r[y][y];
            dp[x] = min(dp[x], dp[y - 1] + s);
        }
    }

    cout << dp[n] << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 472 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 540 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 472 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 540 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 2256 KB Output is correct
22 Correct 2 ms 2260 KB Output is correct
23 Correct 2 ms 2260 KB Output is correct
24 Correct 2 ms 2260 KB Output is correct
25 Correct 2 ms 2260 KB Output is correct
26 Correct 2 ms 2260 KB Output is correct
27 Correct 2 ms 2260 KB Output is correct
28 Correct 2 ms 2252 KB Output is correct
29 Correct 2 ms 2260 KB Output is correct
30 Correct 2 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 472 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 540 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 2256 KB Output is correct
22 Correct 2 ms 2260 KB Output is correct
23 Correct 2 ms 2260 KB Output is correct
24 Correct 2 ms 2260 KB Output is correct
25 Correct 2 ms 2260 KB Output is correct
26 Correct 2 ms 2260 KB Output is correct
27 Correct 2 ms 2260 KB Output is correct
28 Correct 2 ms 2252 KB Output is correct
29 Correct 2 ms 2260 KB Output is correct
30 Correct 2 ms 2260 KB Output is correct
31 Correct 16 ms 11732 KB Output is correct
32 Correct 15 ms 11732 KB Output is correct
33 Correct 16 ms 11748 KB Output is correct
34 Correct 15 ms 11800 KB Output is correct
35 Correct 15 ms 11772 KB Output is correct
36 Correct 16 ms 11732 KB Output is correct
37 Correct 17 ms 11732 KB Output is correct
38 Correct 15 ms 11732 KB Output is correct
39 Correct 16 ms 11812 KB Output is correct
40 Correct 16 ms 11772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 472 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 540 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 2256 KB Output is correct
22 Correct 2 ms 2260 KB Output is correct
23 Correct 2 ms 2260 KB Output is correct
24 Correct 2 ms 2260 KB Output is correct
25 Correct 2 ms 2260 KB Output is correct
26 Correct 2 ms 2260 KB Output is correct
27 Correct 2 ms 2260 KB Output is correct
28 Correct 2 ms 2252 KB Output is correct
29 Correct 2 ms 2260 KB Output is correct
30 Correct 2 ms 2260 KB Output is correct
31 Correct 16 ms 11732 KB Output is correct
32 Correct 15 ms 11732 KB Output is correct
33 Correct 16 ms 11748 KB Output is correct
34 Correct 15 ms 11800 KB Output is correct
35 Correct 15 ms 11772 KB Output is correct
36 Correct 16 ms 11732 KB Output is correct
37 Correct 17 ms 11732 KB Output is correct
38 Correct 15 ms 11732 KB Output is correct
39 Correct 16 ms 11812 KB Output is correct
40 Correct 16 ms 11772 KB Output is correct
41 Correct 624 ms 196220 KB Output is correct
42 Correct 619 ms 196260 KB Output is correct
43 Correct 615 ms 196236 KB Output is correct
44 Correct 614 ms 196172 KB Output is correct
45 Correct 622 ms 196164 KB Output is correct
46 Correct 614 ms 196212 KB Output is correct
47 Correct 612 ms 196216 KB Output is correct
48 Correct 610 ms 196192 KB Output is correct
49 Correct 614 ms 196216 KB Output is correct
50 Correct 647 ms 196304 KB Output is correct
51 Correct 616 ms 196204 KB Output is correct
52 Correct 619 ms 196268 KB Output is correct
53 Correct 610 ms 196336 KB Output is correct
54 Correct 616 ms 196172 KB Output is correct
55 Correct 610 ms 196188 KB Output is correct