답안 #589895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589895 2022-07-05T11:24:35 Z alextodoran Diversity (CEOI21_diversity) C++17
64 / 100
58 ms 4556 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 300000;

int N, Q;
int A[N_MAX + 2];

ll solve (map <int, int> &mp) {
    ll sum = 0, cnt = 0;
    ll sumLR = 0, sumRL = 0;
    ll X = 0, Y = 0;
    for (map <int, int> :: reverse_iterator it = mp.rbegin(); it != mp.rend(); it++) {
        ll len, occ; tie(len, occ) = *it;

        ll u = sumRL - sumLR + sum * occ; ll v = (2 * sum);
        ll L = (v == 0 ? 0 : (u >= 0 ? u / v : -(-u / v)));
        L = max(L, (ll) 0); L = min(L, occ - 1);
        ll R = (occ - 1) - L;

        ll aux;
        // place L to the left
        Y += len * (sumRL * L + len * cnt * L * (L - 1) / 2 + len * (L * (L - 1) * (2 * L - 1) / 6 - L * (L - 1) / 2) / 2);
        aux = 0;
        aux += sum * sum * L;
        aux += 2 * sum * len * L * (L - 1) / 2;
        aux += len * len * L * (L - 1) * (2 * L - 1) / 6;
        aux -= X * L + len * len * L * (L - 1) / 2;
        aux /= 2;
        Y += aux;
        X += len * len * L;
        sumLR += sum * L + len * L * (L - 1) / 2;
        sumRL += len * cnt * L + len * L * (L - 1) / 2;
        sum += len * L;
        cnt += L;
        // place R to the right
        Y += len * (sumLR * R + len * cnt * R * (R - 1) / 2 + len * (R * (R - 1) * (2 * R - 1) / 6 - R * (R - 1) / 2) / 2);
        aux = 0;
        aux += sum * sum * R;
        aux += 2 * sum * len * R * (R - 1) / 2;
        aux += len * len * R * (R - 1) * (2 * R - 1) / 6;
        aux -= X * R + len * len * R * (R - 1) / 2;
        aux /= 2;
        Y += aux;
        X += len * len * R;
        sumRL += sum * R + len * R * (R - 1) / 2;
        sumLR += len * cnt * R + len * R * (R - 1) / 2;
        sum += len * R;
        cnt += R;

        if (sumLR < sumRL) {
            Y += sumRL * len + (sum * sum - X) / 2;
            X += len * len;
            sumLR += sum;
            sumRL += len * cnt;
            sum += len;
            cnt += 1;
        } else {
            Y += sumLR * len + (sum * sum - X) / 2;
            X += len * len;
            sumRL += sum;
            sumLR += len * cnt;
            sum += len;
            cnt += 1;
        }
    }
    ll ret = (cnt - 1) * X + 2 * Y;
    ret += (ll) sum * (cnt - 1);
    ret /= 2;
    return ret;
}

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

    cin >> N >> Q;
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
    }
    sort(A + 1, A + N + 1);

    vector <int> v;
    for (int i = 1; i <= N; i++) {
        if (A[i] == A[i - 1]) {
            v.back()++;
        } else {
            v.push_back(1);
        }
    }

    int l, r;
    cin >> l >> r;

    map <int, int> mp;
    for (int len : v) {
        mp[len]++;
    }
    cout << (ll) (int) v.size() * N * (N + 1) / 2 - solve(mp) << "\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 1 ms 328 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 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 7 ms 852 KB Output is correct
