QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#383815 | #7904. Rainbow Subarray | gingoo# | TL | 1ms | 5812kb | C++23 | 3.7kb | 2024-04-09 17:48:44 | 2024-04-09 17:48:45 |
Judging History
answer
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,k,t;
ll a[500005],sum[500005];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
cin>>n>>k;
for(ll i=1;i<=n;i++)
cin>>a[i],a[i]=a[i]-i+1;
ll i=1,j=1,now=0,sum1=0,sum2=0,p=0,q=0,x=0,mx=1;
multiset<ll> s2;
multiset<ll,greater<ll>> s1;
while(j<=n)
{
if(n-i+1<=mx)
break;
if((j-i+1)%2&&j-i+1!=1)//加入这个数后是奇数
{
x=a[j];
p=*s1.begin();
q=*s2.begin();
s1.erase(s1.begin());
s2.erase(s2.begin());
sum1-=p;
sum2-=q;
if(p>x)
swap(p,x);
if(x>q)
swap(x,q);
s1.insert(p);
s2.insert(q);
sum1+=p;
sum2+=q;
now=s1.size()*x-sum1+sum2-s2.size()*x;
s1.insert(x);
sum1+=x;
}
else if((j-i+1)==1)//加入这个数后是一个数
{
s1.insert(a[j]);
sum1+=a[j];
}
else if((j-i+1)==2)
{
p=*s1.begin();
s1.erase(s1.begin());
sum1-=p;
q=a[j];
now=max(p,q)-min(p,q);
s1.insert(min(p,q));
sum1+=min(p,q);
s2.insert(max(p,q));
sum2+=max(p,q);
}
else
{
x=a[j];
p=*s1.begin();
q=*s2.begin();
s1.erase(s1.begin());
s2.erase(s2.begin());
sum1-=p;
sum2-=q;
bool flag=false;
if(p>x)
swap(p,x);
if(x>q)
swap(x,q);
if(s1.size()>s2.size())
s2.insert(q),sum2+=q,flag=true;
else
s1.insert(p),sum1+=p;
if(!flag)
p=x,x=q;
now=min(s1.size()*p-sum1+sum2-s2.size()*p+x-p,s1.size()*x-sum1+sum2-s2.size()*x+p-x);
s1.insert(p);
sum1+=p;
s2.insert(x);
sum2+=x;
}
while(now>k&&i<j)
{
x=a[i];
if(x<=*s1.begin())
{
auto it=s1.find(x);
s1.erase(it);
sum1-=x;
}
else
{
auto it=s2.find(x);
s2.erase(it);
sum2-=x;
}
if(s1.size()<s2.size())
{
s1.insert(*s2.begin());
sum1+=*s2.begin();
sum2-=*s2.begin();
s2.erase(s2.begin());
}
if((s1.size()+s2.size())%2)
{
now=s1.size()*(*s1.begin())-sum1+sum2-s2.size()*(*s1.begin());
}
else
{
now=min(s1.size()*(*s1.begin())-sum1+sum2-s2.size()*(*s1.begin()),
s1.size()*(*s2.begin())-sum1+sum2-s2.size()*(*s2.begin()));
}
i++;
}
mx=max(mx,j-i+1);
j++;
}
cout<<mx<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5812kb
input:
5 7 5 7 2 5 5 4 11 7 6 0 100 3 4 5 99 100 5 6 1 1 1 1 1 5 50 100 200 300 400 500 1 100 3
output:
4 3 5 1 1
result:
ok 5 lines
Test #2:
score: -100
Time Limit Exceeded
input:
11102 2 167959139 336470888 134074578 5 642802746 273386884 79721198 396628655 3722503 471207868 6 202647942 268792718 46761498 443917727 16843338 125908043 191952768 2 717268783 150414369 193319712 6 519096230 356168102 262263554 174936674 407246545 274667941 279198849 9 527268921 421436316 3613460...