QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#725072#7904. Rainbow SubarrayaYi_7#WA 0ms3840kbC++172.3kb2024-11-08 16:08:232024-11-08 16:08:23

Judging History

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

  • [2024-11-08 16:08:23]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3840kb
  • [2024-11-08 16:08:23]
  • 提交

answer

#include <bits/stdc++.h>

#define int long long

bool check(std::vector<int>& a, int& k, int mid) {
    int n = a.size();

    std::multiset<int> A, B;
    int sumA{}, sumB{};

    for (int i = 0; i < mid; i++) {
        A.insert(a[i]);
        sumA += a[i];
    }

    while (A.size() > B.size()) {
        int it = *A.rbegin();
        B.insert(it);
        A.erase(A.find(it));
        sumA -= it;
        sumB += it;
    }

    int mi = *B.begin();
    int res = mi * A.size() - sumA + sumB - mi * B.size();

    for (int i = 0, j = mid; j < n; i++, j++) {
        if (a[i] < *B.begin()) {
            A.erase(A.find(a[i]));
            sumA -= a[i];
        } else {
            B.erase(B.find(a[i]));
            sumB -= a[i];
        }
        A.insert(a[j]);
        sumA += a[j];
        while (A.size() > B.size()) {
            int it = *A.rbegin();
            B.insert(it);
            A.erase(A.find(it));
            sumA -= it;
            sumB += it;
        }
        while(*A.rbegin() > *B.begin()) {
            int it1 = *A.rbegin();
            int it2 = *B.begin();
            A.erase(A.find(it1));
            B.erase(B.find(it2));
            sumA -= it1;
            sumB -= it2;
            A.insert(it2);
            B.insert(it1);
            sumA += it2;
            sumB += it1;
        }
        mi = *B.begin();
        res = std::min(res, mi * (int)A.size() - sumA + sumB - mi * (int)B.size());
    }
    return res <= k;
}

void solve() {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> a(n);
    for(int i = 0; i < n; i++) {
        std::cin >> a[i];
        a[i] -= i;
    }

    if(!check(a, k, 2)) {
        std::cout << 1 << '\n';
        return ;
    }

    int l = 2, r = n;
    while (l < r) {
        int mid = l + r + 1 >> 1;
        // int mid = 1;
        if (check(a, k, mid))
            l = mid;
        else
            r = mid - 1;
    }
    std::cout << l << '\n';
}

/*
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

*/

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t = 1;
    std::cin >> t;

    for (int i = 0; i < t; ++i) {
        solve();
    }

    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3840kb

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
2

result:

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