QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#554785 | #8332. Two in One | hicgnliaw | TL | 7ms | 4408kb | C++14 | 2.0kb | 2024-09-09 15:46:07 | 2024-09-09 15:46:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
inline int read(){
int s = 0;char ch = getchar();
while(ch<'0'||ch>'9')ch = getchar();
while(ch>='0'&&ch<='9'){s = (s<<1)+(s<<3)+ch-'0';ch = getchar();}
return s;
}
int n,a[N],cnt[N],T,sum[N],r[N];
void solve1(){
int ans = 0,ansl,ansr;
int nk[105],ct;
for(int i = 1;i<=n;i++){
memset(cnt,0,sizeof(cnt));
memset(sum,0,sizeof(sum));
for(int j = i;j<=n;j++){
sum[cnt[a[j]]]--;
sum[++cnt[a[j]]]++;
ct = 0;
for(int k = 1;k<=n;k++){
if(sum[k])nk[++ct] = k;
}
for(int k = 1;k<=ct;k++){
if(ans<nk[k]){
ans = nk[k];
ansl = i,ansr = j;
}
for(int t = 1;t<k;t++){
if((nk[k]|nk[t])>ans){
ans = nk[k]|nk[t];
ansl = i,ansr = j;
}
}
}
}
}
// cout << ans << '\n';
memset(cnt,0,sizeof(cnt));
for(int i = ansl;i<=ansr;i++)cnt[a[i]]++;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
if((cnt[i]|cnt[j])==ans){
cout << ans << '\n';
// cout << ansl << ' ' << ansr << '\n'
// << i << ' ' << j << '\n';
return;
}
}
}
}
void solve2(){
int mx1 = 0,mx2 = 0;
memset(cnt,0,sizeof(cnt));
for(int i = 1;i<=n;i++){
r[a[i]] = i;
cnt[a[i]]++;
}
for(int i = 1;i<=n;i++){
if(cnt[i]>=cnt[mx1]){
mx2 = mx1;
mx1 = i;
}else if(cnt[i]>cnt[mx2])mx2 = i;
}
int c;
if(!cnt[mx2]){
cout << cnt[mx1] << '\n';
// cout << 1 << ' ' << n << '\n';
// cout << mx1 << ' ' << mx1 << '\n';
return;
}
for(c = 1<<20;c;c>>=1){
if((cnt[mx1]&c)&&(cnt[mx2]&c))break;
}
for(int i = n;i>=1;i--){
if(!(cnt[mx1]&c)||!(cnt[mx2]&c)){
cout << (cnt[mx1]|cnt[mx2]) << '\n';
// cout << 1 << ' ' << i << '\n';
// cout << mx1 << ' ' << mx2 << '\n';
return;
}
cnt[a[i]]--;
}
return;
}
int main(){
// freopen("ttheal.in","r",stdin);
// freopen("ttheal.out","w",stdout);
T = read();
while(T--){
n = read();
for(int i = 1;i<=n;i++)a[i] = read();
if(n<=100)solve1();
else solve2();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4408kb
input:
1 7 1 2 3 4 3 2 1
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 4352kb
input:
1 9 1 1 1 1 1 2 2 2 2
output:
7
result:
ok 1 number(s): "7"
Test #3:
score: 0
Accepted
time: 7ms
memory: 4040kb
input:
50 10000 72 65 90 94 67 93 41 43 54 83 31 34 38 37 44 42 92 63 66 79 90 45 41 40 19 54 34 90 13 74 40 77 41 57 74 86 91 79 34 39 21 88 90 57 23 31 8 15 80 27 45 38 53 96 51 82 70 71 19 75 62 95 31 67 99 97 94 29 90 7 95 82 61 98 62 77 43 65 66 30 41 69 38 79 51 9 63 77 13 30 72 70 67 93 92 45 74 50 ...
output:
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
result:
ok 50 numbers
Test #4:
score: -100
Time Limit Exceeded
input:
79114 7 1 2 4 3 2 3 7 7 1 2 4 7 4 1 5 6 4 2 2 4 2 1 7 1 2 1 4 7 3 2 7 1 2 4 7 1 1 3 7 1 2 6 1 5 2 5 6 5 2 6 1 2 5 7 1 1 7 6 6 5 2 7 1 2 5 6 6 7 7 7 1 1 2 7 2 3 6 7 1 1 1 4 6 2 2 7 1 1 5 7 3 5 2 7 1 1 7 6 2 2 5 6 4 3 4 6 3 1 6 4 4 3 2 3 1 7 1 1 2 3 7 6 2 6 1 2 5 6 3 4 7 1 1 1 1 4 7 6 6 2 6 6 2 5 3 6 ...
output:
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 5 3 3 3 3 3 3 3 3 3 3 3 5 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 3 3 3 5 3 3 3 5 3 3 3 3 3 3 3 3 3 3 5 3 3 3 3 3 3 3 3 3 3 3 5 3 3 3 1 3 3 3 3 3 3 5 3 3 3 3 3 3 3 3 3 3 ...