답안 #920257

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
920257 2024-02-02T10:53:32 Z vjudge1 Diversity (CEOI21_diversity) C++17
4 / 100
26 ms 5332 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <set>
#warning That's the baby, that's not my baby

typedef long long ll;

const int NMAX = 3e5;

int a[NMAX + 1];
int f[NMAX + 1];

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

  int n, q;
  std::cin >> n >> q;

  std::vector<int> norm;
  for (int i = 1; i <= n; i++) {
    std::cin >> a[i];
    norm.push_back(a[i]);
  }

  std::sort(norm.begin(), norm.end());
  norm.erase(std::unique(norm.begin(), norm.end()), norm.end());

  for (int i = 1; i <= n; i++) {
    a[i] = std::lower_bound(norm.begin(), norm.end(), a[i]) - norm.begin();
  }

  /**

  1 * (f[1] + f[2] + ... + f[k]) +
  2 * (f[1] * f[2] + f[2] * f[3] + f[3] * f[4] + ... + f[k - 1] * f[k]) +
  3 * (f[1] * f[3] + f[2] * f[4] + ...) +
  ...

  **/

  while (q--) {
    int l, r;
    std::cin >> l >> r;
    std::vector<int> b;
    memset(f, 0, sizeof(f));
    for (int i = l; i <= r; i++) {
      f[a[i]]++;
    }
    std::vector<int> fr;
    for (int i = 0; i < NMAX; i++) {
      if (f[i] != 0) {
        fr.push_back(f[i]);
      }
    }

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

    ll answer = 0;

    std::vector<int> frr;
    for (int i = 0; i < (int) fr.size(); i += 2) {
      frr.push_back(fr[i]);
    }
    int sz = (int) frr.size();
    for (int i = 1; i < (int) fr.size(); i += 2) {
      frr.push_back(fr[i]);
    }
    std::reverse(frr.begin() + sz, frr.end());
//    for (const auto &x : frr) {
//      std::cout << x << ' ';
//    }
//    std::cout << '\n';
    fr = frr;
    int m = (int) fr.size();
    for (int k = 2; k <= m; k++) {
      for (int i = k - 1; i < m; i++) {
        answer += (ll) k * (fr[i - k + 1] * fr[i]);
      }
    }

    /**

    for i=1..N:
      for j=1..i-1:
        cur += j * f[i - j + 1]


    **/

//    do {
//      int m = (int) fr.size();
//      int cur = 0;
//      for (int k = 2; k <= m; k++) {
//        for (int i = k - 1; i < m; i++) {
//          cur += k * (fr[i - k + 1] * fr[i]);
//        }
//      }
//      answer = std::min(answer, cur);
//    } while(std::next_permutation(fr.begin(), fr.end()));
//
//    std::sort(fr.begin(), fr.end());
//    do {
//      int m = (int) fr.size();
//      int cur = 0;
//      for (int k = 2; k <= m; k++) {
//        for (int i = k - 1; i < m; i++) {
//          cur += k * (fr[i - k + 1] * fr[i]);
//        }
//      }
//      if (cur == answer) {
//        for (const auto &x : fr) {
//          std::cout << x << ' ';
//        }
//        std::cout << '\n';
//      }
//    } while(std::next_permutation(fr.begin(), fr.end()));

    for (const auto &x : fr) {
      answer += (ll) x * (x + 1) / 2;
    }

    std::cout << answer << '\n';
  }

  return 0;
}

Compilation message

diversity.cpp:7:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
    7 | #warning That's the baby, that's not my baby
      |  ^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 9 ms 3036 KB Output is correct
5 Correct 15 ms 3544 KB Output is correct
6 Incorrect 26 ms 5332 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 9 ms 3036 KB Output is correct
5 Correct 15 ms 3544 KB Output is correct
6 Incorrect 26 ms 5332 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 9 ms 3036 KB Output is correct
5 Correct 15 ms 3544 KB Output is correct
6 Incorrect 26 ms 5332 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 9 ms 3036 KB Output is correct
15 Correct 15 ms 3544 KB Output is correct
16 Incorrect 26 ms 5332 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 9 ms 3036 KB Output is correct
15 Correct 15 ms 3544 KB Output is correct
16 Incorrect 26 ms 5332 KB Output isn't correct
17 Halted 0 ms 0 KB -