QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#699007 | #7904. Rainbow Subarray | zhangqi19 | WA | 192ms | 7532kb | C++20 | 2.2kb | 2024-11-01 23:43:23 | 2024-11-01 23:43:25 |
Judging History
answer
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define x first
#define y second
#define endl '\n'
#define int long long
using namespace std;
using i64 = long long;
bool muti_test = true;
void init() {}
void solve() {
int n, m;
cin >> n >> m;
vector<int>a(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] -= i;
}
map<int, int>s[3];
int sz[3] = {0};
int sum[3] = {0};
// from i to j
auto mov=[&](int i, int j, int item){
sz[i]--;
sz[j]++;
s[i][item]--;
s[j][item]++;
sum[i] -= item;
sum[j] += item;
if(s[i][item] == 0){
s[i].erase(item);
}
};
auto calc=[&](int l, int r)->int{
if(l >= r) return 0;
int mid1 = s[1].end()->first;
int mid2 = s[2].begin()->first;
int res1 = mid1 * sz[1] - sum[1] + sum[2] - mid1 * sz[2];
int res2 = mid2 * sz[1] - sum[1] + sum[2] - mid2 * sz[2];
return min(res1, res2);
};
int ans = 1;
int l = 1, r = 1;
while(r <= n){
sz[2]++;
sum[2] += a[r];
s[2][a[r]]++;
if(sz[2] - sz[1] == 2) mov(2, 1, s[2].begin()->first);
while(calc(l, r) > m){
if(s[1][a[l]] > 0){
s[1][a[l]]--;
if(!s[1][a[l]]){
s[1].erase(a[l]);
}
sz[1]--;
sum[1] -= a[l];
}else{
s[2][a[l]]--;
if(!s[2][a[l]]){
s[2].erase(a[l]);
}
sz[2]--;
sum[2] -= a[l];
}
l++;
if(sz[2] - sz[1] == 2) mov(2, 1, s[2].begin()->first);
else if(sz[1] - sz[2] == 2) mov(1, 2, s[1].end()->first);
}
ans = max(ans, r - l + 1);
r++;
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
int T = 1;
if (muti_test)
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: 192ms
memory: 7532kb
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 6 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 3 8 6 6 3 1 7 3 1 2 4 6 4 7 4 2 5 7 1 6 3 5 7 6 1 9 5 4 1 7 4 6 3 2 3 6 2 3 10 1 6 4 3 4 5 1 7 5 5 5 8 7 3 6 3 5 5 8 5 4 5 4 1 6 3 4 3 5 9 1 4 1 2 2 9 3 1 6 8 1 8 4 5 7 6 9 5 8 3 2 9 6 5 6 3 6 2 6 1 5 5 6 5 3 4 1 2 1 5 5 8 3 9 3 3 3...
result:
wrong answer 6th lines differ - expected: '5', found: '6'