답안 #551321

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551321 2022-04-20T10:00:56 Z apostoldaniel854 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 16412 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define dbg(x) cerr << #x << " " << x << "\n"

const int MAX_N = 3e5;
int n, Q;
int a[1 + MAX_N];

ll solve(vector <int> &v) {
    int sz = v.size ();
    ll ans = 0;
    for (int l = 0; l < sz; l++) {
        map <int, int> freq;
        for (int r = l; r < sz; r++) {
            freq[v[r]]++;
            ans += freq.size();
        }
    }
    return ans;
}

ll brute_force() {
    vector <int> order(n);
    for (int i = 0; i < n; i++)
        order[i] = i + 1;
    ll ans = (1ll << 60);
    vector <int> best;
    do {
        vector <int> v;
        for (int i : order)
            v.push_back(a[i]);
        ll cnt = solve(v);
        if (ans > cnt) {
            best = v;
            ans = cnt;
        }
    } while (next_permutation(order.begin(), order.end()));
    cout << ans << "\n";
    for (int x : best)
        cout << x << " ";
    cout << "\n";
    return ans;
}

/// maybe we need to add the one with lowest freq closer to the edges
/// can we do this greedy or just by dp? greedy works!

int freq[1 + MAX_N];

ll get_div(int l, int r, int sz) {
    ll ans = 0;
    /// out out
    ans += 1ll * (l - 1) * (sz - r);
    /// in out
    ans += 1ll * (r - l + 1) * (sz - r);
    /// out in
    ans += 1ll * (r - l + 1) * (l - 1);
    /// in in
    ans += 1ll * (r - l + 1) * (r - l + 2) / 2;
 //   dbg (ans);
    return ans;
}

ll greedy_v1() {
    for (int i = 1; i <= n; i++)
        freq[a[i]]++;
    vector <int> values;
    for (int i = 1; i <= MAX_N; i++)
        if (freq[i])
            values.push_back(freq[i]);
    sort (values.begin(), values.end());
    int l = 1, r = n;
    int putL = 0, putR = 0;
    ll ans = 0;
    for (int cnt : values) {
        if (putL < putR) {
            putL += cnt;
            ans += get_div(l, l + cnt - 1, n);
            l += cnt;
        }
        else {
            putR += cnt;
            ans += get_div(r - cnt + 1, r, n);
            r -= cnt;
        }
    }
    return ans;
}

const int RADMAX = 500;

struct Query {
  int st;
  int dr;
  int ind;
};

vector <Query> q[RADMAX];

ll sol[1 + MAX_N];
int car[1 + MAX_N];
bool cmp (Query a, Query b) {
  return a.dr < b.dr;
}

int rad;

