QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#635958 | #9460. Diversity and Variance | ucup-team3586# | AC ✓ | 130ms | 17596kb | C++23 | 4.6kb | 2024-10-12 21:36:03 | 2024-10-12 21:36:04 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
int a[100100],b[100100];
ll val[100100];
int pos[100100],cnt;
int fa1[100100],fa2[100100];
inline int anc1(int x)
{
while(fa1[x]!=x) x=fa1[x]=fa1[fa1[x]];
return x;
}
inline int anc2(int x)
{
while(fa2[x]!=x) x=fa2[x]=fa2[fa2[x]];
return x;
}
int cur[100100];
int alive[100100];
// int limit1[100100],limit2[100100];
// int ind1[100100],ind2[100100];
pair<pii,int> Lim[100100];
int st[100100],nd[100100];
pii vlim[200200];
int St[100100],Nd[100100],cc[100100],spp[100100];
int pp[100100];
int vv[100100];
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int rd() {
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
return x*f;
}
void print(long long x) {
if(x>9) print(x/10);
*O++=x%10+'0';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=rd();
while(t--)
{
int n=rd(),m=rd();
alive[0]=0;
for(int i=1;i<=n;i++)
{
a[i]=rd();
b[i]=a[i];
// limit1[a[i]]=limit2[a[i]]=0;
// ind1[a[i]]=ind2[a[i]]=0;
alive[i]=0;
// vlim[a[i]].clear();
}
a[0]=0;
a[n+1]=10001;
for(int i=1;i<=n;i++)
cur[a[i]]=0;
sort(b+1,b+n+1);
if(m==n-1)
{
cout<<1<<'\n';
continue;
}
if(!m)
{
for(int i=1;i<=n;i++)
{
cout<<i;
if(i==n)
cout<<'\n';
else
cout<<" ";
}
continue;
}
ll ans=1ll*inf*inf;
ll sum1=0,sum2=0;
m=n-m;
for(int i=1;i<=m;i++)
{
sum1+=b[i];
sum2+=b[i]*b[i];
}
ans=sum2*m-sum1*sum1;
val[m]=ans;
for(int i=m-1;i>=0;i--)
{
sum1-=b[i+1];
sum2-=b[i+1]*b[i+1];
sum1+=b[n-(m-1-i)];
sum2+=b[n-(m-1-i)]*b[n-(m-i-1)];
ans=min(ans,sum2*m-sum1*sum1);
val[i]=sum2*m-sum1*sum1;
}
for(int i=1;i<=n;i++)
nd[b[i]]=i;
for(int i=n;i>=1;i--)
st[b[i]]=i;
ll mx=*max_element(val,val+m+1);
cnt=0;
for(int i=1;i<=n;i++)
St[a[i]]=Nd[a[i]]=cc[a[i]]=spp[a[i]]=0;
for(int i=0;i<=m;i++)
if(val[i]==mx)
{
cc[b[i+1]]++;
if(b[n-(m-i)]!=b[i+1])
cc[b[n-(m-i)]]++;
}
for(int i=1;i<=n;i++)
{
if(i==1||b[i]!=b[i-1])
{
St[b[i]]=Nd[b[i-1]];
Nd[b[i]]=St[b[i]]+cc[b[i]];
spp[b[i]]=St[b[i]];
}
}
for(int i=0;i<=m;i++)
if(val[i]==mx)
{
pos[++cnt]=i;
alive[cnt]=1;
Lim[cnt]=mp(mp(b[i+1],b[n-(m-i)]),i);
if(b[i+1]!=b[n-(m-i)])
{
vlim[spp[b[i+1]]++]=mp(i+1-st[b[i+1]],cnt);
vlim[spp[b[n-(m-i)]]++]=mp(nd[b[n-(m-i)]]-(n-(m-i)),cnt);
}
else
vlim[spp[b[i+1]]++]=mp(i+1-st[b[i+1]]+(nd[b[n-(m-i)]]-(n-(m-i))),cnt);
}
for(int i=1;i<=n;i++) if(b[i]!=b[i-1]||i==1)
{
sort(vlim+St[b[i]],vlim+Nd[b[i]]);
pp[b[i]]=St[b[i]];
}
for(int i=0;i<=cnt+1;i++)
fa1[i]=fa2[i]=i;
int tot=0;
int vvv=1;
for(int i=1;i<=n;i++)
{
while(vvv<=cnt&&Lim[vvv].first.second<=b[i])
vvv++;
vv[b[i]]=vvv;
}
for(int i=1;i<=n;i++)
{
int val=a[i];
int q1=anc1(1);
int q2=anc2(cnt);
int ok=0;
if(Lim[q1].first.second<val||Lim[q2].first.first>val)
ok=1;
if(!ok)
{
int mxl=0;
while(Nd[val]>St[val]&&!alive[vlim[Nd[val]-1].second]) Nd[val]--;
if(Nd[val]!=St[val]) mxl=vlim[Nd[val]-1].first;
if(cur[val]+1<=mxl)
ok=1;
}
if(ok)
{
cur[val]++;
while(pp[val]<Nd[val]&&cur[val]>vlim[pp[val]].first)
{
int ind=vlim[pp[val]].second;
if(alive[ind])
{
alive[ind]=0;
fa1[ind]=ind+1;
fa2[ind]=ind-1;
}
pp[val]++;
}
int L1=vv[val];
int cur=anc1(L1);
while(cur<=cnt&&Lim[cur].first.first<val)
{
alive[cur]=0;
fa1[cur]=cur+1;
fa2[cur]=cur-1;
cur=anc1(cur);
}
cout<<i;
tot++;
if(tot==m)
cout<<'\n';
else
cout<<' ';
}
}
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15936kb
input:
9 3 2 34 35 36 4 2 20 35 41 74 5 2 40 30 50 20 10 5 2 50 40 30 20 10 5 2 10 20 30 40 50 5 3 30 40 20 10 50 5 4 10 20 30 40 50 5 1 30 50 40 20 10 5 0 30 50 40 20 10
output:
1 1 4 1 3 5 1 2 5 1 2 5 4 5 1 2 3 4 5 1 2 3 4 5
result:
ok 9 lines
Test #2:
score: 0
Accepted
time: 130ms
memory: 17596kb
input:
30713 38 30 2 3 8 2 0 0 4 4 5 4 8 1 2 1 3 2 0 9 5 6 6 4 8 8 1 4 6 2 3 2 0 9 2 5 8 2 3 2 8 1 5222 2247 8017 1046 1922 9545 1396 2722 8 4 99 99 99 9999 999 9 1 9999 99 98 1 3 3 3 8 8 9 7 3 8 0 6 5 6 1 5 3 7 8 6 4 5 4 5 7 9 6 7 0 5 0 0 9 0 9 0 1 1 3 1 4 5 9 2 8 8 1 2 8 0 1 2 3 6 3 9 1 6 2 7 8 0 5 3 8 7...
output:
3 5 6 11 17 18 31 32 2 3 4 5 6 7 8 4 6 7 8 1 1 2 4 5 7 8 1 5 1 2 3 1 2 3 4 6 7 8 1 11 14 19 22 28 31 35 37 38 39 45 46 47 51 54 59 64 68 74 77 82 85 86 91 95 1 1 3 1 3 4 5 6 7 8 10 13 14 17 19 23 24 25 26 27 33 39 40 1 1 2 3 4 5 6 7 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 23 24 25 26 27 28 ...
result:
ok 30713 lines
Extra Test:
score: 0
Extra Test Passed