QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#696206 | #7904. Rainbow Subarray | Traveler | WA | 122ms | 6320kb | C++20 | 2.4kb | 2024-10-31 21:50:12 | 2024-10-31 21:50:12 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<unordered_map>
#include<vector>
#include<array>
#include<math.h>
#include<map>
#include<stdio.h>
#include<queue>
#include<assert.h>
#include<string>
#include<limits.h>
#include<stack>
#include<set>
#include<list>
#include<algorithm>
#include <chrono>
#include<random>
using namespace std;
typedef long long LL;
#define int long long
typedef unsigned long long ULL;
typedef pair<LL, LL>PII;
typedef pair<double, double>PDD;
typedef pair<char, char>PCC;
LL n, m, k;
const LL inf = 1e18;
const LL N = 1e6 + 10;
const LL mod = 1e9 + 7;
const LL mod2 = 998244353;
void solve() {
cin >> n >> k;
vector<int>a(n + 1);
for (int i = 1;i <= n;i++) {
cin >> a[i];
a[i] -= i;
}
multiset<int>p, q;
int s1 = 0, s2 = 0,ans = 1;
auto balance = [&]()->void {
while (q.size() > p.size() + 1) {
p.insert(*(q.begin()));
s2 -= *(q.begin());
s1 += *(q.begin());
q.erase(q.find(*(q.begin())));
}
while (p.size() > q.size()) {
q.insert(*(p.begin()));
s1 -= *(p.begin());
s2 += *(p.begin());
p.erase(p.find(*(p.begin())));
}
};
auto cost = [&]()->int {
int mid = *(q.begin());
return (mid * p.size() - s1) + (s2 - mid * q.size());
};
for (int i = 1,l= 1;i <= n;i++) {
if (q.size() == 0) {
q.insert(a[i]);
s2 += a[i];
}
else {
if (a[i] >= *(q.begin())) {
q.insert(a[i]);
s2 += a[i];
}
else {
p.insert(a[i]);
s1 += a[i];
}
}
balance();
while (l<i && cost()>k) {
if (p.size()&&p.find(a[l]) != p.end()) {
p.erase(p.find(a[l]));
s1 -= a[l];
}
else {
s2 -= a[l];
q.erase(q.find(a[l]));
}
balance();
l++;
}
ans = max(ans, i - l + 1);
}
cout << ans << "\n";
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3552kb
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: 122ms
memory: 6320kb
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 7 7 2 4 1 5 1 1 5 3 2 7 8 7 7 1 9 6 2 5 3 1 7 8 7 3 4 3 9 5 8 6 6 3 1 8 4 1 2 4 6 4 7 4 1 5 7 1 7 3 5 7 7 1 9 5 4 1 7 4 6 3 2 3 6 2 3 10 1 8 4 2 4 5 1 7 5 5 5 8 7 3 6 3 5 7 8 5 4 5 4 1 6 3 4 7 5 9 1 4 1 2 2 10 3 1 6 8 1 8 4 5 7 6 10 7 8 3 2 9 6 5 7 3 6 2 6 1 5 5 7 5 3 4 1 2 1 5 7 8 3 9 3 3...
result:
wrong answer 6th lines differ - expected: '5', found: '7'