QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#993#634867#9460. Diversity and VarianceQingyuucup-team4435Success!2024-10-14 17:19:512024-10-14 17:19:51

详细

Extra Test:

Time Limit Exceeded

input:

20
100000 71857
5738 8435 7608 321 3546 9021 6865 764 4987 3112 4661 9670 2345 5476 5421 8889 8124 4522 8119 8812 5884 9622 1874 5355 2758 3998 758 9279 5235 7139 6313 5270 8386 1282 9448 386 8348 1790 2452 2890 5261 3159 6341 7144 8611 2144 482 1915 4349 6821 7184 6188 1915 6861 467 9326 1377 2219 ...

output:

4 6 8 12 16 20 22 27 28 34 35 36 45 47 55 56 57 75 86 87 95 97 98 103 107 109 113 120 125 126 127 128 129 132 136 155 157 160 162 163 168 170 176 177 180 182 188 190 191 192 198 199 200 204 205 206 209 211 212 213 214 219 222 223 229 231 234 237 242 245 249 251 252 253 257 258 259 261 264 272 274 27...

result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#634867#9460. Diversity and Varianceucup-team4435#TL 868ms10636kbC++203.0kb2024-10-12 18:15:142024-10-14 17:20:30

answer

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

using ll = long long;
using ld = long double;

#define all(a) begin(a), end(a)
#define len(a) int((a).size())

void solve(int /* test_num */) {
    int n, m;
    cin >> n >> m;
    map<int, set<int>> mp;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        mp[a[i]].insert(i);
    }
    sort(all(a));

    if (m == n - 1) {
        cout << "1\n";
        return;
    }

    if (m == 0) {
        for (int i = 1; i <= n; i++) {
            cout << i << " \n"[i == n];
        }
        return;
    }

    map<int, set<int>> cur;

    auto take = [&](int val) {
        auto &st = mp[val];
        assert(!st.empty());
        int res = *st.begin();
        st.erase(st.begin());
        cur[val].insert(res);
        return res;
    };

    auto rem = [&](int val) {
        auto &st = cur[val];
        assert(!st.empty());
        int res = *st.rbegin();
        st.erase(prev(st.end()));
        mp[val].insert(res);
        return res;
    };

    auto f = [&](ll sum, ll sum2) {
        return (n - m) * sum2 - sum * sum;
    };

    vector<bool> ans(n);
    ll best_sum = 0, best_sum2 = 0;
    for (int i = 0; i < n - m; i++) {
        best_sum += a[i];
        best_sum2 += a[i] * a[i];
        ans[take(a[i])] = true;
    }

    ll cur_sum = best_sum, cur_sum2 = best_sum2;
    set<int> del, ins;
    for (int i = n - m - 1, j = n - 1; i >= 0; i--, j--) {
        cur_sum -= a[i];
        cur_sum2 -= a[i] * a[i];
        cur_sum += a[j];
        cur_sum2 += a[j] * a[j];

        int p1 = rem(a[i]);
        int p2 = take(a[j]);

        if (p1 != p2) {
            del.erase(p1), ins.erase(p1);
            del.erase(p2), ins.erase(p2);
            
            if (ans[p1]) {
                del.insert(p1);
            }
            if (!ans[p2]) {
                ins.insert(p2);
            }
        }
        assert(len(del) == len(ins));

        ll cur_f = f(cur_sum, cur_sum2);
        ll sum_f = f(best_sum, best_sum2);
        if (cur_f > sum_f || (cur_f == sum_f && !del.empty() && *ins.begin() < *del.begin())) {
            best_sum = cur_sum;
            best_sum2 = cur_sum2;

            for (auto x : del) {
                ans[x] = false;
            }
            for (auto x : ins) {
                ans[x] = true;
            }
            del.clear();
            ins.clear();
        }
    }

    assert(count(all(ans), true) == n - m);
    bool first = true;
    for (int i = 0; i < n; i++) {
        if (ans[i]) {
            if (first) {
                first = false;
            } else {
                cout << ' ';
            }
            cout << i + 1;
        }
    }
    cout << '\n';
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int tests;
    cin >> tests;
    for (int test_num = 1; test_num <= tests; test_num++) {
        solve(test_num);
    }
}