QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#276588 | #7904. Rainbow Subarray | koifish | WA | 202ms | 18212kb | C++14 | 2.5kb | 2023-12-05 23:23:22 | 2023-12-05 23:23:23 |
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);
if (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);
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
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
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
Wrong Answer
time: 202ms
memory: 18212kb
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 4 2 2 6 6 6 2 3 1 3 1 1 3 1 2 7 5 5 10 1 9 6 2 4 3 1 5 6 5 2 4 3 8 3 8 6 6 3 1 8 4 1 2 4 8 4 6 4 1 6 6 1 6 5 5 3 4 1 8 5 3 1 8 6 5 3 2 2 7 2 3 10 1 6 4 2 4 4 1 10 5 6 2 7 6 3 5 2 4 3 8 5 4 4 2 1 7 1 4 5 5 8 1 4 1 1 2 10 3 1 6 8 1 8 4 5 5 6 6 4 8 3 2 8 2 5 6 1 6 2 4 1 3 3 6 4 1 4 1 2 1 6 5 9 4 9 3 ...
result:
wrong answer 3rd lines differ - expected: '3', found: '2'