QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#335178 | #7904. Rainbow Subarray | hano | RE | 0ms | 3616kb | C++14 | 2.4kb | 2024-02-22 20:56:29 | 2024-02-22 20:56:30 |
Judging History
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