QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#679304#7904. Rainbow Subarrayenar#TL 0ms3480kbC++203.2kb2024-10-26 17:19:392024-10-26 17:19:40

Judging History

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

  • [2024-10-26 17:19:40]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3480kb
  • [2024-10-26 17:19:39]
  • 提交

answer

#include <bits/stdc++.h>
using i64 = long long;

template<typename T>
struct Fenwick {
    Fenwick(int n) : v(n + 1) {}
    void update(int pos, T dx) {
        for(int i = pos; i < v.size(); i += (i & -i)) {
            v[i] += dx;
        }
    }
    T query(int pos) {
        T res{};
        for(int i = pos; i > 0; i -= (i & -i)) {
            res += v[i];
        }
        return res;
    }
    T range(int l, int r) {
        return query(r) - query(l - 1);
    }
    std::vector<T> v;
};

void solve() {
    i64 n, k;
    std::cin >> n >> k;
    std::vector<i64> v(n + 1), pre(n + 1);
    for(int i = 1; i <= n; ++i) {
        std::cin >> v[i];
        v[i] = v[i] - i + n;
        pre[i] = pre[i - 1] + v[i];
    }
    std::vector<i64> temp = v;
    std::sort(temp.begin() + 1, temp.end());
    temp.erase(std::unique(temp.begin() + 1, temp.end()), temp.end());
    std::vector<int> pos(n + 1);
    for(int i = 1; i <= n; ++i) {
        pos[i] = std::lower_bound(temp.begin() + 1, temp.end(), v[i]) - temp.begin();
    }

    Fenwick<i64> tr(n);
    Fenwick<i64> tot(n);
    std::multiset<i64> st1, st2;
    auto modify = [&](i64 p, i64 sgn) ->void {
        tr.update(pos[p], 1LL * sgn * v[p]);
        tot.update(pos[p], sgn);
    };
    auto balance = [&](int k) ->void {
        while(st1.size() > k) {
            i64 val = *st1.rbegin();
            st1.extract(val);
            st2.insert(val);
        }
        while(st1.size() < k && st2.size()) {
            i64 val = *st2.begin();
            st1.insert(val);
            st2.extract(val);
        }
    };
    auto insert = [&](i64 val) ->void {
        st1.insert(val);
    };
    auto erase = [&](i64 val) ->void {
        if(st1.find(val) != st1.end()) {
            st1.extract(val);
        } else {
            st2.extract(val);
        }
    };
    i64 l = 1, r = n, ans = 1;
    while(l <= r) {
        i64 mid = (l + r) / 2;
        i64 res = LLONG_MAX;
        i64 i = 1, j = 1;
        while(j < mid) {
            modify(j, 1);
            insert(v[j]);
            balance((mid + 1) / 2);
            ++j;
        }
        while(j <= n) {
            modify(j, 1);
            insert(v[j]);
            balance((mid + 1) / 2);

            //i64 ave = std::round((double)(pre[j] - pre[i - 1]) / mid);
            i64 ave = *st1.rbegin();

            i64 p = std::upper_bound(temp.begin() + 1, temp.end(), ave) - temp.begin() - 1;
            i64 num = tot.range(1, p);
            i64 sum = 1LL * ave * num - tr.range(1, p) + tr.range(p + 1, n) - 1LL * ave * (mid - num); 
            res = std::min(res, sum);
            modify(i, -1);
            erase(v[i]);
            balance((mid + 1) / 2);
            ++i, ++j;
        }
        while(i <= n) {
            modify(i, -1);
            erase(v[i]);
            ++i;
        }
        if(res <= k) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    std::cout << ans << '\n';
}


int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T = 1;
    std::cin >> T;
    while(T--) {
        solve();
    }
}


詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3480kb

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
Time Limit Exceeded

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
4
1
1
3
2
2
7
8
7
7
1
7
6
2
4
3
1
6
7
7
3
4
3
9
3
8
6
6
3
1
6
3
1
2
4
6
4
6
4
1
4
7
1
6
3
5
6
6
1
7
5
3
1
6
4
5
3
2
2
6
2
3
10
1
4
3
2
4
5
1
7
5
5
5
8
5
3
6
3
5
5
8
5
4
5
2
1
5
2
3
3
4
8
1
3
1
2
2
8
3
1
6
8
1
8
4
5
6
6
8
4
8
3
2
8
4
5
6
2
6
2
4
1
5
4
5
3
2
4
1
2
1
4
5
8
3
7
3
3
3...

result: