1837F - Editorial for Two - CodeForces Solution


binary search data structures greedy

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

class segtree {
  private:
    struct node {
        long long s = 0;
        int cnt = 0;
        
        node() : s(0), cnt(0) { }
        
        void assign(int v) {
            if (v) {
                s = v;
                cnt = 1;
            } else {
                s = 0;
                cnt = 0;
            }
        }

        void merge(const node &a, const node &b) {
            s = a.s + b.s;
            cnt = a.cnt + b.cnt;
        }
    };

    int b;
    node* tree;

  public:
    const int n;
    
    segtree(int _n) : n(_n) {
        assert(_n > 0);
        for (b = 1; b < n; b <<= 1);
        tree = new node[b << 1];
        for (int i = 0; i < n; i++) {
            tree[b + i] = node();
        }
        for (int i = b - 1; i > 0; i--) {
            tree[i].merge(tree[i * 2], tree[i * 2 + 1]);
        }
    }
    
    template <typename... M>
    void update(int ind, const M&... v) {
        assert(0 <= ind && ind < n);
        tree[b + ind].assign(v...);
        for (int i = (b + ind) / 2; i > 0; i >>= 1) {
            tree[i].merge(tree[i * 2], tree[i * 2 + 1]);
        }
    }
    
    long long query(int k) const {
        int x = 1;
        long long sum = 0;
        while (x < b) {
            const node& L = tree[x << 1];
            long long cnt_left = L.cnt;
            if (cnt_left <= k) {
                sum += L.s;
                k -= cnt_left;
                x = (x << 1) ^ 1;
            } else {
                x = (x << 1);
            }
        }
        const node& nd = tree[x];
        if (k > 0) {
            sum += nd.s;
            k -= nd.cnt;
        }
        if (k > 0) {
            return 2e18;
        }
        return sum;
    }
};

void foo() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<pair<int,int>> values(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        values[i] = {a[i], i};
    }
    sort(values.begin(), values.end());
    vector<int> pos(n);
    for (int i = 0; i < n; i++) {
        pos[values[i].second] = i;
    }
    
    segtree left_st(n), right_st(n);
    for (int i = 0; i < n; i++) {
        right_st.update(pos[i], a[i]);
    }
    
    long long ans = 2e18;
    
    auto consider = [&](int L) -> pair<long long, long long> {
        const int R = k - L;
        long long sumL = left_st.query(L);
        long long sumR = right_st.query(R);
        ans = min(ans, max(sumL, sumR));
        return make_pair(sumL, sumR);
    };
    
    int take_left = 0;
    consider(0);
    for (int i = 0; i < n; i++) {
        left_st.update(pos[i], a[i]);
        right_st.update(pos[i], 0);
        while (take_left < k) {
            auto p = consider(take_left + 1);
            if (p.first <= p.second) {
                take_left++;
            } else {
                break;
            }
        }
        consider(take_left);
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int cases;
    cin >> cases;
    for (int t = 1; t <= cases; t++) {
        foo();
    }
}


Comments

Submit
0 Comments
More Questions

1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String