QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#536074 | #4835. Mode | tarjen | TL | 3ms | 11992kb | C++20 | 3.2kb | 2024-08-28 18:14:16 | 2024-08-28 18:14:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+1;
struct Gmx{
int val[maxn],cnt[maxn];
int n,all,mx;
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;
}
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;
}
}sol,sol2;
void gmax(int &x,int y){
if(y>x)x=y;
}
int sum[maxn],now[maxn],pr[maxn];
int lst[maxn],jmp[maxn],siz[maxn];
int a[maxn];
int ans[maxn];
const int N=600;
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;
}
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();
memset(lst,0,(cnt+1)*4);
memset(jmp,0,(cnt+1)*4);
memset(siz,0,(cnt+1)*4);
for(int i=1;i<=n;i++){
a[i]=getid(a[i]);
jmp[i]=lst[a[i]];
lst[a[i]]=i;
siz[a[i]]++;
}
for(int i=1;i<=cnt;i++)ans[i]=siz[i];
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(siz[z]>=N){
memset(sum,0,(n+1)*4);
for(int x=lst[z];x!=0;x=jmp[x]){
sum[x]=1;
}
for(int i=1;i<=n;i++)sum[i]+=sum[i-1];
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]]];
gmax(ans[a[i]],siz[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];
gmax(ans[a[i]],siz[a[i]]+now[a[i]]);
if(--now[a[i]]<0)now[a[i]]=0;
pr[a[i]]=i;
}
}
int max_ans=0;
for(int i=1;i<=cnt;i++)gmax(max_ans,ans[i]);
printf("%d\n",max_ans);
for(int i=1;i<=cnt;i++)if(ans[i]==max_ans)printf("%d\n",all[i-1]);
for(int i=1;i<=cnt;i++)ans[i]=0;
}
int main()
{
int T=read();
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11932kb
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: 11904kb
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: 0ms
memory: 11988kb
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: 2ms
memory: 8080kb
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: 3ms
memory: 11992kb
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...