QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#669129 | #7904. Rainbow Subarray | YAXLI2ARD | RE | 1ms | 4028kb | C++17 | 2.9kb | 2024-10-23 17:22:14 | 2024-10-23 17:22:15 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T,n,a[500009];
ll k;
struct DDZ{
multiset<int> a,b;
ll at=0,bt=0;
// void debug(){
// cout<<a.size()<<" "<<at<<" "<<b.size()<<" "<<bt<<endl;
// }
inline int get_mid(){
return *b.begin();
}
inline void insert(int x){
if(b.empty()){
b.insert(x);
bt+=x;
}
else{
int mid=get_mid();
if(x>=mid){
if(b.size()==a.size()){
b.insert(x);
bt+=x;
}
else{
b.erase(b.begin());
a.insert(mid);
b.insert(x);
bt-=mid;
bt+=x;
at+=mid;
}
}
else{
if(b.size()==a.size()){
auto it=a.end();
it--;
b.insert(*it);
a.erase(it);
a.insert(x);
at-=*it;
bt+=*it;
at+=x;
}
else{
a.insert(x);
at+=x;
}
}
}
}
inline void del(int x){
int mid=get_mid();
if(x>=mid){
if(b.size()==a.size()){
b.erase(b.find(x));
auto it=a.end();
it--;
b.insert(*it);
a.erase(it);
bt-=x;
at-=*it;
bt+=*it;
}
else{
b.erase(b.find(x));
bt-=x;
}
}
else{
if(b.size()==a.size()){
a.erase(a.find(x));
at-=x;
}
else{
a.erase(a.find(x));
a.insert(mid);
b.erase(b.begin());
at-=x;
at+=mid;
bt-=mid;
}
}
}
inline bool check(){
int mid=get_mid();
ll res=(1ll*a.size()*mid-at)+(1ll*bt-1ll*b.size()*mid);
return res<=k;
}
};
void solve(){
scanf("%d%lld",&n,&k);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
a[i]-=i;
}
int l=1,r=1,ans=0;
DDZ h;
h.insert(a[1]);
int idx=0;
while(1){
if(h.check()){
ans=max(ans,r-l+1);
// cout<<l<<" "<<r<<" ";
// h.debug();
if(r==n) break;
r++;
h.insert(a[r]);
}
else{
h.del(a[l]);
l++;
}
// if(l>r||r>n) break;
idx++;
if(idx>2*n) break;
}
printf("%d\n",ans);
}
int main(){
scanf("%d",&T);
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4028kb
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...