4 条题解

  • 5
    @ 2025-3-28 8:42:36

    你说的对,我使用基数排序,脑子都不用动一下。

    详见 Luogu P4604 [WC2017] 挑战

    排序操作的时间复杂度是线性的。

    #include<bits/stdc++.h>
    using namespace std;
    using ui=unsigned int;
    istream& fin=cin;
    ostream& fout=cout;
    template <typename ForwardIterator,
              typename = is_unsigned<typename ForwardIterator::value_type>>
    void radixSort(ForwardIterator first, ForwardIterator last) {
      constexpr ui P = 8;
      constexpr size_t T = 4;
      size_t n = distance(first, last);
      ui W = 0;
      auto *dat = new typename ForwardIterator::value_type[n];
      for (size_t i = 0; i < T; ++i, W += P) {
        array<size_t, 1u << P> cnt{};
        for_each(first, last, [&](typename ForwardIterator::reference const x) {
          ++cnt[(x >> W) & ((1u << P) - 1)];
        });
        partial_sum(cnt.begin(), cnt.end(), cnt.begin());
        rotate(cnt.begin(), prev(cnt.end()), cnt.end()), cnt[0] = 0;
        for_each(first, last, [&](typename ForwardIterator::reference const x) {
          dat[cnt[(x >> W) & ((1u << P) - 1)]++] = x;
        });
        copy(dat, dat + n, first);
      }
      delete[] dat;
    }
    int main(void){
        ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
        size_t n;fin>>n;
        vector<ui> a(n);
        for (ui& i:a) fin>>i;
        radixSort(a.begin(),a.end());
        a.erase(unique(a.begin(),a.end()),a.end());
        ui ans=0,c=1;
        for (auto it=next(a.begin());it!=a.end();++it)
            if (*it-*prev(it)==1) ++c;
            else{
                ans=max(ans,c);
                c=1;
            }
        ans=max(ans,c);
        fout<<ans;
        return 0;
    }
    

    信息

    ID
    102
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    (无)
    递交数
    156
    已通过
    16
    上传者