QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#611989 | #7904. Rainbow Subarray | mojo__hugo | WA | 166ms | 6404kb | C++20 | 2.9kb | 2024-10-05 01:03:50 | 2024-10-05 01:03:52 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define ull unsigned long long
using PII = pair<int, int>;
using ld = long double;
const int N = 5e5 + 5, M = 1e9 + 7;
typedef __int128 lll;
istream &operator>>(istream &cin, lll &x) { x=0; static string s; cin>>s; for (char c:s) x=x*10+(c-'0'); return cin; }
ostream &operator<<(ostream &cout, lll x) { static char s[60]; int tp=1; s[0]='0'+(x%10); while (x/=10) s[tp++]='0'+(x%10); while (tp--) cout<<s[tp]; return cout; }
mt19937_64 rng{ chrono::steady_clock::now().time_since_epoch().count() };
ll n, k, a[N];
template <typename T>
struct Fen{
int n;
vector<T> a;
Fen(int n_ = 0) {
init(n_);
}
void init(int n_) {
n = n_;
a.assign(n + 1, T{});
}
void add(int x, const T &v) {
for (int i = x; i <= n; i += i & -i) {
a[i] += v;
}
}
T qry(int x) {
T ans{};
for (int i = x; i > 0; i -= i & -i) {
ans += a[i];
}
return ans;
}
T qry(int l, int r) {
return qry(r) - qry(l - 1);
}
int kth(const T &k) {
int x = 0;
T cur{};
for (int i = 1 << __lg(n); i > 0; i /= 2) {
if (x + i <= n && cur + a[x + i] < k) {
x += i;
cur += a[x];
}
}
return x + 1;
}
};
#define find(x) lower_bound(v.begin(), v.begin() + m - 1, x) - v.begin()+1
void solve() {
cin >> n >> k;
for (int i = 1;i <= n;i++) {
cin >> a[i];
a[i] += n - i;
}
vector<ll>v;
for (int i = 1;i <= n;i++)v.push_back(a[i]);
sort(v.begin(), v.end());
int m = unique(v.begin(), v.end()) - v.begin();
Fen<ll>t(m + 2);
Fen<int>t2(m + 2);
int l = 1;
ll res = 0;
int ans = 1;
for (int i = 1;i <= n;i++) {
int xx = find(a[i]);
t.add(xx, a[i]);
t2.add(xx, 1);
res += a[i];
int L = i - l + 1;
int id = t2.kth((L + 1) / 2) - 1;
ll x = v[id];
int idxx = find(x);
int c1 = t2.qry(idxx);
ll s1 = c1 * x - 2 * t.qry(idxx) + res - (L - c1) * x;
while (s1 > k) {
xx = find(a[l]);
t.add(xx, a[l]);
t2.add(xx, 1);
res -= a[l];
l++;
L = i - l + 1;
id = t2.kth((L + 1) / 2) - 1;
x = v[id];
idxx = find(x);
c1 = t2.qry(idxx);
c1 = t2.qry(idxx);
s1 = c1 * x - 2 * t.qry(idxx) + res - (L - c1) * x;
}
ans = max(ans, i - l + 1);
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int tt = 1;
// cout << fixed << setprecision(12);
cin >> tt;
while (tt--)solve();
}
详细
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: 166ms
memory: 6404kb
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 5 7 2 4 1 1 1 1 3 1 2 7 5 7 7 1 7 5 2 4 3 1 3 7 7 3 4 3 9 3 8 6 6 1 1 6 1 1 2 4 5 4 6 4 1 3 5 1 6 3 5 6 6 1 7 5 3 1 6 3 5 3 2 2 5 1 3 10 1 4 2 2 4 5 1 7 5 5 5 8 5 3 6 3 5 4 8 5 3 5 2 1 5 1 2 3 3 8 1 3 1 2 2 8 3 1 6 8 1 8 4 5 6 6 7 4 8 3 2 8 4 5 6 2 6 2 4 1 5 4 5 3 2 4 1 2 1 2 5 6 3 7 3 3 3...
result:
wrong answer 3rd lines differ - expected: '3', found: '2'