QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#536127#4835. ModeAfterlifeTL 51ms17160kbC++174.1kb2024-08-28 18:50:022024-08-28 18:50:03

Judging History

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

  • [2024-08-28 18:50:03]
  • 评测
  • 测评结果:TL
  • 用时:51ms
  • 内存:17160kb
  • [2024-08-28 18:50:02]
  • 提交

answer

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

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+1;
struct Gmx{
    int val[maxn],cnt[maxn];
    int n,all,mx;
    inline void init(int _n=0,int _all=0){
        n=_n;all=_all;
        memset(cnt,0,(n+1)*4);
        memset(val,0,(all+1)*4);
        cnt[0]=n;
        mx=0;
    }
    inline void add(int x){
        cnt[val[x]]--;
        val[x]++;
        cnt[val[x]]++;
        mx=max(mx,val[x]);
    }
    inline void del(int x){
        cnt[val[x]]--;
        val[x]--;
        cnt[val[x]]++;
        if(cnt[mx]==0)mx--;
    }
    inline int getmax(){
        return mx;
    }
}sol,sol2;
void gmax(int &x,int y){
    if(y>x)x=y;
}
int sum[maxn],now[maxn],pr[maxn];
vector<int>ve[maxn];
int a[maxn];
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
typedef pair<int,int> pii;
typedef long long ll;
int dynamic(int n,int cnt) { /// n*N + n*(>=N)
    ll ans = 1e18;
    map<int,int> mp;
    for(int i= 1;i <= cnt;i++) {
        if(ve[i].size()) mp[(int)ve[i].size()]++ ;
    }
    vector<pair<int,int> > v;
    for(auto [x,y] : mp) v.push_back({x,y}) ;
    int cho = -1;
    int nxt = v.size() - 1 , cc = 0;
    for(int i = min(n , 5 * (int)sqrt(n));i >= 1;i--) {
        if(nxt >=0 && v[nxt].first == i) cc++ ;
        /// choose i
        if(1LL * i * n + 1LL * cc * n < ans) {
            ans = 1LL * i * n + 1LL * cc * n;
            cho = i;
        }
    }
    return cho ;
}
void solve()
{
    int n=read();
    vector<int> all;
    auto getid = [&](int x){
        return lower_bound(all.begin(),all.end(),x)-all.begin()+1;
    };
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=n;i++)all.push_back(a[i]);
    sort(all.begin(),all.end());
    all.resize(unique(all.begin(),all.end())-all.begin());
    int cnt=(int)all.size();
    for(int i=1;i<=cnt;i++)ve[i].clear();
    for(int i=1;i<=n;i++)a[i]=getid(a[i]),ve[a[i]].push_back(i);
    
    int N=dynamic(n,cnt) ;

    vector<int> ans(cnt+1);
    for(int i=1;i<=cnt;i++)ans[i]=(int)ve[a[i]].size();
    for(int t=1;t<=N;t++){
        sol.init(n,cnt),sol2.init(n,cnt);
        for(int i=1;i<=n;i++)sol2.add(a[i]);
        int j=0;
        while(sol.mx<t&&j<n){++j;sol.add(a[j]);sol2.del(a[j]);}
        if(sol.mx!=t)break;
        for(int i=1;i<=n;i++){
            if(i>0)gmax(ans[a[i-1]],sol2.val[a[i-1]]+t);
            sol.del(a[i]);
            while(sol.mx<t&&j<n){
                j++;
                gmax(ans[a[j]],sol2.val[a[j]]+t);
                sol.add(a[j]);
                sol2.del(a[j]);
            }
            sol2.add(a[i]);
            if(sol.mx!=t)break;
        }
    }
    for(int z=1;z<=cnt;z++)if((int)ve[z].size()>=N){
        for(int i=1;i<=n;i++){
            sum[i]=sum[i-1]+(a[i]==z);
        }
        memset(pr,0,(cnt+1)*4);
        memset(now,0,(cnt+1)*4);
        for(int i=1;i<=n;i++)if(a[i]!=z){
            now[a[i]]+=sum[i]-sum[pr[a[i]]];
            // cout<<"z="<<z<<" i="<<i<<" now="<<now[a[i]]<<" ans="<<(int)ve[a[i]].size()+now[a[i]]<<endl;
            gmax(ans[a[i]],(int)ve[a[i]].size()+now[a[i]]);
            now[a[i]]--;
            if(now[a[i]]<0)now[a[i]]=0;
            pr[a[i]]=i;
        }
        memset(now,0,(cnt+1)*4);
        for(int i=1;i<=cnt;i++)pr[i]=n;
        for(int i=n;i>=1;i--)if(a[i]!=z){
            now[a[i]]+=sum[pr[a[i]]]-sum[i];
            // cout<<"z="<<z<<" i="<<i<<" now="<<now[a[i]]<<" ans="<<(int)ve[a[i]].size()+now[a[i]]<<endl;
            gmax(ans[a[i]],(int)ve[a[i]].size()+now[a[i]]);
            now[a[i]]--;
            if(now[a[i]]<0)now[a[i]]=0;
            pr[a[i]]=i;
        }
    }
    int max_ans=*max_element(ans.begin(),ans.end());
    printf("%d\n",max_ans);
    for(int i=1;i<=cnt;i++)if(ans[i]==max_ans)printf("%d\n",all[i-1]);
}
int main()
{
    int T=read();
    while(T--)solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 10796kb

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: 0ms
memory: 12952kb

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: 3ms
memory: 10828kb

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: 4ms
memory: 13176kb

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: 0ms
memory: 13324kb

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: 0
Accepted
time: 50ms
memory: 16920kb

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:

72021
564718673
40464
764450551
14267
485362070
14627
904735088
4168
647351267
2097
876938652

result:

ok 12 numbers

Test #7:

score: 0
Accepted
time: 51ms
memory: 17160kb

input:

6
200000
757703054 544067926 5887448 544067926 757703054 757703054 544067926 757703054 544067926 544067926 643910770 544067926 5887448 544067926 757703054 544067926 5887448 544067926 5887448 643910770 5887448 757703054 544067926 544067926 544067926 544067926 544067926 544067926 757703054 757703054 5...

output:

72009
544067926
40553
532894160
14507
280401558
14661
652613244
4149
954112179
2118
387281831

result:

ok 12 numbers

Test #8:

score: 0
Accepted
time: 49ms
memory: 17124kb

input:

6
200000
941492387 378192988 783332532 378192988 378192988 72235422 378192988 378192988 378192988 783332532 783332532 783332532 378192988 378192988 783332532 378192988 783332532 72235422 378192988 449924898 378192988 783332532 941492387 449924898 72235422 72235422 72235422 378192988 378192988 378192...

output:

72125
378192988
40566
204258647
14420
679764421
14458
621315231
4176
283127255
2125
379004468

result:

ok 12 numbers

Test #9:

score: 0
Accepted
time: 46ms
memory: 17048kb

input:

6
200000
652509537 513994713 513994713 513994713 940751563 652509537 652509537 43705451 513994713 652509537 43705451 513994713 513994713 652509537 652509537 43705451 513994713 940751563 513994713 652509537 43705451 940751563 43705451 43705451 652509537 513994713 513994713 652509537 513994713 5139947...

output:

72040
652509537
40565
33034714
14364
848061415
14689
972643361
4192
106061785
2110
861238489

result:

ok 12 numbers

Test #10:

score: -100
Time Limit Exceeded

input:

9
50000
264178469 144383292 725622388 29492121 605488152 580675905 55883389 390555088 441004197 831609975 438791943 987615719 859267729 743196568 55883389 39388076 959436397 876153780 45621610 363531956 977960026 782710197 106874917 807199457 562810007 715666999 743196568 538628510 824046493 1893675...

output:


result: