QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276591 | #7904. Rainbow Subarray | koifish | WA | 1ms | 5628kb | C++14 | 2.6kb | 2023-12-05 23:28:31 | 2023-12-05 23:28:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
multiset<int> s1, s2;
const int N = 5e5 + 10;
int a[N], p[N];
int n, k;
void add(int x, int cnt)
{
s1.insert(x);
cnt=s1.size()+s2.size();
while (int(s1.size()) > (cnt + 1) / 2)
{
int k = *s1.rbegin();
auto f = s1.find(k);
s1.erase(f);
s2.insert(k);
}
}
void del(int x, int cnt)
{
if (s1.count(x))
{
auto k = s1.find(x);
s1.erase(k);
}
else
{
auto k = s2.find(x);
s2.erase(k);
}
while (int(s1.size()) > (cnt + 1) / 2)
{
int k = *s1.rbegin();
auto f = s1.find(k);
s1.erase(f);
s2.insert(k);
}
while (int(s1.size()) < (cnt + 1) / 2)
{
int k = *s2.begin();
auto f = s2.find(k);
s2.erase(f);
s1.insert(k);
}
while (int(s2.size()) < cnt / 2)
{
int k = *s1.rbegin();
auto f = s1.find(k);
s1.erase(f);
s2.insert(k);
}
}
bool check(int l, int r)
{
int val = *s1.rbegin();
int len = r - l + 1;
int sum = p[r] - p[l - 1];
// cout << l << " " << r << " " << val << " " << sum << endl;
if (abs(val * len - sum) <= k)
return true;
return false;
}
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
p[i] = p[i - 1] + a[i] - i;
a[i] -= i;
}
int l = 1, r = 1;
int ans = 1;
while (r <= n)
{
while (r <= n)
{
add(a[r], r - l + 1);
//cout << "add " << l << " " << r << " " << s1.size() << " " << s2.size() << endl;
if (r <= n && check(l, r))
{
ans = max(ans, r - l + 1);
//cout<<l<<" "<<r<<endl;
r++;
}
else
break;
}
if (r > n)
r--;
while (l <= r && (!check(l, r)))
{
del(a[l], r - l);
//cout << "del " << r - l << " " << l << " " << r << " " << s1.size() << " " << s2.size() << endl;
l++;
}
if (l > r)
r++;
if (r == n)
{
ans = max(ans, r - l + 1);
break;
}
}
cout << ans << endl;
}
int main()
{
cin.tie(0)->sync_with_stdio(false);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5628kb
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:
6 3 4 1 1
result:
wrong answer 1st lines differ - expected: '4', found: '6'