Submission #920252

# Submission time Handle Problem Language Result Execution time Memory
920252 2024-02-02T10:50:12 Z vjudge1 Diversity (CEOI21_diversity) C++17
Compilation error
0 ms 0 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[100];

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 < 100; i++) {
      if (f[i] != 0) {
        fr.push_back(f[i]);
      }
    }

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

    int 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 += k * (fr[i - k + 1] * fr[i]);
      }
    }

//    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 += 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
      |  ^~~~~~~
diversity.cpp: In function 'int main()':
diversity.cpp:30:63: error: expected ')' before ';' token
   30 |   norm.erase(std::unique(norm.begin(), norm.end(), norm.end());
      |             ~                                                 ^
      |                                                               )
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/bits/char_traits.h:39,
                 from /usr/include/c++/10/ios:40,
                 from /usr/include/c++/10/ostream:38,
                 from /usr/include/c++/10/iostream:39,
                 from diversity.cpp:1:
/usr/include/c++/10/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Iterator2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]':
/usr/include/c++/10/bits/stl_algo.h:951:20:   required from '_ForwardIterator std::__unique(_ForwardIterator, _ForwardIterator, _BinaryPredicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _BinaryPredicate = __gnu_cxx::__ops::_Iter_comp_iter<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >]'
/usr/include/c++/10/bits/stl_algo.h:1015:27:   required from '_FIter std::unique(_FIter, _FIter, _BinaryPredicate) [with _FIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _BinaryPredicate = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]'
diversity.cpp:30:62:   required from here
/usr/include/c++/10/bits/predefined_ops.h:156:30: error: no match for call to '(__gnu_cxx::__normal_iterator<int*, std::vector<int> >) (int&, int&)'
  156 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~