QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#275680#7904. Rainbow SubarrayJacka1#WA 36ms7936kbC++142.8kb2023-12-04 23:03:492023-12-04 23:03:49

Judging History

你现在查看的是最新测评结果

  • [2024-06-09 00:00:26]
  • hack成功,自动添加数据
  • (/hack/652)
  • [2023-12-04 23:03:49]
  • 评测
  • 测评结果:WA
  • 用时:36ms
  • 内存:7936kb
  • [2023-12-04 23:03:49]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

using i64 = long long;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<i64> a(n + 10), b(n + 10);
    vector<i64> cnt_less(n + 10), cnt_greater(n + 10);
    vector<i64> sum_less(n + 10), sum_greater(n + 10);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];  
        b[i] = a[i] - a[i - 1];
    }
    for (int i = 2; i <= n; i++) {
        cnt_less[i] = cnt_less[i - 1] + (b[i] < 1);
        sum_less[i] = sum_less[i - 1] + (b[i] < 1) * b[i];
        cnt_greater[i] = cnt_greater[i - 1] + (b[i] >= 1);
        sum_greater[i] = sum_greater[i - 1] + (b[i] >= 1) * b[i];
    }
    int l = 0, r = n - 1;
    auto check = [&](int mid) -> bool {
        if (mid == 0) return true;
        for (int l = 2, r = l + mid - 1; r <= n; l++, r++) {
            i64 s = sum_greater[r] - sum_greater[l - 1] + sum_less[r] - sum_less[l - 1];
            i64 need = cnt_less[r] - cnt_less[l - 1] - sum_less[r] + sum_less[l - 1];
            int len = r - l + 1;
            if (s > len) {
                need -= (b[r] < 1);
                need += b[r] * (b[r] < 1);
                need -= (b[l] < 1);
                need += b[l] * (b[l] < 1);
                i64 more = s - len;
                need += more;
                i64 lmore = max(0ll, b[l] - 1);
                i64 x = b[l], y = b[r];
                b[l] -= min(more, lmore);
                more -= min(more, lmore);
                b[r] -= more;
                need += (b[r] < 1);
                need -= b[r] * (b[r] < 1);
                need += (b[l] < 1);
                need -= b[l] * (b[l] < 1);
                b[l] = x, b[r] = y;
            }
            if (s < len) {
                need -= (b[r] < 1);
                need += b[r] * (b[r] < 1);
                need -= (b[l] < 1);
                need += b[l] * (b[l] < 1);
                i64 more = len - s;
                need += more;
                i64 lmore = max(0ll, 1 - b[l]);
                i64 x = b[l], y = b[r];
                b[l] += min(more, lmore);
                more -= min(more, lmore);
                b[r] += more;
                need += (b[r] < 1);
                need -= b[r] * (b[r] < 1);
                need += (b[l] < 1);
                need -= b[l] * (b[l] < 1);
                b[l] = x, b[r] = y;
            }
            if (need <= k) return true;
        }    
        return false;
    };
    while (l < r) {
        int mid = l + r + 1 >> 1;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    cout << l + 1 << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int T;
    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: 3544kb

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: 36ms
memory: 7936kb

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:

2
4
3
2
6
7
7
2
4
1
4
1
1
3
2
2
9
9
7
9
1
9
7
2
4
3
2
6
8
7
3
4
3
10
3
8
6
6
3
1
7
4
2
2
4
7
4
6
4
2
6
7
1
8
3
5
6
9
1
7
5
3
1
6
5
5
3
2
2
7
2
3
10
1
7
3
2
4
5
2
8
5
5
6
8
7
3
6
3
5
5
9
5
4
5
2
2
5
2
5
3
5
10
1
4
2
2
2
10
5
1
6
8
2
8
4
5
7
6
10
6
8
3
2
9
4
5
6
2
6
2
4
1
5
5
5
3
2
4
1
2
1
4
5
8
3
8
4...

result:

wrong answer 1st lines differ - expected: '1', found: '2'