5 Correct 14 ms 1416 KB Output is correct
6 Correct 23 ms 2060 KB Output is correct
7 Correct 21 ms 2052 KB Output is correct
8 Correct 23 ms 2056 KB Output is correct
9 Correct 22 ms 2004 KB Output is correct
10 Correct 23 ms 2132 KB Output is correct
11 Correct 24 ms 2132 KB Output is correct
12 Correct 23 ms 2128 KB Output is correct
13 Correct 23 ms 2132 KB Output is correct
14 Correct 22 ms 2132 KB Output is correct
15 Correct 22 ms 2100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 7 ms 852 KB Output is correct
5 Correct 14 ms 1416 KB Output is correct
6 Correct 23 ms 2060 KB Output is correct
7 Correct 21 ms 2052 KB Output is correct
8 Correct 23 ms 2056 KB Output is correct
9 Correct 22 ms 2004 KB Output is correct
10 Correct 23 ms 2132 KB Output is correct
11 Correct 24 ms 2132 KB Output is correct
12 Correct 23 ms 2128 KB Output is correct
13 Correct 23 ms 2132 KB Output is correct
14 Correct 22 ms 2132 KB Output is correct
15 Correct 22 ms 2100 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 8 ms 960 KB Output is correct
20 Correct 18 ms 1728 KB Output is correct
21 Correct 26 ms 2240 KB Output is correct
22 Correct 25 ms 2176 KB Output is correct
23 Correct 25 ms 2276 KB Output is correct
24 Correct 25 ms 2264 KB Output is correct
25 Correct 26 ms 2204 KB Output is correct
26 Correct 25 ms 2260 KB Output is correct
27 Correct 25 ms 2200 KB Output is correct
28 Correct 24 ms 2180 KB Output is correct
29 Correct 25 ms 2104 KB Output is correct
30 Correct 28 ms 2348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 7 ms 852 KB Output is correct
5 Correct 14 ms 1416 KB Output is correct
6 Correct 23 ms 2060 KB Output is correct
7 Correct 21 ms 2052 KB Output is correct
8 Correct 23 ms 2056 KB Output is correct
9 Correct 22 ms 2004 KB Output is correct
10 Correct 23 ms 2132 KB Output is correct
11 Correct 24 ms 2132 KB Output is correct
12 Correct 23 ms 2128 KB Output is correct
13 Correct 23 ms 2132 KB Output is correct
14 Correct 22 ms 2132 KB Output is correct
15 Correct 22 ms 2100 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 8 ms 960 KB Output is correct
20 Correct 18 ms 1728 KB Output is correct
21 Correct 26 ms 2240 KB Output is correct
22 Correct 25 ms 2176 KB Output is correct
23 Correct 25 ms 2276 KB Output is correct
24 Correct 25 ms 2264 KB Output is correct
25 Correct 26 ms 2204 KB Output is correct
26 Correct 25 ms 2260 KB Output is correct
27 Correct 25 ms 2200 KB Output is correct
28 Correct 24 ms 2180 KB Output is correct
29 Correct 25 ms 2104 KB Output is correct
30 Correct 28 ms 2348 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 8 ms 700 KB Output is correct
38 Correct 6 ms 724 KB Output is correct
39 Correct 11 ms 1084 KB Output is correct
40 Correct 22 ms 1876 KB Output is correct
41 Correct 32 ms 2632 KB Output is correct
42 Correct 33 ms 2640 KB Output is correct
43 Correct 33 ms 2636 KB Output is correct
44 Correct 33 ms 2644 KB Output is correct
45 Correct 34 ms 2752 KB Output is correct
46 Correct 40 ms 2636 KB Output is correct
47 Correct 40 ms 2556 KB Output is correct
48 Correct 32 ms 2612 KB Output is correct
49 Correct 31 ms 2644 KB Output is correct
50 Correct 32 ms 2644 KB Output is correct
51 Correct 32 ms 2588 KB Output is correct
52 Correct 32 ms 2612 KB Output is correct
53 Correct 31 ms 2640 KB Output is correct
54 Correct 32 ms 2636 KB Output is correct
55 Correct 32 ms 2640 KB Output is correct
56 Correct 32 ms 2636 KB Output is correct
57 Correct 31 ms 2604 KB Output is correct
58 Correct 31 ms 2620 KB Output is correct
59 Correct 32 ms 2544 KB Output is correct
60 Correct 32 ms 2620 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 1 ms 328 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 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 7 ms 852 KB Output is correct
15 Correct 14 ms 1416 KB Output is correct
16 Correct 23 ms 2060 KB Output is correct
17 Correct 21 ms 2052 KB Output is correct
18 Correct 23 ms 2056 KB Output is correct
19 Correct 22 ms 2004 KB Output is correct
20 Correct 23 ms 2132 KB Output is correct
21 Correct 24 ms 2132 KB Output is correct
22 Correct 23 ms 2128 KB Output is correct
23 Correct 23 ms 2132 KB Output is correct
24 Correct 22 ms 2132 KB Output is correct
25 Correct 22 ms 2100 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 8 ms 960 KB Output is correct
30 Correct 18 ms 1728 KB Output is correct
31 Correct 26 ms 2240 KB Output is correct
32 Correct 25 ms 2176 KB Output is correct
33 Correct 25 ms 2276 KB Output is correct
34 Correct 25 ms 2264 KB Output is correct
35 Correct 26 ms 2204 KB Output is correct
36 Correct 25 ms 2260 KB Output is correct
37 Correct 25 ms 2200 KB Output is correct
38 Correct 24 ms 2180 KB Output is correct
39 Correct 25 ms 2104 KB Output is correct
40 Correct 28 ms 2348 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 8 ms 700 KB Output is correct
48 Correct 6 ms 724 KB Output is correct
49 Correct 11 ms 1084 KB Output is correct
50 Correct 22 ms 1876 KB Output is correct
51 Correct 32 ms 2632 KB Output is correct
52 Correct 33 ms 2640 KB Output is correct
53 Correct 33 ms 2636 KB Output is correct
54 Correct 33 ms 2644 KB Output is correct
55 Correct 34 ms 2752 KB Output is correct
56 Correct 40 ms 2636 KB Output is correct
57 Correct 40 ms 2556 KB Output is correct
58 Correct 32 ms 2612 KB Output is correct
59 Correct 31 ms 2644 KB Output is correct
60 Correct 32 ms 2644 KB Output is correct
61 Correct 32 ms 2588 KB Output is correct
62 Correct 32 ms 2612 KB Output is correct
63 Correct 31 ms 2640 KB Output is correct
64 Correct 32 ms 2636 KB Output is correct
65 Correct 32 ms 2640 KB Output is correct
66 Correct 32 ms 2636 KB Output is correct
67 Correct 31 ms 2604 KB Output is correct
68 Correct 31 ms 2620 KB Output is correct
69 Correct 32 ms 2544 KB Output is correct
70 Correct 32 ms 2620 KB Output is correct
71 Correct 6 ms 744 KB Output is correct
72 Correct 7 ms 724 KB Output is correct
73 Correct 7 ms 668 KB Output is correct
74 Correct 6 ms 664 KB Output is correct
75 Correct 7 ms 724 KB Output is correct
76 Correct 16 ms 1176 KB Output is correct
77 Correct 13 ms 1108 KB Output is correct
78 Correct 14 ms 1368 KB Output is correct
79 Correct 13 ms 1192 KB Output is correct
80 Correct 12 ms 1236 KB Output is correct
81 Correct 25 ms 2080 KB Output is correct
82 Correct 26 ms 2056 KB Output is correct
83 Correct 25 ms 2108 KB Output is correct
84 Correct 26 ms 2044 KB Output is correct
85 Correct 25 ms 2144 KB Output is correct
86 Correct 30 ms 2284 KB Output is correct
87 Correct 32 ms 2320 KB Output is correct
88 Correct 29 ms 2252 KB Output is correct
89 Correct 27 ms 2252 KB Output is correct
90 Correct 28 ms 2244 KB Output is correct
91 Correct 45 ms 3864 KB Output is correct
92 Correct 46 ms 3744 KB Output is correct
93 Correct 45 ms 3768 KB Output is correct
94 Correct 45 ms 3848 KB Output is correct
95 Correct 45 ms 3844 KB Output is correct
96 Correct 47 ms 4508 KB Output is correct
97 Correct 48 ms 4456 KB Output is correct
98 Correct 49 ms 4428 KB Output is correct
99 Correct 48 ms 4516 KB Output is correct
100 Correct 48 ms 4504 KB Output is correct
101 Correct 49 ms 4468 KB Output is correct
102 Correct 48 ms 4432 KB Output is correct
103 Correct 51 ms 4448 KB Output is correct
104 Correct 49 ms 4464 KB Output is correct
105 Correct 49 ms 4428 KB Output is correct
106 Correct 48 ms 4556 KB Output is correct
107 Correct 47 ms 4556 KB Output is correct
108 Correct 48 ms 4480 KB Output is correct
109 Correct 49 ms 4556 KB Output is correct
110 Correct 48 ms 4544 KB Output is correct
111 Correct 58 ms 4556 KB Output is correct
112 Correct 48 ms 4556 KB Output is correct
113 Correct 48 ms 4556 KB Output is correct
114 Correct 49 ms 4520 KB Output is correct
115 Correct 48 ms 4496 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 1 ms 328 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 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 7 ms 852 KB Output is correct
15 Correct 14 ms 1416 KB Output is correct
16 Correct 23 ms 2060 KB Output is correct
17 Correct 21 ms 2052 KB Output is correct
18 Correct 23 ms 2056 KB Output is correct
19 Correct 22 ms 2004 KB Output is correct
20 Correct 23 ms 2132 KB Output is correct
21 Correct 24 ms 2132 KB Output is correct
22 Correct 23 ms 2128 KB Output is correct
23 Correct 23 ms 2132 KB Output is correct
24 Correct 22 ms 2132 KB Output is correct
25 Correct 22 ms 2100 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 8 ms 960 KB Output is correct
30 Correct 18 ms 1728 KB Output is correct
31 Correct 26 ms 2240 KB Output is correct
32 Correct 25 ms 2176 KB Output is correct
33 Correct 25 ms 2276 KB Output is correct
34 Correct 25 ms 2264 KB Output is correct
35 Correct 26 ms 2204 KB Output is correct
36 Correct 25 ms 2260 KB Output is correct
37 Correct 25 ms 2200 KB Output is correct
38 Correct 24 ms 2180 KB Output is correct
39 Correct 25 ms 2104 KB Output is correct
40 Correct 28 ms 2348 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 8 ms 700 KB Output is correct
48 Correct 6 ms 724 KB Output is correct
49 Correct 11 ms 1084 KB Output is correct
50 Correct 22 ms 1876 KB Output is correct
51 Correct 32 ms 2632 KB Output is correct
52 Correct 33 ms 2640 KB Output is correct
53 Correct 33 ms 2636 KB Output is correct
54 Correct 33 ms 2644 KB Output is correct
55 Correct 34 ms 2752 KB Output is correct
56 Correct 40 ms 2636 KB Output is correct
57 Correct 40 ms 2556 KB Output is correct
58 Correct 32 ms 2612 KB Output is correct
59 Correct 31 ms 2644 KB Output is correct
60 Correct 32 ms 2644 KB Output is correct
61 Correct 32 ms 2588 KB Output is correct
62 Correct 32 ms 2612 KB Output is correct
63 Correct 31 ms 2640 KB Output is correct
64 Correct 32 ms 2636 KB Output is correct
65 Correct 32 ms 2640 KB Output is correct
66 Correct 32 ms 2636 KB Output is correct
67 Correct 31 ms 2604 KB Output is correct
68 Correct 31 ms 2620 KB Output is correct
69 Correct 32 ms 2544 KB Output is correct
70 Correct 32 ms 2620 KB Output is correct
71 Correct 6 ms 744 KB Output is correct
72 Correct 7 ms 724 KB Output is correct
73 Correct 7 ms 668 KB Output is correct
74 Correct 6 ms 664 KB Output is correct
75 Correct 7 ms 724 KB Output is correct
76 Correct 16 ms 1176 KB Output is correct
77 Correct 13 ms 1108 KB Output is correct
78 Correct 14 ms 1368 KB Output is correct
79 Correct 13 ms 1192 KB Output is correct
80 Correct 12 ms 1236 KB Output is correct
81 Correct 25 ms 2080 KB Output is correct
82 Correct 26 ms 2056 KB Output is correct
83 Correct 25 ms 2108 KB Output is correct
84 Correct 26 ms 2044 KB Output is correct
85 Correct 25 ms 2144 KB Output is correct
86 Correct 30 ms 2284 KB Output is correct
87 Correct 32 ms 2320 KB Output is correct
88 Correct 29 ms 2252 KB Output is correct
89 Correct 27 ms 2252 KB Output is correct
90 Correct 28 ms 2244 KB Output is correct
91 Correct 45 ms 3864 KB Output is correct
92 Correct 46 ms 3744 KB Output is correct
93 Correct 45 ms 3768 KB Output is correct
94 Correct 45 ms 3848 KB Output is correct
95 Correct 45 ms 3844 KB Output is correct
96 Correct 47 ms 4508 KB Output is correct
97 Correct 48 ms 4456 KB Output is correct
98 Correct 49 ms 4428 KB Output is correct
99 Correct 48 ms 4516 KB Output is correct
100 Correct 48 ms 4504 KB Output is correct
101 Correct 49 ms 4468 KB Output is correct
102 Correct 48 ms 4432 KB Output is correct
103 Correct 51 ms 4448 KB Output is correct
104 Correct 49 ms 4464 KB Output is correct
105 Correct 49 ms 4428 KB Output is correct
106 Correct 48 ms 4556 KB Output is correct
107 Correct 47 ms 4556 KB Output is correct
108 Correct 48 ms 4480 KB Output is correct
109 Correct 49 ms 4556 KB Output is correct
110 Correct 48 ms 4544 KB Output is correct
111 Correct 58 ms 4556 KB Output is correct
112 Correct 48 ms 4556 KB Output is correct
113 Correct 48 ms 4556 KB Output is correct
114 Correct 49 ms 4520 KB Output is correct
115 Correct 48 ms 4496 KB Output is correct
116 Incorrect 2 ms 468 KB Output isn't correct
117 Halted 0 ms 0 KB -