QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#719195 | #7904. Rainbow Subarray | Stelor | WA | 0ms | 3816kb | C++20 | 2.6kb | 2024-11-06 23:16:13 | 2024-11-06 23:16:13 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll inf=1e18;
void solve(){
ll n,k;
cin >> n >> k;
vector<ll> a(n+1);
for(int i=1;i<=n;++i){
std::cin >> a[i];
a[i]-=i;
}
int L=1,R=1;
multiset<ll> s2;
multiset<ll,greater<>> s1;
s1.insert(-inf);
s2.insert(inf);
ll sum1=0,sum2=0;
s2.insert(a[1]);
sum2+=a[1];
int ans=1;
while(R<=n){
++R;
if(a[R]>*s1.begin()){
s2.insert(a[R]);
sum2+=a[R];
}else{
s1.insert(a[R]);
sum1+=a[R];
}
while(fabs(s1.size()-s2.size())>1){
if(s1.size()>s2.size()){
ll top=*s1.begin();
sum2+=top;
s2.insert(*s1.begin());
s1.erase(s1.begin());
sum1-=top;
}
if(s1.size()<s2.size()){
ll top=*s2.begin();
sum1+=top;
s1.insert(*s2.begin());
s2.erase(s2.begin());
sum2-=top;
}
}
while(true){
bool f=true;
if(s1.size()>=s2.size()){
ll mid=*s1.begin();
ll now=(int)s1.size()*mid-sum1;
now+=sum2-(int)s2.size()*mid;
if(now>k) f=false;
}else if(s1.size()<s2.size()){
ll mid=*s2.begin();
ll now=(int)s1.size()*mid-sum1;
now+=sum2-(int)s2.size()*mid;
if(now>k) f=false;
}
if(f) break;
if(a[L]>*s1.begin()){
s2.erase(s2.find(a[L]));
sum2-=a[L];
}else{
s1.erase(s1.find(a[L]));
sum1-=a[L];
}
++L;
while(fabs(s1.size()-s2.size())>1){
if(s1.size()>s2.size()){
ll top=*s1.begin();
sum2+=top;
s2.insert(*s1.begin());
s1.erase(s1.begin());
sum1-=top;
}
if(s1.size()<s2.size()){
ll top=*s2.begin();
sum1+=top;
s1.insert(*s2.begin());
s2.erase(s2.begin());
sum2-=top;
}
}
}
ans=max(ans,R-L+1);
}
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3816kb
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 2
result:
wrong answer 5th lines differ - expected: '1', found: '2'