QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#620752 | #7904. Rainbow Subarray | yld | WA | 1ms | 3640kb | C++20 | 2.3kb | 2024-10-07 20:58:47 | 2024-10-07 20:58:48 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int MAXN=5e5+5;
int a[MAXN],n,k;
bool check(int x)
{
if(x==1) return 1;
multiset<int> small,big;
int suma=0,sumb=0;
auto insert=[&](int temp)
{
if(small.size()<=big.size())
{
small.insert(temp);
suma+=temp;
if(big.size() && *small.rbegin()>*big.begin())
{
int x=*small.rbegin(),y=*big.begin();
small.erase(small.find(x));
suma-=x;
big.erase(big.find(y));
sumb-=y;
small.insert(y);big.insert(x);
suma+=y;sumb+=x;
}
}
else
{
big.insert(temp);
sumb+=temp;
if(small.size() && *small.rbegin()>*big.begin())
{
int x=*small.rbegin(),y=*big.begin();
small.erase(small.find(x));
suma-=x;
big.erase(big.find(y));
sumb-=y;
small.insert(y);big.insert(x);
suma+=y;sumb+=x;
}
}
};
auto del=[&](int temp)
{
if(small.size() && temp<=*small.rbegin()) small.erase(small.find(temp)),suma-=temp;
else big.erase(big.find(temp)),sumb-=temp;
};
for(int i=1;i<=n;i++)
{
if(i<=x) insert(a[i]);
else
{
del(a[i-x]);
insert(a[i]);
}
if(i>=x)
{
int mid;
if(x%2) mid=*small.rbegin();
else mid=(*small.rbegin()+*big.begin())/2;
// if(x==5) cerr<<mid<<' '<<suma<<' '<<sumb<<endl;
int cha=sumb-(big.size()*mid)+(small.size()*mid)-suma;
if(cha<=k) return 1;
}
}
return 0;
}
void solve()
{
cin>>n>>k;
vector<int> a(n+1);
for(int i=1;i<=n;i++) cin>>a[i],a[i]-=i;
// for(int i=1;i<=n;i++) cerr<<a[i]<<' ';
// cerr<<endl;
int l=1,r=n+1;
while(l<r)
{
int mid=l+r>>1;
if(check(mid)) l=mid+1;
else r=mid;
}
cout<<l-1<<endl;
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3640kb
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:
7 6 5 5 1
result:
wrong answer 1st lines differ - expected: '4', found: '7'