QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#584338 | #7904. Rainbow Subarray | argtarg | TL | 524ms | 11596kb | C++20 | 6.1kb | 2024-09-23 12:47:27 | 2024-09-23 12:47:28 |
Judging History
answer
#pragma GCC optimize(2, 3)
#include<bits/stdc++.h>
using namespace std;
//#define int long long
using ll = long long;
#define endl '\n'
void solve();
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
cin >> tt;
while (tt--)solve();
return 0;
}
vector< multiset<int>::iterator > sit(5e5 + 10);
vector<int> a(5e5 + 10);
void solve()
{
int n;
ll k;
cin >> n >> k;
// vector<int> a(n + 1);
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++)a[i] -= i;
int l = 0, r = n + 1;
auto check = [&](int mid)->bool {
multiset<int> s;
int len = mid;
int x = (len + 1) / 2;
int L = 0, R = 0;
for (int i = 1; i <= len; i++)
{
s.insert(a[i]);
}
auto it = s.begin();
for (int i = 1; i < x; i++)it++;
int res = 1e18;
int now = *it;
for (int i = 1; i <= len; i++)
{
if (a[i] <= now)
{
L += a[i];
}
else R += a[i];
}
int ans = 1e18;
auto c = [&](int L, int R)
{
return abs(L - now * x) + abs(R - now * (len - x));
};
res = c(L, R);
ans = res;
for (int i = len + 1; i <= n; i++)
{
s.insert(a[i]);
if (a[i] > now)
{
// res += a[i] - now;
L += *it;
R -= *it;
it++;
R += a[i];
int j = i - len;
int vj = a[j];
now = *it;
if (vj < now)
{
//L -=
L -= vj;
s.erase(s.find(vj));
//res -= now - vj;
}
else if (vj > now)
{
R -= vj;
it--;
L -= *it;
R += *it;
s.erase(s.find(vj));
// res -= vj - now;
}
else if (vj == now)
{
it = s.erase(it);
L -= vj;
}
res = c(L, R);
ans = min(res, ans);
if (ans <= k)return 1;
}
else
{
// res += now - a[i];
R += *it;
L -= *it;
it--;
L += a[i];
int j = i - len;
int vj = a[j];
now = *it;
if (vj < now)
{
L += *it;
R -= *it;
it++;
s.erase(s.find(vj));
//res -= now - vj;
}
else if (vj > now)
{
s.erase(s.find(vj));
// res -= vj - now;
}
else if (vj == now)
{
it = s.erase(it);
L -= vj;
}
ans = min(res, ans);
if (ans <= k)return 1;
}
now = *it;
}
return ans <= k;
};
auto cc = [&](ll U, ll S, ll now, ll len) -> long long
{
if (len & 1)
{
return abs(U - (S - U - now));
}
else
{
return abs(U + now - (S - U - now));
}
};
//vector< multiset<int>::iterator > sit(n + 1);
// for (int i = 1; i <= n; i++)cout << a[i] << ' '; cout << endl;
#define nums a
auto ochec = [&](int len) -> int
{
// cout << "len == " << len << endl;
//multiset<int> s(nums.begin() + 1, nums.begin() + 1 + len);
multiset<int> s;
for (int i = 1; i <= len; i++)
{
sit[i] = s.insert(nums[i]);
}
auto it = s.begin();
for (int i = 0; i < len / 2; i++)it++;
int now = *it;
/*auto c = [&](int L, int R) -> long long
{
return abs(L - now * (len / 2 + 1)) + abs(R - now * (len - (len / 2 + 1)));
};*/
ll S = 0;
ll U = 0;
ll ans = 1e18;
//int L = 0, R = 0;
auto tit = s.begin();
for (int i = 1; i <= len; i++, tit++)
{
//if (i <= len / 2 + 1)
//{
// // L += *tit;
//}
// sit[i] = tit;
if (i > len / 2 + 1)
{
//R += *tit;
U += *tit;
}
S += *tit;
}
now = *it;
// ans = c(L, R);
ans = cc(U, S, now, len);
if (ans <= k)return 1;
for (int p = len + 1; p <= n; p++)
{
// cout << "*it == " << (*it) << '\n';
sit[p] = s.insert(nums[p]);
S += nums[p];
if (nums[p] < (*it))
{
// L += nums[p];
//R += *it;
//L -= *it;
U += *it;
it--;
}
else U += nums[p];
if (nums[p - len] <= (*it))
{
// U -= *it;
//L -= nums[p - len];
//R -= *it;
//L += *it;
it++;
U -= *it;
}
else U -= nums[p - len];
//s.erase(s.lower_bound(nums[p - len]));
s.erase(sit[p - len]);
S -= nums[p - len];
now = *it;
// ans = min(ans, c(L, R));
ans = min(ans, cc(U, S, now, len));
if (ans <= k)return 1;
}
return ans <= k;
};
while (l < r - 1)
{
int mid = l + r >> 1;
if (ochec(mid))l = mid;
else r = mid;
}
// cout << "l == ";
cout << l << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8988kb
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: 0
Accepted
time: 524ms
memory: 11596kb
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 3 2 6 5 7 2 4 1 4 1 1 3 2 2 7 8 7 7 1 7 6 2 4 3 1 6 7 7 3 4 3 9 3 8 6 6 3 1 6 3 1 2 4 6 4 6 4 1 4 7 1 6 3 5 6 6 1 7 5 3 1 6 4 5 3 2 2 6 2 3 10 1 4 3 2 4 5 1 7 5 5 5 8 5 3 6 3 5 5 8 5 4 5 2 1 5 2 3 3 4 8 1 3 1 2 2 8 3 1 6 8 1 8 4 5 6 6 8 4 8 3 2 8 4 5 6 2 6 2 4 1 5 4 5 3 2 4 1 2 1 4 5 8 3 7 3 3 3...
result:
ok 11102 lines
Test #3:
score: -100
Time Limit Exceeded
input:
1 500000 17244641009859 54748096 75475634 204928248 276927808 84875072 103158867 27937890 322595515 186026685 45468307 69240390 139887597 188586447 373764525 121365644 310156469 185188306 60350786 211308832 384695957 370562147 208427221 35937909 267590963 126478310 275357775 55361535 335993561 36696...