답안 #999131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999131 2024-06-15T07:08:45 Z IdanRosen Diversity (CEOI21_diversity) C++
4 / 100
6 ms 1696 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;


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

    int n, q;
    cin >> n >> q;

    vector<int> arr(n);
    for (auto &ref: arr) cin >> ref;


    while (q--) {
        int left, right;
        cin >> left >> right;
        left--;

        std::sort(arr.begin(), arr.end());

        vector<int> lens;
        {
            vector<int> raw_lens;
            int last = -1;
            for (int i = 0; i < n; i++) {
                if (last == -1) {
                    last = i;
                    continue;
                }

                if (arr[i] == arr[last]) {}
                else {
                    raw_lens.push_back(i - last);
                    last = i;
                }
            }
            raw_lens.push_back(n - last);
            std::sort(raw_lens.begin(), raw_lens.end());

            vector<int> first, second;
            int i = 0;
            while (true) {
                if (i < raw_lens.size()) {
                    first.push_back(raw_lens[i]);
                    i++;
                } else break;

                if (i < raw_lens.size()) {
                    second.push_back(raw_lens[i]);
                    i++;
                } else break;
            }

            for (int l: first)
                lens.push_back(l);
            for (auto it = second.rbegin(); it != second.rend(); it++)
                lens.push_back(*it);
        }

        struct ptr {
            int in;
            int cnt;
        };
        auto advance = [&](ptr &ptr) {
            if (ptr.cnt + 1 == lens[ptr.in]) {
                ptr.cnt = 0;
                ptr.in++;
            } else ptr.cnt++;
        };

        int total = 0;
        {
            int bias = 0;
            vector<int> uniques;
            {
                ptr first{0, 0};
                ptr second{0, 0};
                for (int sublen = 1; sublen <= n; sublen++) {

                    uniques.push_back(second.in - first.in + 1);
                    advance(second);
                }
            }

            int sum = accumulate(uniques.begin(), uniques.end(), 0);
            total = sum;
            for (int i = 1; i < uniques.size(); i++) {
                sum -= (uniques[i - 1] - bias);

                if ((uniques[i] - bias) == 2) {
                    bias++;
                    sum -= uniques.size() - i;
                }

                total += sum;
            }
        }

        cout << total << "\n";
    }

}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |                 if (i < raw_lens.size()) {
      |                     ~~^~~~~~~~~~~~~~~~~
diversity.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |                 if (i < raw_lens.size()) {
      |                     ~~^~~~~~~~~~~~~~~~~
diversity.cpp:96:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |             for (int i = 1; i < uniques.size(); i++) {
      |                             ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Incorrect 6 ms 1696 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Incorrect 6 ms 1696 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Incorrect 6 ms 1696 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Incorrect 6 ms 1696 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Incorrect 6 ms 1696 KB Output isn't correct
15 Halted 0 ms 0 KB -