QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#78488#4835. Modehos_lyricWA 14ms6576kbC++149.1kb2023-02-19 09:48:402023-02-19 09:48:43

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-19 09:48:43]
  • 评测
  • 测评结果:WA
  • 用时:14ms
  • 内存:6576kb
  • [2023-02-19 09:48:40]
  • 提交

answer

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


// fast IO by yosupo
// sc.read(string &) appends the input
struct Scanner {
    FILE* fp = nullptr;
    char line[(1 << 15) + 1];
    size_t st = 0, ed = 0;
    void reread() {
        memmove(line, line + st, ed - st);
        ed -= st;
        st = 0;
        ed += fread(line + ed, 1, (1 << 15) - ed, fp);
        line[ed] = '\0';
    }
    bool succ() {
        while (true) {
            if (st == ed) {
                reread();
                if (st == ed) return false;
            }
            while (st != ed && isspace(line[st])) st++;
            if (st != ed) break;
        }
        if (ed - st <= 50) reread();
        return true;
    }
    template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        while (true) {
            size_t sz = 0;
            while (st + sz < ed && !isspace(line[st + sz])) sz++;
            ref.append(line + st, sz);
            st += sz;
            if (!sz || st != ed) break;
            reread();            
        }
        return true;
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        bool neg = false;
        if (line[st] == '-') {
            neg = true;
            st++;
        }
        ref = T(0);
        while (isdigit(line[st])) {
            ref = 10 * ref + (line[st++] - '0');
        }
        if (neg) ref = -ref;
        return true;
    }
    template <class T> bool read_single(vector<T>& ref) {
        for (auto& d : ref) {
            if (!read_single(d)) return false;
        }
        return true;
    }
    void read() {}
    template <class H, class... T> void read(H& h, T&... t) {
        bool f = read_single(h);
        assert(f);
        read(t...);
    }
    Scanner(FILE* _fp) : fp(_fp) {}
};

struct Printer {
  public:
    template <bool F = false> void write() {}
    template <bool F = false, class H, class... T>
    void write(const H& h, const T&... t) {
        if (F) write_single(' ');
        write_single(h);
        write<true>(t...);
    }
    template <class... T> void writeln(const T&... t) {
        write(t...);
        write_single('\n');
    }

    Printer(FILE* _fp) : fp(_fp) {}
    ~Printer() { flush(); }

