QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#403384#7904. Rainbow Subarrayucup-team3282#WA 125ms7900kbC++141.7kb2024-05-02 10:13:382024-05-02 10:13:39

Judging History

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

  • [2024-06-09 00:00:26]
  • hack成功,自动添加数据
  • (/hack/652)
  • [2024-05-02 10:13:39]
  • 评测
  • 测评结果:WA
  • 用时:125ms
  • 内存:7900kb
  • [2024-05-02 10:13:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;

int T;
int n;ll k;
int a[maxn];
int sum[maxn];

struct heap{
	multiset<int,greater<int> > q1;
	multiset<int> q2;
	ll sum1,sum2;
	
	void print(){
		for(auto it=q1.begin();it!=q1.end();it++)
			cout<<*it<<' ';
		for(auto it=q2.begin();it!=q2.end();it++)
			cout<<*it<<' ';
		cout<<endl;
	}
	void clear(){
		q1.clear();q2.clear();
		sum1=sum2=0;
	}
	void maintain(){
		int s1=q1.size(),s2=q2.size();
		if(s1>(s1+s2+1)/2){
			int t=*q1.begin();
			sum2+=t;
			sum1-=t;
			q2.insert(t);
			q1.erase(q1.begin());
		}
		else if(s1<(s1+s2+1)/2){
			int t=*q2.begin();
			sum1+=t;
			sum2-=t;
			q1.insert(t);
			q2.erase(q2.begin());
		}
	}
	void insert(int x){
//		cout<<"ins "<<x<<endl;
		if(q1.empty()||x<=*q1.begin()){
			sum1+=x;
			q1.insert(x);
		}
		else{
			sum2+=x;
			q2.insert(x);
		}
		maintain();
//		print();
	}
	void erase(int x){
//		cout<<"era "<<x<<endl;
		if(x<=*q1.begin()){
			sum1-=x;
			q1.erase(x);
		}
		else{
			sum2-=x;
			q2.erase(x);
		}
		maintain();
//		print();
	}
	ll query(){
//		cout<<"q "<<sum1<<' '<<sum2<<' '<<(*q1.begin())<<' '<<q1.size()<<' '<<q2.size()<<endl;
		return (ll)q1.size()*(*q1.begin())-sum1+sum2-(ll)q2.size()*(*q1.begin());
	}
}q;

int main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		cin>>n>>k;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		for(int i=1;i<=n;i++)
			a[i]-=i;
		
		int l=1,r;
		int ans=0;
		q.clear();
		for(r=1;r<=n;r++){
			q.insert(a[r]);
			while(q.query()>k)
				q.erase(a[l++]);
//			cout<<l<<' '<<r<<' '<<q.query()<<endl;
//			q.print();
			ans=max(ans,r-l+1);
		}
		cout<<ans<<endl;
	}
	
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5708kb

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
Wrong Answer
time: 125ms
memory: 7900kb

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
4
3
2
6
5
7
2
4
1
4
1
1
3
2
2
7
8
7
7
1
7
6
2
4
3
1
6
7
7
3
4
3
9
3
8
6
6
3
1
6
3
1
2
4
6
4
6
4
1
4
7
1
6
3
5
6
6
1
7
5
3
1
6
4
5
3
2
2
6
2
3
10
1
4
3
2
4
5
1
7
5
5
5
8
5
3
6
3
5
5
8
5
4
5
2
1
5
2
3
3
4
8
1
3
1
2
2
8
3
1
6
8
1
8
4
5
6
6
8
4
8
3
2
8
4
5
6
2
6
2
4
1
5
4
5
3
2
4
1
2
1
4
5
8
3
7
3
3
3...

result:

wrong answer 11002nd lines differ - expected: '517', found: '515'