QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#278140 | #7904. Rainbow Subarray | ucup-team956 | WA | 137ms | 7912kb | C++20 | 2.8kb | 2023-12-07 12:57:26 | 2023-12-07 12:57:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define time chrono::system_clock::now().time_since_epoch().count()
mt19937_64 rnd(time);
#define maxn 1000005
#define int long long
int read() {int x;cin>>x;return x;}
void solve() {
int n = read(), k = read();
vector<int>a(n + 1);
for(int i = 1; i <= n; i++) {
a[i] = read();
a[i] -= i;
}
set<pair<int,int>>s1,s2;
vector<int>f(n + 1);
int sum1 = 0, sum2 = 0, l = 1;
auto gao1 = [&]() {
auto it = s1.end(); it --;
s1.erase(s1.find(*it));
// cout << it->fi`rst << " " << it->second << "!!!\n";
// cout << "ok!!\n";
s2.insert(*it);
f[it -> second] = 2;
sum1 -= it -> first;
sum2 += it -> first;
};
auto gao2 = [&]() {
auto it = s2.begin();
s2.erase(s2.find(*it));
s1.insert(*it);
f[it -> second] = 1;
sum2 -= it -> first;
sum1 += it -> first;
};
int ans = 1;
for(int r = 1; r <= n; r++) {
s2.insert({a[r], r});
f[r] = 2;
sum2 += a[r];
// cout << s1.size() << " " << s2.size() << "\n";
if(s2.size() - s1.size() == 2) {
gao2();
// cout << "gao2:" << s2.size() << "\n";
}
while(1) {
// cout << s1.size() << " " << s2.size() << "\n";
int a1 = sum1;
int a2 = sum2;
// cout << a2 << " " << a1 << "\n";
// cout <<"!!!\n";
if(s2.size() > s1.size()) {
a2 -= s2.begin() -> first;
}
if(a2 - a1 <= k) { // ok
break;
}
int id = f[l];
// cout << l << "\n";
// cout << "r" << r << "\n";
// cout << "id:" << id << "\n";
// return;
if(id == 1) {
s1.erase({a[l], l});
sum1 -= a[l];
l += 1;
if(s2.size() - s1.size() == 2) gao2();
}
else {
s2.erase({a[l], l});
sum2 -= a[l];
l += 1;
// cout << "!!!>>\n";
// cout << s1.size() << " " << s2.size() << "\n";
if(s1.size() > s2.size()) gao1();
// cout << s1.size() << " " << s2.size() << "\n";
// cout << s2.begin() ->first << " " << s2.begin() -> second << "\n";
// cout << "sum :" << sum1 << " " << sum2 << "\n";
// return;
}
}
// return;
ans = max(ans, r - l + 1);
}
cout << ans << "\n";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t = read();
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3536kb
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: 137ms
memory: 7912kb
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 5 1 1 5 3 2 7 8 7 7 1 9 6 2 4 3 1 7 8 7 3 4 3 9 3 8 6 6 3 1 7 3 1 2 4 6 4 7 4 1 5 7 1 6 3 5 7 6 1 9 5 3 1 7 4 5 3 2 3 6 2 3 10 1 6 3 2 4 5 1 7 5 5 5 8 7 3 6 3 5 5 8 5 4 5 3 1 5 3 4 3 5 9 1 3 1 2 2 9 3 1 6 8 1 8 4 5 7 6 9 4 8 3 2 9 6 5 6 3 6 2 4 1 5 5 5 3 3 4 1 2 1 5 5 8 3 7 3 3 3...
result:
wrong answer 11th lines differ - expected: '4', found: '5'