  private:
    static constexpr size_t SIZE = 1 << 15;
    FILE* fp;
    char line[SIZE], small[50];
    size_t pos = 0;
    void flush() {
        fwrite(line, 1, pos, fp);
        pos = 0;
    }
    void write_single(const char& val) {
        if (pos == SIZE) flush();
        line[pos++] = val;
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void write_single(T val) {
        if (pos > (1 << 15) - 50) flush();
        if (val == 0) {
            write_single('0');
            return;
        }
        if (val < 0) {
            write_single('-');
            val = -val;  // todo min
        }
        size_t len = 0;
        while (val) {
            small[len++] = char('0' + (val % 10));
            val /= 10;
        }
        for (size_t i = 0; i < len; i++) {
            line[pos + i] = small[len - 1 - i];
        }
        pos += len;
    }
    void write_single(const string& s) {
        for (char c : s) write_single(c);
    }
    void write_single(const char* s) {
        size_t len = strlen(s);
        for (size_t i = 0; i < len; i++) write_single(s[i]);
    }
    template <class T> void write_single(const vector<T>& val) {
        auto n = val.size();
        for (size_t i = 0; i < n; i++) {
            if (i) write_single(' ');
            write_single(val[i]);
        }
    }
    void write_single(long double d){
		{
			long long v=d;
			write_single(v);
			d-=v;
		}
		write_single('.');
		for(int _=0;_<8;_++){
			d*=10;
			long long v=d;
			write_single(v);
			d-=v;
		}
    }
};

Scanner sc(stdin);
Printer pr(stdout);


int N;
vector<int> A;

int main() {
  // for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
  int numCases; sc.read(numCases); { for (int caseId = 1; caseId <= numCases; ++caseId) {
    // scanf("%d", &N);
    sc.read(N);
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      // scanf("%d", &A[i]);
      sc.read(A[i]);
    }
    
    auto as = A;
    sort(as.begin(), as.end());
    as.erase(unique(as.begin(), as.end()), as.end());
    const int asLen = as.size();
    for (int i = 0; i < N; ++i) {
      A[i] = lower_bound(as.begin(), as.end(), A[i]) - as.begin();
    }
// cerr<<"A = "<<A<<endl;
    
    vector<int> freq(asLen, 0);
    vector<vector<int>> iss(asLen);
    for (int i = 0; i < N; ++i) {
      ++freq[A[i]];
      iss[A[i]].push_back(i);
    }
    auto ans = freq;
    
if(N==200'000){
 vector<int>freqFreq(N+1,0);
 for(int j=0;j<asLen;++j)++freqFreq[freq[j]];
 for(int f=N;f>=1;--f)if(freqFreq[f])cout<<f<<" "<<freqFreq[f]<<endl;
 exit(0);
}
    
    const int sqrtN = (int)sqrt(0.25 * (double)N);
    // const int sqrtN = N;
    // const int sqrtN = 0;
    
    // (max freq inside) >= k
    vector<int> freqIn(asLen, 0);
    vector<int> freqFreqIn(N + 1, 0);
    vector<int> rs(N + 1);
    for (int k = 1; k <= sqrtN; ++k) {
      fill(freqIn.begin(), freqIn.end(), 0);
      fill(freqFreqIn.begin(), freqFreqIn.begin() + (k + 1), 0);
      auto ok = [&]() -> bool {
        return (freqFreqIn[k] > 0);
      };
      auto add = [&](int a) -> void {
        --freqFreqIn[min(freqIn[a], k)];
        ++freqIn[a];
        ++freqFreqIn[min(freqIn[a], k)];
      };
      auto rem = [&](int a) -> void {
        --freqFreqIn[min(freqIn[a], k)];
        --freqIn[a];
        ++freqFreqIn[min(freqIn[a], k)];
      };
      for (int l = 0, r = 0; ; ) {
        for (; r < N && !ok(); add(A[r++])) {}
        rs[l] = ok() ? r : (N + 1);
        if (l == N) break;
        rem(A[l++]);
      }
// cerr<<"k = "<<k<<": rs = "<<rs<<endl;
      for (int j = 0; j < asLen; ++j) {
        const auto &is = iss[j];
        const int isLen = is.size();
        int score = 0;
        for (int hL = 0, hR = 0; hL <= isLen; ++hL) {
          const int l = (hL == 0) ? 0 : (is[hL - 1] + 1);
          if (rs[l] <= N) {
            for (; hR < isLen && is[hR] < rs[l]; ++hR) {}
// cerr<<"  j = "<<j<<", is = "<<is<<"; ["<<hL<<", "<<hR<<")"<<endl;
            chmax(score, k + isLen - (hR - hL));
          }
        }
        chmax(ans[j], score);
      }
    }
    
    vector<int> fs(N + 1, 0);
    for (int jIn = 0; jIn < asLen; ++jIn) if (freq[jIn] > sqrtN) {
      for (int i = 0; i < N; ++i) {
        fs[i + 1] = fs[i] + ((A[i] == jIn) ? 1 : 0);
      }
      for (int j = 0; j < asLen; ++j) {
        const auto &is = iss[j];
        const int isLen = is.size();
        int score = 0;
        int mx = 0;
        for (int h = 0; h <= isLen; ++h) {
          {
            const int l = (h == 0) ? 0 : (is[h - 1] + 1);
            chmax(mx, -(fs[l] - h));
          }
          {
            const int r = (h == isLen) ? N : is[h];
            chmax(score, isLen + mx + (fs[r] - h));
          }
        }
        chmax(ans[j], score);
      }
    }
    
    const int maxAns = *max_element(ans.begin(), ans.end());
    // printf("%d\n", maxAns);
    pr.writeln(maxAns);
    for (int j = 0; j < asLen; ++j) if (maxAns == ans[j]) {
      // printf("%d\n", as[j]);
      pr.writeln(as[j]);
    }
#ifdef LOCAL
if(N<=100){
vector<int>brt(asLen,0);
for(int j=0;j<asLen;++j)for(int jj=0;jj<asLen;++jj)for(int l=0;l<=N;++l)for(int r=l;r<=N;++r){
 int cnt=0;
 for(int i=0;i<N;++i)if(A[i]==((l<=i&&i<r)?jj:j))++cnt;
 chmax(brt[j],cnt);
}
if(ans!=brt){
 cerr<<"A = "<<A<<endl;
 cerr<<"ans = "<<ans<<endl;
 cerr<<"brt = "<<brt<<endl;
 assert(false);
}
cerr<<"========"<<endl;
}
#endif
  }
#ifndef LOCAL
  // break;
#endif
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3564kb

input:

4
5
1 2 3 2 1
5
1 1 3 1 1
6
2 4 2 4 8 8
5
1 2 3 4 5

output:

4
1
5
1
4
2
4
8
2
1
2
3
4
5

result:

ok 14 numbers

Test #2:

score: 0
Accepted
time: 3ms
memory: 3568kb

input:

10
300
336470888 634074578 642802746 167959139 642802746 481199252 481199252 481199252 167959139 634074578 634074578 336470888 336470888 481199252 642802746 481199252 481199252 167959139 642802746 634074578 167959139 336470888 634074578 642802746 167959139 481199252 167959139 167959139 167959139 481...

output:

80
481199252
634074578
46
153774342
39
846318354
30
937534594
27
698063951
27
419330425
20
603780410
706588687
801036056
20
541308492
19
352404708
16
187061768
428773075

result:

ok 24 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

10
300
641009859 804928248 804928248 804928248 804928248 641009859 476927808 641009859 641009859 641009859 75475634 804928248 804928248 641009859 804928248 54748096 75475634 75475634 54748096 75475634 54748096 54748096 476927808 476927808 75475634 476927808 641009859 75475634 476927808 476927808 754...

output:

84
75475634
47
173884819
253838924
593535580
37
119584259
29
66715052
671541499
706982083
25
683509776
23
145885283
637691905
26
968506132
18
277852473
891198181
954696748
18
967045702
19
493331319

result:

ok 27 numbers

Test #4:

score: 0
Accepted
time: 3ms
memory: 3520kb

input:

10
300
923264237 524125987 524125987 524125987 923264237 751244358 374288891 923264237 923264237 923264237 535590429 524125987 374288891 751244358 524125987 923264237 751244358 751244358 923264237 751244358 535590429 535590429 751244358 923264237 751244358 524125987 751244358 923264237 524125987 923...

output:

85
524125987
50
475906689
38
802613215
28
824887911
28
506836893
754648411
23
708438632
731263599
20
210467639
284624362
746100908
806519500
980100704
21
797383894
21
156438550
977566828
17
111777754

result:

ok 27 numbers

Test #5:

score: 0
Accepted
time: 1ms
memory: 3468kb

input:

10
300
702209411 496813081 561219907 702209411 702209411 561219907 730593611 496813081 702209411 561219907 673102149 702209411 496813081 702209411 673102149 496813081 730593611 496813081 673102149 702209411 673102149 673102149 496813081 496813081 702209411 673102149 561219907 702209411 561219907 561...

output:

81
496813081
53
675266630
38
767363622
32
404396525
27
118275344
195136790
21
422498140
522949042
869477976
887728896
998214353
22
611458649
20
298642883
618165181
853396846
18
510545542
18
41119063
42804963
383659701

result:

ok 29 numbers

Test #6:

score: -100
Wrong Answer
time: 14ms
memory: 6576kb

input:

6
200000
564718673 564718673 291882089 291882089 412106895 291882089 291882089 412106895 564718673 564718673 412106895 412106895 412106895 564718673 291882089 564718673 412106895 291882089 564718673 291882089 564718673 291882089 291882089 564718673 291882089 412106895 564718673 291882089 564718673 5...

output:

71983 1
56377 1
39774 1
24055 1
7811 1

result:

wrong answer 1st numbers differ - expected: '72021', found: '71983'