QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#335178#7904. Rainbow SubarrayhanoRE 0ms3616kbC++142.4kb2024-02-22 20:56:292024-02-22 20:56:30

Judging History

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

  • [2024-06-09 00:00:26]
  • hack成功,自动添加数据
  • (/hack/652)
  • [2024-02-22 20:56:30]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3616kb
  • [2024-02-22 20:56:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define fi first
#define se second
#define lc node*2
#define rc (node*2+1)
const int N=5e5+5;
const int mod=1e9+7;
int arr[N];
int l=1,r=1;
multiset<ll>lft,rgt;
ll suml=0,sumr=0,median;
int ans=0;
void del(int ind){
	if(arr[ind]==median){
		if(lft.size()==rgt.size()){
			median=(*lft.rend());
			lft.erase(lft.find(median));
			suml-=median;
		}else{
			if(lft.size()>rgt.size()){
				median=(*lft.rbegin());
				lft.erase(lft.find(median));
				suml-=median;
			}else{
				median=(*rgt.begin());
				rgt.erase(rgt.find(median));
				sumr-=median;
			}
		}
	}else if(arr[ind]<median){
		lft.erase(lft.find(arr[ind]));
		suml-=arr[ind];
		if(rgt.size()==lft.size()){
			return;
		}
		suml+=median;
		lft.insert(median);
		median=(*rgt.begin());
		rgt.erase(rgt.find(median));
		sumr-=median;
	}else{
		rgt.erase(rgt.find(arr[ind]));
		sumr-=arr[ind];
		if(rgt.size()==lft.size()){
			return;
		}
		sumr+=median;
		rgt.insert(median);
		median=(*lft.rbegin());
		lft.erase(lft.find(median));
		suml-=median;
	}
			//cout<<"del "<<median<<' '<<lft.size()<<' '<<rgt.size()<<' '<<suml<<' '<<sumr<<endl;

}

void add(int ind){
	int now=arr[ind];
	if(ind==1){
			median=now;
		}else if(now<=median){
			lft.insert(now);
			if(lft.size()==rgt.size()){
				suml+=now;
				return;
			}
			rgt.insert(median);
			sumr+=median;
			suml+=now;
			median=(*lft.rbegin());
			lft.erase(lft.find(median));
			suml-=median;
		}else{
			rgt.insert(now);
			if(lft.size()==rgt.size()){
				sumr+=now;
				return;
			}
			lft.insert(median);
			
			suml+=median;
			sumr+=now;
			median=(*rgt.begin());
			rgt.erase(rgt.find(median));
			sumr-=median;
		}
		//cout<<"add "<<median<<' '<<lft.size()<<' '<<rgt.size()<<' '<<suml<<' '<<sumr<<endl;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;cin>>t;
	while(t--){
		ll n,k;cin>>n>>k;
		for(int i=1;i<=n;i++){
			cin>>arr[i];
			arr[i]-=i;
		}
		add(1);
		while(l<=r && r<=n){
			if(median*lft.size()-suml + (sumr-median*rgt.size())<=k){
				ans=max(ans,r-l+1);
				r++;
				if(r>n)break;
				add(r);
			}else{
				del(l);
				l++;
			}
		}
		cout<<ans<<endl;
		ans=0;
		l=1,r=1;
		suml=sumr=median=0;
		lft.clear();rgt.clear();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:

1

result: