QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#661233 | #7904. Rainbow Subarray | foolnine# | WA | 1309ms | 9576kb | C++20 | 3.5kb | 2024-10-20 15:23:26 | 2024-10-20 15:23:27 |
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;
vector<i64> v = a;
for (int i = x; i <= n; i++) {
v.push_back((pre[i] - pre[i - x]) / x);
v.push_back((pre[i] - pre[i - x]) / x + 1);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), 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);
for (int i = 0; i < x - 1; i++) {
f.add(lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1, a[i], 1);
}
bool ret = 0;
for (int i = x - 1; i < n; i++) {
f.add(lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1, a[i], 1);
i64 cal = pre[i + 1] - pre[i + 1 - x];
i64 c1 = cal / x, c2 = c1 + 1;
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);
}
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();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
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: 1309ms
memory: 9576kb
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 6 8 7 7 1 7 6 2 4 3 1 4 7 7 3 4 3 9 3 8 6 6 2 1 6 3 1 2 4 6 4 5 4 1 4 7 1 6 3 5 6 6 1 7 5 3 1 6 3 4 3 2 2 6 2 3 10 1 4 3 2 4 5 1 7 5 5 4 6 5 3 6 2 5 5 8 5 4 5 2 1 5 2 3 3 4 7 1 3 1 2 2 6 3 1 6 8 1 8 4 5 5 6 7 4 8 3 2 8 4 5 5 2 6 2 4 1 5 4 5 3 2 4 1 2 1 4 4 8 3 7 3 3 3...
result:
wrong answer 17th lines differ - expected: '7', found: '6'