답안 #798683

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
798683 2023-07-30T23:03:12 Z Johann Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 13600 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const ll INF = 1e18;
int N, Q;
vi A, AA;
ll ans;

vi split(int x)
{
    vi sp;
    int j = 0;
    while (2 * (1 << j) <= x)
    {
        sp.push_back(1 << j);
        x -= 1 << j;
        sp.push_back(1 << j);
        x -= 1 << j;
        ++j;
    }
    j = 0;
    while (x > 0)
    {
        if (x & (1 << j))
            sp.push_back(1 << j), x ^= 1 << j;
        ++j;
    }
    return sp;
}

ll numberOfSubsequences(ll n)
{
    return n * (n + 1) / 2;
}
ll totalLengthOfSubsequences(ll n)
{
    return n * (n + 1) * (n + 1) / 2 - (2 * n + 1) * (n + 1) * n / 6;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> Q;
    AA.resize(N);
    for (int i = 0; i < N; ++i)
        cin >> AA[i];

    while (Q--)
    {
        {
            int l, r;
            cin >> l >> r;
            --l;
            A = vi(AA.begin() + l, AA.begin() + r);
        }
        ll n = sz(A);

        vi heatMap(*max_element(all(A)) + 1, 0);
        for (int i = 0; i < sz(A); ++i)
            ++heatMap[A[i]];
        map<int, int> cnts;
        for (int i = 0; i < sz(heatMap); ++i)
            if (heatMap[i])
                ++cnts[heatMap[i]];

        vpii S;
        for (auto it = cnts.begin(); it != cnts.end(); ++it)
            for (int x : split(it->second))
                S.push_back({it->first, x});

        ll pref = 0;
        int num = sz(S);

        vi dp[2];
        for (int foo = 0; foo < 2; ++foo)
            dp[foo].reserve(N + 1), dp[foo].resize(N + 1);
        fill(all(dp[0]), INF);
        dp[0][0] = totalLengthOfSubsequences(n);

        for (int j = 0; j < num; ++j)
        {
            int ii = j & 1;
            int ni = ii ^ 1;
            fill(all(dp[ni]), INF);

            ll x = S[j].first;  // how much
            ll y = S[j].second; // how often
            ll len = x * y;

            ll subsure = 0;
            // if both are in set
            subsure += totalLengthOfSubsequences(len) - x * x * (totalLengthOfSubsequences(y) - y) - y * numberOfSubsequences(x);

            // either with left or right endpoint in set
            ll factor = len * (len + 1) / 2 - x * y * (y + 1) / 2;
            subsure += factor * (n - len);

            for (ll left = 0; left < sz(dp[ii]); ++left)
            {
                ll tmp = dp[ii][left];
                if (tmp == INF)
                    continue;
                tmp -= subsure;

                // left
                dp[ni][left + len] = min(dp[ni][left + len], tmp - (len - y) * left * (n - left - len));
                // right
                ll right = pref - left;
                dp[ni][left] = min(dp[ni][left], tmp - (len - y) * right * (n - right - len));
            }
            pref += len;
        }
        ans = *min_element(all(dp[num & 1]));
        cout << ans << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 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 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
5 Correct 13 ms 6484 KB Output is correct
6 Correct 20 ms 9684 KB Output is correct
7 Correct 20 ms 9720 KB Output is correct
8 Correct 23 ms 9828 KB Output is correct
9 Correct 20 ms 9720 KB Output is correct
10 Correct 21 ms 9716 KB Output is correct
11 Correct 20 ms 9716 KB Output is correct
12 Correct 20 ms 9684 KB Output is correct
13 Correct 20 ms 9720 KB Output is correct
14 Correct 19 ms 9720 KB Output is correct
15 Correct 20 ms 9712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
5 Correct 13 ms 6484 KB Output is correct
6 Correct 20 ms 9684 KB Output is correct
7 Correct 20 ms 9720 KB Output is correct
8 Correct 23 ms 9828 KB Output is correct
9 Correct 20 ms 9720 KB Output is correct
10 Correct 21 ms 9716 KB Output is correct
11 Correct 20 ms 9716 KB Output is correct
12 Correct 20 ms 9684 KB Output is correct
13 Correct 20 ms 9720 KB Output is correct
14 Correct 19 ms 9720 KB Output is correct
15 Correct 20 ms 9712 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 9 ms 3412 KB Output is correct
20 Correct 18 ms 6484 KB Output is correct
21 Correct 27 ms 9684 KB Output is correct
22 Correct 27 ms 9684 KB Output is correct
23 Correct 27 ms 9716 KB Output is correct
24 Correct 28 ms 9716 KB Output is correct
25 Correct 28 ms 9684 KB Output is correct
26 Correct 28 ms 9684 KB Output is correct
27 Correct 28 ms 9684 KB Output is correct
28 Correct 27 ms 9724 KB Output is correct
29 Correct 28 ms 9720 KB Output is correct
30 Correct 27 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
5 Correct 13 ms 6484 KB Output is correct
6 Correct 20 ms 9684 KB Output is correct
7 Correct 20 ms 9720 KB Output is correct
8 Correct 23 ms 9828 KB Output is correct
9 Correct 20 ms 9720 KB Output is correct
10 Correct 21 ms 9716 KB Output is correct
11 Correct 20 ms 9716 KB Output is correct
12 Correct 20 ms 9684 KB Output is correct
13 Correct 20 ms 9720 KB Output is correct
14 Correct 19 ms 9720 KB Output is correct
15 Correct 20 ms 9712 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 9 ms 3412 KB Output is correct
20 Correct 18 ms 6484 KB Output is correct
21 Correct 27 ms 9684 KB Output is correct
22 Correct 27 ms 9684 KB Output is correct
23 Correct 27 ms 9716 KB Output is correct
24 Correct 28 ms 9716 KB Output is correct
25 Correct 28 ms 9684 KB Output is correct
26 Correct 28 ms 9684 KB Output is correct
27 Correct 28 ms 9684 KB Output is correct
28 Correct 27 ms 9724 KB Output is correct
29 Correct 28 ms 9720 KB Output is correct
30 Correct 27 ms 9684 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 312 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 564 KB Output is correct
35 Correct 4 ms 652 KB Output is correct
36 Correct 6 ms 596 KB Output is correct
37 Correct 36 ms 2004 KB Output is correct
38 Correct 52 ms 2120 KB Output is correct
39 Correct 123 ms 3888 KB Output is correct
40 Correct 281 ms 7404 KB Output is correct
41 Correct 457 ms 10916 KB Output is correct
42 Correct 443 ms 10920 KB Output is correct
43 Correct 466 ms 10912 KB Output is correct
44 Correct 448 ms 10916 KB Output is correct
45 Correct 447 ms 11036 KB Output is correct
46 Correct 438 ms 10856 KB Output is correct
47 Correct 439 ms 10912 KB Output is correct
48 Correct 443 ms 10912 KB Output is correct
49 Correct 442 ms 10916 KB Output is correct
50 Correct 448 ms 10912 KB Output is correct
51 Correct 441 ms 10872 KB Output is correct
52 Correct 447 ms 10972 KB Output is correct
53 Correct 443 ms 10912 KB Output is correct
54 Correct 452 ms 10908 KB Output is correct
55 Correct 450 ms 10912 KB Output is correct
56 Correct 439 ms 10916 KB Output is correct
57 Correct 450 ms 10908 KB Output is correct
58 Correct 443 ms 10828 KB Output is correct
59 Correct 452 ms 10920 KB Output is correct
60 Correct 446 ms 10828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 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 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 9 ms 3412 KB Output is correct
15 Correct 13 ms 6484 KB Output is correct
16 Correct 20 ms 9684 KB Output is correct
17 Correct 20 ms 9720 KB Output is correct
18 Correct 23 ms 9828 KB Output is correct
19 Correct 20 ms 9720 KB Output is correct
20 Correct 21 ms 9716 KB Output is correct
21 Correct 20 ms 9716 KB Output is correct
22 Correct 20 ms 9684 KB Output is correct
23 Correct 20 ms 9720 KB Output is correct
24 Correct 19 ms 9720 KB Output is correct
25 Correct 20 ms 9712 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 9 ms 3412 KB Output is correct
30 Correct 18 ms 6484 KB Output is correct
31 Correct 27 ms 9684 KB Output is correct
32 Correct 27 ms 9684 KB Output is correct
33 Correct 27 ms 9716 KB Output is correct
34 Correct 28 ms 9716 KB Output is correct
35 Correct 28 ms 9684 KB Output is correct
36 Correct 28 ms 9684 KB Output is correct
37 Correct 28 ms 9684 KB Output is correct
38 Correct 27 ms 9724 KB Output is correct
39 Correct 28 ms 9720 KB Output is correct
40 Correct 27 ms 9684 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 312 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 564 KB Output is correct
45 Correct 4 ms 652 KB Output is correct
46 Correct 6 ms 596 KB Output is correct
47 Correct 36 ms 2004 KB Output is correct
48 Correct 52 ms 2120 KB Output is correct
49 Correct 123 ms 3888 KB Output is correct
50 Correct 281 ms 7404 KB Output is correct
51 Correct 457 ms 10916 KB Output is correct
52 Correct 443 ms 10920 KB Output is correct
53 Correct 466 ms 10912 KB Output is correct
54 Correct 448 ms 10916 KB Output is correct
55 Correct 447 ms 11036 KB Output is correct
56 Correct 438 ms 10856 KB Output is correct
57 Correct 439 ms 10912 KB Output is correct
58 Correct 443 ms 10912 KB Output is correct
59 Correct 442 ms 10916 KB Output is correct
60 Correct 448 ms 10912 KB Output is correct
61 Correct 441 ms 10872 KB Output is correct
62 Correct 447 ms 10972 KB Output is correct
63 Correct 443 ms 10912 KB Output is correct
64 Correct 452 ms 10908 KB Output is correct
65 Correct 450 ms 10912 KB Output is correct
66 Correct 439 ms 10916 KB Output is correct
67 Correct 450 ms 10908 KB Output is correct
68 Correct 443 ms 10828 KB Output is correct
69 Correct 452 ms 10920 KB Output is correct
70 Correct 446 ms 10828 KB Output is correct
71 Correct 57 ms 2132 KB Output is correct
72 Correct 57 ms 2152 KB Output is correct
73 Correct 58 ms 2132 KB Output is correct
74 Correct 56 ms 2132 KB Output is correct
75 Correct 56 ms 2100 KB Output is correct
76 Correct 152 ms 3944 KB Output is correct
77 Correct 148 ms 3992 KB Output is correct
78 Correct 155 ms 3924 KB Output is correct
79 Correct 147 ms 3924 KB Output is correct
80 Correct 149 ms 3924 KB Output is correct
81 Correct 385 ms 7672 KB Output is correct
82 Correct 390 ms 7668 KB Output is correct
83 Correct 397 ms 7672 KB Output is correct
84 Correct 406 ms 7668 KB Output is correct
85 Correct 401 ms 7684 KB Output is correct
86 Correct 317 ms 7760 KB Output is correct
87 Correct 310 ms 7848 KB Output is correct
88 Correct 311 ms 7968 KB Output is correct
89 Correct 311 ms 7844 KB Output is correct
90 Correct 321 ms 7836 KB Output is correct
91 Correct 272 ms 12244 KB Output is correct
92 Correct 276 ms 12240 KB Output is correct
93 Correct 267 ms 12108 KB Output is correct
94 Correct 261 ms 12128 KB Output is correct
95 Correct 278 ms 12232 KB Output is correct
96 Correct 146 ms 13136 KB Output is correct
97 Correct 155 ms 13168 KB Output is correct
98 Correct 153 ms 13168 KB Output is correct
99 Correct 160 ms 13152 KB Output is correct
100 Correct 157 ms 13172 KB Output is correct
101 Correct 161 ms 13172 KB Output is correct
102 Correct 161 ms 13172 KB Output is correct
103 Correct 155 ms 13044 KB Output is correct
104 Correct 154 ms 13172 KB Output is correct
105 Correct 152 ms 13132 KB Output is correct
106 Correct 123 ms 13516 KB Output is correct
107 Correct 113 ms 13524 KB Output is correct
108 Correct 118 ms 13592 KB Output is correct
109 Correct 114 ms 13476 KB Output is correct
110 Correct 119 ms 13532 KB Output is correct
111 Correct 127 ms 13600 KB Output is correct
112 Correct 110 ms 13536 KB Output is correct
113 Correct 114 ms 13596 KB Output is correct
114 Correct 121 ms 13516 KB Output is correct
115 Correct 123 ms 13600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 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 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 9 ms 3412 KB Output is correct
15 Correct 13 ms 6484 KB Output is correct
16 Correct 20 ms 9684 KB Output is correct
17 Correct 20 ms 9720 KB Output is correct
18 Correct 23 ms 9828 KB Output is correct
19 Correct 20 ms 9720 KB Output is correct
20 Correct 21 ms 9716 KB Output is correct
21 Correct 20 ms 9716 KB Output is correct
22 Correct 20 ms 9684 KB Output is correct
23 Correct 20 ms 9720 KB Output is correct
24 Correct 19 ms 9720 KB Output is correct
25 Correct 20 ms 9712 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 9 ms 3412 KB Output is correct
30 Correct 18 ms 6484 KB Output is correct
31 Correct 27 ms 9684 KB Output is correct
32 Correct 27 ms 9684 KB Output is correct
33 Correct 27 ms 9716 KB Output is correct
34 Correct 28 ms 9716 KB Output is correct
35 Correct 28 ms 9684 KB Output is correct
36 Correct 28 ms 9684 KB Output is correct
37 Correct 28 ms 9684 KB Output is correct
38 Correct 27 ms 9724 KB Output is correct
39 Correct 28 ms 9720 KB Output is correct
40 Correct 27 ms 9684 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 312 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 564 KB Output is correct
45 Correct 4 ms 652 KB Output is correct
46 Correct 6 ms 596 KB Output is correct
47 Correct 36 ms 2004 KB Output is correct
48 Correct 52 ms 2120 KB Output is correct
49 Correct 123 ms 3888 KB Output is correct
50 Correct 281 ms 7404 KB Output is correct
51 Correct 457 ms 10916 KB Output is correct
52 Correct 443 ms 10920 KB Output is correct
53 Correct 466 ms 10912 KB Output is correct
54 Correct 448 ms 10916 KB Output is correct
55 Correct 447 ms 11036 KB Output is correct
56 Correct 438 ms 10856 KB Output is correct
57 Correct 439 ms 10912 KB Output is correct
58 Correct 443 ms 10912 KB Output is correct
59 Correct 442 ms 10916 KB Output is correct
60 Correct 448 ms 10912 KB Output is correct
61 Correct 441 ms 10872 KB Output is correct
62 Correct 447 ms 10972 KB Output is correct
63 Correct 443 ms 10912 KB Output is correct
64 Correct 452 ms 10908 KB Output is correct
65 Correct 450 ms 10912 KB Output is correct
66 Correct 439 ms 10916 KB Output is correct
67 Correct 450 ms 10908 KB Output is correct
68 Correct 443 ms 10828 KB Output is correct
69 Correct 452 ms 10920 KB Output is correct
70 Correct 446 ms 10828 KB Output is correct
71 Correct 57 ms 2132 KB Output is correct
72 Correct 57 ms 2152 KB Output is correct
73 Correct 58 ms 2132 KB Output is correct
74 Correct 56 ms 2132 KB Output is correct
75 Correct 56 ms 2100 KB Output is correct
76 Correct 152 ms 3944 KB Output is correct
77 Correct 148 ms 3992 KB Output is correct
78 Correct 155 ms 3924 KB Output is correct
79 Correct 147 ms 3924 KB Output is correct
80 Correct 149 ms 3924 KB Output is correct
81 Correct 385 ms 7672 KB Output is correct
82 Correct 390 ms 7668 KB Output is correct
83 Correct 397 ms 7672 KB Output is correct
84 Correct 406 ms 7668 KB Output is correct
85 Correct 401 ms 7684 KB Output is correct
86 Correct 317 ms 7760 KB Output is correct
87 Correct 310 ms 7848 KB Output is correct
88 Correct 311 ms 7968 KB Output is correct
89 Correct 311 ms 7844 KB Output is correct
90 Correct 321 ms 7836 KB Output is correct
91 Correct 272 ms 12244 KB Output is correct
92 Correct 276 ms 12240 KB Output is correct
93 Correct 267 ms 12108 KB Output is correct
94 Correct 261 ms 12128 KB Output is correct
95 Correct 278 ms 12232 KB Output is correct
96 Correct 146 ms 13136 KB Output is correct
97 Correct 155 ms 13168 KB Output is correct
98 Correct 153 ms 13168 KB Output is correct
99 Correct 160 ms 13152 KB Output is correct
100 Correct 157 ms 13172 KB Output is correct
101 Correct 161 ms 13172 KB Output is correct
102 Correct 161 ms 13172 KB Output is correct
103 Correct 155 ms 13044 KB Output is correct
104 Correct 154 ms 13172 KB Output is correct
105 Correct 152 ms 13132 KB Output is correct
106 Correct 123 ms 13516 KB Output is correct
107 Correct 113 ms 13524 KB Output is correct
108 Correct 118 ms 13592 KB Output is correct
109 Correct 114 ms 13476 KB Output is correct
110 Correct 119 ms 13532 KB Output is correct
111 Correct 127 ms 13600 KB Output is correct
112 Correct 110 ms 13536 KB Output is correct
113 Correct 114 ms 13596 KB Output is correct
114 Correct 121 ms 13516 KB Output is correct
115 Correct 123 ms 13600 KB Output is correct
116 Execution timed out 7053 ms 1256 KB Time limit exceeded
117 Halted 0 ms 0 KB -