QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#554413#7904. Rainbow Subarrayliqingyang#TL 2ms13268kbC++141.3kb2024-09-09 11:08:322024-09-09 11:08:32

Judging History

你现在查看的是最新测评结果

  • [2024-09-09 11:08:32]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:13268kb
  • [2024-09-09 11:08:32]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int T,n,a[500010],A[500010],f[1<<19];
long long m,g[1<<19];
inline void add(int x,int v)
{
	for(int V=A[x]*v;x<(1<<19);
	f[x]+=v,g[x]+=V,x+=x&-x);
}
inline bool query(int x,long long sum)
{
	int p=0,now=0;
	long long ret=0;
	for(int i=1<<18;i;i>>=1)
	{
		if(now+f[p|i]<(x+1>>1))
		{
			p|=i;
			now+=f[p];
			ret+=g[p];
		}
	}
	return (long long)A[p+1]*
	(now-(x-now))-ret+(sum-ret)<=m;
}
inline bool judge(int x)
{
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	long long sum=0;
	for(int i=1;i<=x;i++)
	{
		add(a[i],1);
		sum+=A[a[i]];
	}
	if(query(x,sum))
	{
		return 1;
	}
	for(int i=x+1,now=x+1>>1;i<=n;i++)
	{
		add(a[i-x],-1),add(a[i],1);
		sum+=A[a[i]]-A[a[i-x]];
		if(query(x,sum))
		{
			return 1;
		}
	}
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			a[i]+=n-i;
		}
		memcpy(A,a,sizeof(A));
		sort(A+1,A+n+1);
		int num=unique(A+1,A+n+1)-A-1;
		for(int i=1;i<=n;i++)
		{
			a[i]=lower_bound
			(A+1,A+num+1,a[i])-A;
		}
		int l=1,r=n,ans;
		while(l<=r)
		{
			int mid=l+r>>1;
			judge(mid)?(l=(ans=mid)+1):(r=mid-1);
		}
		cout<<ans<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13268kb

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...

output:


result: