QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#657216 | #7904. Rainbow Subarray | frankly6 | TL | 0ms | 3600kb | C++17 | 3.3kb | 2024-10-19 14:23:09 | 2024-10-19 14:23:10 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<set>
#include<queue>
#include<vector>
#define int long long
using namespace std;
const int MX=500050;
int T, N, K;
int ar[MX];
multiset<int> st, ed;
int read()
{
int r=0, f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') {r=r*10+ch-'0'; ch=getchar();}
return r*f;
}
void balance(int &mid, int &s1, int &s2, int siz)
{
// cout << "IN, siz=" << siz << '\n';
int det=(siz%2==0);
while(st.size()+det!=ed.size())
{
// cout << st.size()+det << " " << ed.size() << '\n';
if(st.size()+det>ed.size())
{
ed.insert(mid), s2+=mid;
auto it=st.find(*st.rbegin());
mid=*it; st.erase(it); s1-=mid;
// cout << st.size()+det << " " << ed.size() << '\n';
}
else
{
st.insert(mid), s1+=mid;
auto it=ed.find(*ed.begin());
mid=*it; ed.erase(it); s2-=mid;
}
}
// cout << "BALANCE\n"; cout << " st:"; for(auto u:st) cout << u << " "; cout << '\n'; cout << " ed:"; for(auto u:ed) cout << u << " "; cout << '\n'; cout << " mid:" << mid << '\n';
}
signed main()
{
// freopen("testdata.in","r",stdin);
T=read();
while(T--)
{
st.clear(); ed.clear();
N=read(); K=read();
for(int i=1;i<=N;i++) ar[i]=read();
for(int i=1;i<=N;i++) ar[i]+=N-i;
// for(int i=1;i<=N;i++) cout << ar[i] << " "; cout << '\n';
int ans=1, mid=ar[1];
int s1=0, s2=0;
for(int l=1,r=2;l<=N&&r<=N+1;l++) //current range [l,r)
{
int now=mid*st.size()-s1+s2-mid*ed.size();
// cout << "in, l=" << l << ", r=" << r << '\n';
// cout << "now=" << now << ", K=" << K << '\n';
while(now<=K)
{
if(ar[r]>mid) ed.insert(ar[r]), s2+=ar[r];
else st.insert(ar[r]), s1+=ar[r];
r++;
balance(mid,s1,s2,r-l);
now=mid*st.size()-s1+s2-mid*ed.size();
if(now<=K) ans=max(r-l,ans);
// cout << "now=" << now << ", l=" << l << ", ans=" << ans << '\n';
}
// cout << "st:"; for(auto u:st) cout << u << " "; cout << '\n';
// cout << "ed:"; for(auto u:ed) cout << u << " "; cout << '\n';
if(ar[l]<mid)
{
auto it=st.find(ar[l]);
s1-=*it; st.erase(it);
}
else if(ar[l]>mid)
{
auto it=ed.find(ar[l]);
s2-=*it; ed.erase(it);
}
else if(ar[l]==mid)
{
if((r-l)%2==1)
{
auto it=st.find(*st.rbegin());
mid=*it; s1-=mid; st.erase(it);
}
else
{
auto it=ed.find(*ed.begin());
mid=*it; s2-=mid; ed.erase(it);
}
}
balance(mid,s1,s2,r-l-1);
// cout << "out, l=" << l << ", r=" << r << '\n';
}
cout << ans << '\n';
}
return (0-0);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
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
Time Limit Exceeded
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...