QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#629950#7904. Rainbow SubarrayYurilyRE 0ms3828kbC++202.1kb2024-10-11 15:42:062024-10-11 15:42:07

Judging History

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

  • [2024-10-11 15:42:07]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3828kb
  • [2024-10-11 15:42:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int MAX=5e5+5;
int n,a[MAX];
long long k,sum1,sum2,cur;
multiset<int> s1,s2;
int mid;
// 快读 
long long read(){
	long long x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9'){
		if(c == '-'){
			f = -1;
		}
		c = getchar();
	}
	while(c >= '0' && c <= '9'){
		x = x*10+c-'0';
		c = getchar();
	}
	return x*f;
}

void del(int x){
	
	if(x<mid){
	//	cout<<"*"<<endl;
		auto p1=s1.find(-x);
		sum1-=x;
		sum1+=mid;
		s1.erase(p1);
		s1.insert(mid);
		auto p2=s2.begin();
		mid=*p2;
		sum2-=mid;
		s2.erase(p2);
	}
	else{
		auto p2=s2.find(x);
		if(p2!=s2.end()){	
			sum2-=x;
			s2.erase(p2);
			if(s1.size()>s2.size()){
				sum2+=mid;
				s2.insert(mid);
				auto p1=s1.begin();
				mid=-(*p1);
				sum1-=mid;
				s1.erase(p1);
			}
		}
		else{
			if(s1.size()>=s2.size()){
				auto p1=s1.begin();
				mid=-(*p1);
				sum1-=mid;
				s1.erase(p1);
			}
			else{
				auto p2=s2.begin();
				mid=*p2;
				sum2-=mid;
				s2.erase(p2);
			}
		}
	}
	cur=(long long)mid*s1.size()-sum1+sum2-(long long)mid*s2.size();
}
void insert(int x){
	if(x<=mid){
		s1.insert(-x);
		sum1+=x;
		if(s1.size()>s2.size()){
			auto p=s1.begin();
			s2.insert(mid);
			sum2+=mid;
			mid=-(*p);
			s1.erase(p);
			sum1-=mid; 
		}
	}
	else{
		s2.insert(x);
		sum2+=x;
		if(s1.size()+1<s2.size()){
			auto p=s2.begin();
			s1.insert(-mid);
			sum1+=mid;
			mid=*p;
			s2.erase(p);
			sum2-=mid;
		}
	}
	cur=(long long)mid*s1.size()-sum1+sum2-(long long)mid*s2.size();
}
void solve(){
	n=read(),k=read();
	s1.clear(),s2.clear();
	sum1=sum2=cur=0;
	for(int i=1;i<=n;++i){
		a[i]=read();
		a[i]-=i;
	}
	mid=a[1];
	int l=1,r=1,ans=1;
	while(r<n){
	//	cout<<l<<" "<<r<<" "<<mid<<endl;
		while(r<n&&cur<=k){
			insert(a[++r]);
			if(cur<=k)
				ans=max(ans,r-l+1);
	//		cout<<l<<" "<<r<<" "<<mid<<" "<<s1.size()<<" "<<s2.size()<<" "<<(*s2.begin())<<endl;
		}
		while(l<r&&cur>k){
			del(a[l++]);
	//		cout<<l<<" "<<r<<" "<<mid<<endl;
		}
	}
	printf("%d\n",ans);	
	
}
int main(){
	int T;
	cin>>T;
	while(T--){
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3828kb

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
Runtime Error

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: