QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#661343 | #7904. Rainbow Subarray | foolnine | TL | 1ms | 3564kb | C++20 | 4.3kb | 2024-10-20 15:51:20 | 2024-10-20 15:51:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct Fenwick {
int n;
vector<i64> f;
vector<int> f1;
Fenwick() {}
Fenwick(int n) {
init(n);
}
void init(int n) {
this->n = n;
f.assign(n + 1, 0);
f1.assign(n + 1, 0);
}
void add(int x, i64 y, int flag) {
if (flag == 1) {
while (x <= n) {
f[x] += y;
f1[x]++;
x += x & -x;
}
} else {
while (x <= n) {
f[x] -= y;
f1[x]--;
x += x & -x;
}
}
}
pair<i64, int> sum(int x) {
i64 ret = 0;
int r1 = 0;
while (x > 0) {
ret += f[x];
r1 += f1[x];
x -= x & -x;
}
return make_pair(ret, r1);
}
};
void solve() {
int n;
i64 m;
cin >> n >> m;
vector<i64> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i] += (n - i - 1);
}
// for (int i = 0; i < n; i++) {
// cerr << a[i] << " \n"[i == n - 1];
// }
// cerr << "---" << endl;
vector<i64> pre(n + 1, 0);
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] + a[i];
}
auto check = [&](int x) -> bool {
// cerr << x << endl;
if (x == 1) {
return true;
} else if (x == 2) {
for (int i = 0; i < n - 1; i++) {
if (llabs(a[i] - a[i + 1]) <= m) {
return true;
}
}
return false;
}
vector<i64> v = a;
sort(v.begin(), v.end());
// for (int i = 0; i < v.size(); i++) {
// cerr << v[i] << " \n"[i == v.size() - 1];
// }
int N = v.size();
Fenwick f(N);
multiset<i64> st1, st2;
for (int i = 0; i < x - 1; i++) {
st1.insert(a[i]);
f.add(lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1, a[i], 1);
}
while (st1.size() > st2.size()) {
st2.insert(*st1.begin());
st1.erase(st1.begin());
}
bool ret = 0;
for (int i = x - 1; i < n; i++) {
st1.insert(a[i]);
f.add(lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1, a[i], 1);
if (st1.size() > st2.size()) {
st2.insert(*st1.begin());
st1.erase(st1.begin());
}
i64 cal = pre[i + 1] - pre[i + 1 - x];
i64 c1 = *st2.rbegin(), c2 = *st1.begin();
auto [s1, n1] = f.sum(lower_bound(v.begin(), v.end(), c1) - v.begin() + 1);
auto [s2, n2] = f.sum(lower_bound(v.begin(), v.end(), c2) - v.begin() + 1);
i64 need1 = (1LL * n1 * c1 - s1 + (cal - s1) - (1LL * (x - n1) * c1));
i64 need2 = (1LL * n2 * c2 - s2 + (cal - s2) - (1LL * (x - n2) * c2));
// cerr << "i: " << i + 1 << ", need1: " << need1 << ", need2: " << need2 << endl;
// cerr << "c1: " << c1 << ", c2: " << c2 << endl;
// cerr << "sum1: " << s1 << ", n1: " << n1 << endl;
// cerr << "sum2: " << s2 << ", n2: " << n2 << endl;
if (need1 <= m || need2 <= m) {
ret = 1;
break;
}
f.add(lower_bound(v.begin(), v.end(), a[i - x + 1]) - v.begin() + 1, a[i - x + 1], 0);
if (st2.contains(a[i - x + 1])) {
st2.erase(st2.find(a[i - x + 1]));
} else {
st1.erase(st1.find(a[i - x + 1]));
st1.insert(*st2.rbegin());
st2.erase(prev(st2.end()));
}
}
return ret;
};
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
if (check(mid)) {
lo = mid;
} else {
hi = mid - 1;
}
// cerr << "---" << endl;
}
cout << lo << endl;
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
// cerr << "\n\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3564kb
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...
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...