inline int bucket (int x) {
  return x / rad;
}
multiset <int> st;
void upd(int value, int val) {
    if (car[value])
        st.erase(st.find(car[value]));
    car[value] += val;
    if (car[value])
        st.insert(car[value]);
}
ll get(int L, int R){
    int putL = 0, putR = 0;
    ll ans = 0;
    int l = 1, r = R - L + 1;
    for (int cnt : st) {
        if (putL < putR) {
            putL += cnt;
            ans += get_div(l, l + cnt - 1, R - L + 1);
            l += cnt;
        }
        else {
            putR += cnt;
            ans += get_div(r - cnt + 1, r, R - L + 1);
            r -= cnt;
        }
    }
    return ans;
}
void query_mode() {
    rad = sqrt(n);
    for (int i = 1; i <= Q; i++) {
        int x, y;
        cin >> x >> y;
        q[bucket (x)].push_back ({x, y, i});
    }
    for (int b = 0; b <= rad; b++) {
        sort (q[b].begin (), q[b].end (), cmp);
        memset (car, 0, sizeof (car));
        st.clear();
        int dr = (b + 1) * rad;
        for (auto w : q[b]) {
          while (dr <= w.dr) {
            upd(a[dr], 1);
            dr++;
          }
          int bound = min (w.dr, (b + 1) * rad - 1);
          for (int i = w.st; i <= bound; i++)
            upd(a[i], 1);
          sol[w.ind] = get(w.st, w.dr);
          for (int i = w.st; i <= bound; i++)
            upd(a[i], -1);
        }
    }
    for (int i = 1; i <= Q; i++)
        cout << sol[i] << "\n";
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> Q;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    query_mode();
    /*
    while (q--) {
        int l, r;
        cin >> l >> r;
    }
    ll ans_brute = brute_force();
    ll ans_greddy = greedy_v1();
    cout << ans_brute << " " << ans_greddy << "\n";*/
    return 0;
}
/**
4 2
1 1 1 1
1 2
2 4

**/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1500 KB Output is correct
4 Correct 1 ms 1500 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1500 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1492 KB Output is correct
2 Correct 3 ms 1500 KB Output is correct
3 Correct 8 ms 1492 KB Output is correct
4 Correct 25 ms 2164 KB Output is correct
5 Correct 38 ms 2596 KB Output is correct
6 Correct 51 ms 3360 KB Output is correct
7 Correct 47 ms 3284 KB Output is correct
8 Correct 52 ms 3260 KB Output is correct
9 Correct 51 ms 3284 KB Output is correct
10 Correct 49 ms 3272 KB Output is correct
11 Correct 57 ms 3312 KB Output is correct
12 Correct 69 ms 3344 KB Output is correct
13 Correct 50 ms 3276 KB Output is correct
14 Correct 49 ms 3212 KB Output is correct
15 Correct 48 ms 3284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1492 KB Output is correct
2 Correct 3 ms 1500 KB Output is correct
3 Correct 8 ms 1492 KB Output is correct
4 Correct 25 ms 2164 KB Output is correct
5 Correct 38 ms 2596 KB Output is correct
6 Correct 51 ms 3360 KB Output is correct
7 Correct 47 ms 3284 KB Output is correct
8 Correct 52 ms 3260 KB Output is correct
9 Correct 51 ms 3284 KB Output is correct
10 Correct 49 ms 3272 KB Output is correct
11 Correct 57 ms 3312 KB Output is correct
12 Correct 69 ms 3344 KB Output is correct
13 Correct 50 ms 3276 KB Output is correct
14 Correct 49 ms 3212 KB Output is correct
15 Correct 48 ms 3284 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 2 ms 1492 KB Output is correct
18 Correct 5 ms 1588 KB Output is correct
19 Correct 24 ms 2148 KB Output is correct
20 Correct 39 ms 2772 KB Output is correct
21 Correct 54 ms 3500 KB Output is correct
22 Correct 62 ms 3468 KB Output is correct
23 Correct 64 ms 3484 KB Output is correct
24 Correct 65 ms 3416 KB Output is correct
25 Correct 58 ms 3460 KB Output is correct
26 Correct 57 ms 3432 KB Output is correct
27 Correct 58 ms 3456 KB Output is correct
28 Correct 59 ms 3408 KB Output is correct
29 Correct 63 ms 3428 KB Output is correct
30 Correct 63 ms 3484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1492 KB Output is correct
2 Correct 3 ms 1500 KB Output is correct
3 Correct 8 ms 1492 KB Output is correct
4 Correct 25 ms 2164 KB Output is correct
5 Correct 38 ms 2596 KB Output is correct
6 Correct 51 ms 3360 KB Output is correct
7 Correct 47 ms 3284 KB Output is correct
8 Correct 52 ms 3260 KB Output is correct
9 Correct 51 ms 3284 KB Output is correct
10 Correct 49 ms 3272 KB Output is correct
11 Correct 57 ms 3312 KB Output is correct
12 Correct 69 ms 3344 KB Output is correct
13 Correct 50 ms 3276 KB Output is correct
14 Correct 49 ms 3212 KB Output is correct
15 Correct 48 ms 3284 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 2 ms 1492 KB Output is correct
18 Correct 5 ms 1588 KB Output is correct
19 Correct 24 ms 2148 KB Output is correct
20 Correct 39 ms 2772 KB Output is correct
21 Correct 54 ms 3500 KB Output is correct
22 Correct 62 ms 3468 KB Output is correct
23 Correct 64 ms 3484 KB Output is correct
24 Correct 65 ms 3416 KB Output is correct
25 Correct 58 ms 3460 KB Output is correct
26 Correct 57 ms 3432 KB Output is correct
27 Correct 58 ms 3456 KB Output is correct
28 Correct 59 ms 3408 KB Output is correct
29 Correct 63 ms 3428 KB Output is correct
30 Correct 63 ms 3484 KB Output is correct
31 Correct 2 ms 1492 KB Output is correct
32 Correct 2 ms 1492 KB Output is correct
33 Correct 2 ms 1492 KB Output is correct
34 Correct 6 ms 1600 KB Output is correct
35 Correct 8 ms 1492 KB Output is correct
36 Correct 6 ms 1620 KB Output is correct
37 Correct 17 ms 1928 KB Output is correct
38 Correct 20 ms 2004 KB Output is correct
39 Correct 31 ms 2276 KB Output is correct
40 Correct 62 ms 3108 KB Output is correct
41 Correct 90 ms 3900 KB Output is correct
42 Correct 91 ms 3876 KB Output is correct
43 Correct 78 ms 3788 KB Output is correct
44 Correct 79 ms 3888 KB Output is correct
45 Correct 82 ms 3920 KB Output is correct
46 Correct 80 ms 3904 KB Output is correct
47 Correct 80 ms 3748 KB Output is correct
48 Correct 83 ms 3812 KB Output is correct
49 Correct 83 ms 3864 KB Output is correct
50 Correct 83 ms 3808 KB Output is correct
51 Correct 84 ms 3872 KB Output is correct
52 Correct 80 ms 3788 KB Output is correct
53 Correct 83 ms 3788 KB Output is correct
54 Correct 81 ms 3788 KB Output is correct
55 Correct 89 ms 3776 KB Output is correct
56 Correct 80 ms 3796 KB Output is correct
57 Correct 84 ms 3864 KB Output is correct
58 Correct 80 ms 3872 KB Output is correct
59 Correct 87 ms 3788 KB Output is correct
60 Correct 90 ms 3876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1500 KB Output is correct
4 Correct 1 ms 1500 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1500 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 2 ms 1492 KB Output is correct
12 Correct 3 ms 1500 KB Output is correct
13 Correct 8 ms 1492 KB Output is correct
14 Correct 25 ms 2164 KB Output is correct
15 Correct 38 ms 2596 KB Output is correct
16 Correct 51 ms 3360 KB Output is correct
17 Correct 47 ms 3284 KB Output is correct
18 Correct 52 ms 3260 KB Output is correct
19 Correct 51 ms 3284 KB Output is correct
20 Correct 49 ms 3272 KB Output is correct
21 Correct 57 ms 3312 KB Output is correct
22 Correct 69 ms 3344 KB Output is correct
23 Correct 50 ms 3276 KB Output is correct
24 Correct 49 ms 3212 KB Output is correct
25 Correct 48 ms 3284 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 2 ms 1492 KB Output is correct
28 Correct 5 ms 1588 KB Output is correct
29 Correct 24 ms 2148 KB Output is correct
30 Correct 39 ms 2772 KB Output is correct
31 Correct 54 ms 3500 KB Output is correct
32 Correct 62 ms 3468 KB Output is correct
33 Correct 64 ms 3484 KB Output is correct
34 Correct 65 ms 3416 KB Output is correct
35 Correct 58 ms 3460 KB Output is correct
36 Correct 57 ms 3432 KB Output is correct
37 Correct 58 ms 3456 KB Output is correct
38 Correct 59 ms 3408 KB Output is correct
39 Correct 63 ms 3428 KB Output is correct
40 Correct 63 ms 3484 KB Output is correct
41 Correct 2 ms 1492 KB Output is correct
42 Correct 2 ms 1492 KB Output is correct
43 Correct 2 ms 1492 KB Output is correct
44 Correct 6 ms 1600 KB Output is correct
45 Correct 8 ms 1492 KB Output is correct
46 Correct 6 ms 1620 KB Output is correct
47 Correct 17 ms 1928 KB Output is correct
48 Correct 20 ms 2004 KB Output is correct
49 Correct 31 ms 2276 KB Output is correct
50 Correct 62 ms 3108 KB Output is correct
51 Correct 90 ms 3900 KB Output is correct
52 Correct 91 ms 3876 KB Output is correct
53 Correct 78 ms 3788 KB Output is correct
54 Correct 79 ms 3888 KB Output is correct
55 Correct 82 ms 3920 KB Output is correct
56 Correct 80 ms 3904 KB Output is correct
57 Correct 80 ms 3748 KB Output is correct
58 Correct 83 ms 3812 KB Output is correct
59 Correct 83 ms 3864 KB Output is correct
60 Correct 83 ms 3808 KB Output is correct
61 Correct 84 ms 3872 KB Output is correct
62 Correct 80 ms 3788 KB Output is correct
63 Correct 83 ms 3788 KB Output is correct
64 Correct 81 ms 3788 KB Output is correct
65 Correct 89 ms 3776 KB Output is correct
66 Correct 80 ms 3796 KB Output is correct
67 Correct 84 ms 3864 KB Output is correct
68 Correct 80 ms 3872 KB Output is correct
69 Correct 87 ms 3788 KB Output is correct
70 Correct 90 ms 3876 KB Output is correct
71 Correct 21 ms 2028 KB Output is correct
72 Correct 21 ms 2012 KB Output is correct
73 Correct 22 ms 2004 KB Output is correct
74 Correct 19 ms 2032 KB Output is correct
75 Correct 19 ms 1940 KB Output is correct
76 Correct 49 ms 2508 KB Output is correct
77 Correct 40 ms 2592 KB Output is correct
78 Correct 37 ms 2516 KB Output is correct
79 Correct 38 ms 2584 KB Output is correct
80 Correct 37 ms 2516 KB Output is correct
81 Correct 71 ms 3608 KB Output is correct
82 Correct 77 ms 3716 KB Output is correct
83 Correct 75 ms 3668 KB Output is correct
84 Correct 79 ms 3692 KB Output is correct
85 Correct 69 ms 3704 KB Output is correct
86 Correct 77 ms 4172 KB Output is correct
87 Correct 79 ms 4176 KB Output is correct
88 Correct 94 ms 4348 KB Output is correct
89 Correct 82 ms 4284 KB Output is correct
90 Correct 77 ms 4228 KB Output is correct
91 Correct 122 ms 9048 KB Output is correct
92 Correct 127 ms 9036 KB Output is correct
93 Correct 123 ms 9336 KB Output is correct
94 Correct 130 ms 9028 KB Output is correct
95 Correct 135 ms 9036 KB Output is correct
96 Correct 127 ms 13880 KB Output is correct
97 Correct 125 ms 13896 KB Output is correct
98 Correct 117 ms 13900 KB Output is correct
99 Correct 128 ms 13868 KB Output is correct
100 Correct 125 ms 13956 KB Output is correct
101 Correct 118 ms 14076 KB Output is correct
102 Correct 122 ms 13916 KB Output is correct
103 Correct 118 ms 13964 KB Output is correct
104 Correct 117 ms 13920 KB Output is correct
105 Correct 129 ms 14000 KB Output is correct
106 Correct 132 ms 16272 KB Output is correct
107 Correct 120 ms 16292 KB Output is correct
108 Correct 118 ms 16276 KB Output is correct
109 Correct 116 ms 16332 KB Output is correct
110 Correct 117 ms 16348 KB Output is correct
111 Correct 123 ms 16272 KB Output is correct
112 Correct 135 ms 16336 KB Output is correct
113 Correct 123 ms 16412 KB Output is correct
114 Correct 129 ms 16344 KB Output is correct
115 Correct 117 ms 16340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1500 KB Output is correct
4 Correct 1 ms 1500 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1500 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 2 ms 1492 KB Output is correct
12 Correct 3 ms 1500 KB Output is correct
13 Correct 8 ms 1492 KB Output is correct
14 Correct 25 ms 2164 KB Output is correct
15 Correct 38 ms 2596 KB Output is correct
16 Correct 51 ms 3360 KB Output is correct
17 Correct 47 ms 3284 KB Output is correct
18 Correct 52 ms 3260 KB Output is correct
19 Correct 51 ms 3284 KB Output is correct
20 Correct 49 ms 3272 KB Output is correct
21 Correct 57 ms 3312 KB Output is correct
22 Correct 69 ms 3344 KB Output is correct
23 Correct 50 ms 3276 KB Output is correct
24 Correct 49 ms 3212 KB Output is correct
25 Correct 48 ms 3284 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 2 ms 1492 KB Output is correct
28 Correct 5 ms 1588 KB Output is correct
29 Correct 24 ms 2148 KB Output is correct
30 Correct 39 ms 2772 KB Output is correct
31 Correct 54 ms 3500 KB Output is correct
32 Correct 62 ms 3468 KB Output is correct
33 Correct 64 ms 3484 KB Output is correct
34 Correct 65 ms 3416 KB Output is correct
35 Correct 58 ms 3460 KB Output is correct
36 Correct 57 ms 3432 KB Output is correct
37 Correct 58 ms 3456 KB Output is correct
38 Correct 59 ms 3408 KB Output is correct
39 Correct 63 ms 3428 KB Output is correct
40 Correct 63 ms 3484 KB Output is correct
41 Correct 2 ms 1492 KB Output is correct
42 Correct 2 ms 1492 KB Output is correct
43 Correct 2 ms 1492 KB Output is correct
44 Correct 6 ms 1600 KB Output is correct
45 Correct 8 ms 1492 KB Output is correct
46 Correct 6 ms 1620 KB Output is correct
47 Correct 17 ms 1928 KB Output is correct
48 Correct 20 ms 2004 KB Output is correct
49 Correct 31 ms 2276 KB Output is correct
50 Correct 62 ms 3108 KB Output is correct
51 Correct 90 ms 3900 KB Output is correct
52 Correct 91 ms 3876 KB Output is correct
53 Correct 78 ms 3788 KB Output is correct
54 Correct 79 ms 3888 KB Output is correct
55 Correct 82 ms 3920 KB Output is correct
56 Correct 80 ms 3904 KB Output is correct
57 Correct 80 ms 3748 KB Output is correct
58 Correct 83 ms 3812 KB Output is correct
59 Correct 83 ms 3864 KB Output is correct
60 Correct 83 ms 3808 KB Output is correct
61 Correct 84 ms 3872 KB Output is correct
62 Correct 80 ms 3788 KB Output is correct
63 Correct 83 ms 3788 KB Output is correct
64 Correct 81 ms 3788 KB Output is correct
65 Correct 89 ms 3776 KB Output is correct
66 Correct 80 ms 3796 KB Output is correct
67 Correct 84 ms 3864 KB Output is correct
68 Correct 80 ms 3872 KB Output is correct
69 Correct 87 ms 3788 KB Output is correct
70 Correct 90 ms 3876 KB Output is correct
71 Correct 21 ms 2028 KB Output is correct
72 Correct 21 ms 2012 KB Output is correct
73 Correct 22 ms 2004 KB Output is correct
74 Correct 19 ms 2032 KB Output is correct
75 Correct 19 ms 1940 KB Output is correct
76 Correct 49 ms 2508 KB Output is correct
77 Correct 40 ms 2592 KB Output is correct
78 Correct 37 ms 2516 KB Output is correct
79 Correct 38 ms 2584 KB Output is correct
80 Correct 37 ms 2516 KB Output is correct
81 Correct 71 ms 3608 KB Output is correct
82 Correct 77 ms 3716 KB Output is correct
83 Correct 75 ms 3668 KB Output is correct
84 Correct 79 ms 3692 KB Output is correct
85 Correct 69 ms 3704 KB Output is correct
86 Correct 77 ms 4172 KB Output is correct
87 Correct 79 ms 4176 KB Output is correct
88 Correct 94 ms 4348 KB Output is correct
89 Correct 82 ms 4284 KB Output is correct
90 Correct 77 ms 4228 KB Output is correct
91 Correct 122 ms 9048 KB Output is correct
92 Correct 127 ms 9036 KB Output is correct
93 Correct 123 ms 9336 KB Output is correct
94 Correct 130 ms 9028 KB Output is correct
95 Correct 135 ms 9036 KB Output is correct
96 Correct 127 ms 13880 KB Output is correct
97 Correct 125 ms 13896 KB Output is correct
98 Correct 117 ms 13900 KB Output is correct
99 Correct 128 ms 13868 KB Output is correct
100 Correct 125 ms 13956 KB Output is correct
101 Correct 118 ms 14076 KB Output is correct
102 Correct 122 ms 13916 KB Output is correct
103 Correct 118 ms 13964 KB Output is correct
104 Correct 117 ms 13920 KB Output is correct
105 Correct 129 ms 14000 KB Output is correct
106 Correct 132 ms 16272 KB Output is correct
107 Correct 120 ms 16292 KB Output is correct
108 Correct 118 ms 16276 KB Output is correct
109 Correct 116 ms 16332 KB Output is correct
110 Correct 117 ms 16348 KB Output is correct
111 Correct 123 ms 16272 KB Output is correct
112 Correct 135 ms 16336 KB Output is correct
113 Correct 123 ms 16412 KB Output is correct
114 Correct 129 ms 16344 KB Output is correct
115 Correct 117 ms 16340 KB Output is correct
116 Correct 545 ms 3724 KB Output is correct
117 Correct 643 ms 3728 KB Output is correct
118 Correct 1885 ms 4288 KB Output is correct
119 Correct 2598 ms 4376 KB Output is correct
120 Correct 3039 ms 4348 KB Output is correct
121 Correct 5533 ms 4804 KB Output is correct
122 Correct 6445 ms 4852 KB Output is correct
123 Execution timed out 7089 ms 5068 KB Time limit exceeded
124 Halted 0 ms 0 KB -