QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#535934 | #4835. Mode | tarjen | TL | 2ms | 3640kb | C++20 | 2.8kb | 2024-08-28 16:50:23 | 2024-08-28 16:50:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
struct Gmx{
vector<int> val,cnt;
int n,mx;
Gmx(int _n=0):n(_n){
val.resize(n+1);
cnt.resize(n+1);
cnt[0]=n;
mx=0;
}
void add(int x){
cnt[val[x]]--;
val[x]++;
cnt[val[x]]++;
mx=max(mx,val[x]);
}
void del(int x){
cnt[val[x]]--;
val[x]--;
cnt[val[x]]++;
if(cnt[mx]==0)mx--;
}
int getmax(){
return mx;
}
};
void gmax(int &x,int y){
if(y>x)x=y;
}
const int N=600;
void solve()
{
int n;cin>>n;
// int n=1e5;
map<int,int> ma;
vector<int> a(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
// a[i]=rand();
ma[a[i]]=1;
}
int cnt=0;
vector<int>id{0};
for(auto &it:ma)it.second=++cnt,id.push_back(it.first);
vector<vector<int>> ve(cnt+1);
for(int i=1;i<=n;i++)a[i]=ma[a[i]],ve[a[i]].push_back(i);
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++){
Gmx sol(n+1),sol2(n+1);
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){
vector<int>sum(n+1);
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+(a[i]==z);
}
vector<int>pr(cnt+1),now(cnt+1);
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;
}
pr=vector<int>(cnt+1,n);
now=vector<int>(cnt+1,0);
for(int i=1;i<=n;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());
cout<<max_ans<<"\n";
for(int i=1;i<=cnt;i++)if(ans[i]==max_ans)cout<<id[i]<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
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: 2ms
memory: 3640kb
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: 2ms
memory: 3508kb
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: 0ms
memory: 3640kb
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: 2ms
memory: 3560kb
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
Time Limit Exceeded
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...