QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#384683#4500. LoopTobo#AC ✓84ms4660kbC++201.2kb2024-04-10 09:45:132024-04-10 09:45:14

Judging History

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

  • [2024-04-10 09:45:14]
  • 评测
  • 测评结果:AC
  • 用时:84ms
  • 内存:4660kb
  • [2024-04-10 09:45:13]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize(3, "inline")
using namespace std;

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

    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (k >= n - 1)
        {
            sort(a.begin(), a.end(), greater<>());
            for (int i : a)
                cout << i << ' ';
            cout << '\n';
            continue;
        }
        priority_queue<int> que;
        for (int i = 0; i < n; i++)
        {
            if (que.size() < k)
                que.push(a[i]), a[i] = 0;
            else if (a[i] < que.top())
            {
                int cur = que.top();
                que.pop();
                swap(a[i], cur);
                que.push(cur);
            }
        }
        for (int i = 0; i < n; i++)
            if (a[i])
                cout << a[i] << ' ';
        while (!que.empty())
        {
            cout << que.top() << ' ';
            que.pop();
        }
        cout << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 84ms
memory: 4660kb

input:

100
300000 300000
1 1 300000 153456 153456 1 153456 153456 300000 1 300000 1 300000 300000 300000 1 300000 300000 300000 1 300000 300000 300000 300000 300000 1 300000 153456 300000 1 153456 153456 1 300000 153456 300000 300000 1 300000 1 1 153456 153456 300000 153456 153456 153456 153456 1 300000 30...

output:

300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000 300000...

result:

ok 100 lines