QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#321286 | #7904. Rainbow Subarray | haze | WA | 0ms | 3572kb | C++23 | 2.7kb | 2024-02-04 13:00:14 | 2024-02-04 13:00:15 |
Judging History
answer
/*
Rainbow Subarray
https://codeforces.com/gym/104901/problem/K
Author: haze
*/
#include <bits/stdc++.h>
#define irep(i, l, r) for(int i = (l); i <= (r); ++ i)
#define drep(i, r, l) for(int i = (r); i >= (l); -- i)
#define IOS ios::sync_with_stdio(false), cin.tie(nullptr);
using namespace std;
typedef long long ll;
inline ll read() {
ll s = 0;
bool fl = false;
char ch = (char) getchar();
while (!isdigit(ch)) {
if (ch == '-')fl = true;
ch = (char) getchar();
}
while (isdigit(ch)) {
s = s * 10 + (ch ^ 48);
ch = (char) getchar();
}
return fl ? -s : s;
}
const int mod = 1000000000 + 7;
const int itinf = 1000000999;
const ll llinf = 2e18;
const int N = 500099;
struct DS{
multiset<int>sl, sr;
ll suml, sumr;
DS(){suml = sumr = 0;}
void adjust(){
ll n = sl.size() + sr.size();// l fl n / 2, r ce n / 2;
while(sl.size() < n / 2){
suml += *sr.begin();
sumr -= *sr.begin();
sl.emplace(*sr.begin());
sr.erase(sr.begin());
}
while(sl.size() > n / 2){
sumr += *(-- sl.end());
suml -= *(-- sl.end());
sr.emplace(*(-- sl.end()));
sl.erase(-- sl.end());
}
}
void insert(int x){
if(sr.empty() || x >= *sr.begin()){
sr.emplace(x);
sumr += x;
}
else{
sl.emplace(x);
suml += x;
}
adjust();
}
void del(int x){
if(sl.contains(x)){
suml -= x;
sl.erase(sl.find(x));
}
if(sr.contains(x)){
sumr -= x;
sr.erase(sr.find(x));
}
adjust();
}
};
void solve() {
ll n = read(), k = read();
vector<ll>a(n);
irep(i, 0, n - 1)
a[i] = read() - i;
ll ans = 0;
ll l = 0, r = 0;
DS u;
while(r <= n){
// cout << "(l r) : "<< l << ' ' << r << endl;
// for(int x : u.sl)cout << x << ' ';
// cout << '|';
// for(int x : u.sr)cout << ' ' << x;
// cout << endl << u.suml << " & " << u.sumr << endl;
ll sr = u.sumr - u.suml;
if((r - l) % 2 == 1)sr -= *u.sr.begin();
// cout << "S = " << sr << endl;
if(sr <= k){
ans = max(ans, r - l);
u.insert(a[r ++]);
}
else{
u.del(a[l ++]);
}
}
ans = max(ans, r - l);
cout << ans << '\n';
}
int main() {
// IOS
int T = read();
while (T--) {
solve();
}
return 0;
}
//7 1 3 2 0 6 1
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3572kb
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 4 6 2 2
result:
wrong answer 2nd lines differ - expected: '3', found: '4'