QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#634867 | #9460. Diversity and Variance | ucup-team4435# | TL | 868ms | 10636kb | C++20 | 3.0kb | 2024-10-12 18:15:14 | 2024-10-14 17:20:30 |
Judging History
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);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3620kb
input:
9 3 2 34 35 36 4 2 20 35 41 74 5 2 40 30 50 20 10 5 2 50 40 30 20 10 5 2 10 20 30 40 50 5 3 30 40 20 10 50 5 4 10 20 30 40 50 5 1 30 50 40 20 10 5 0 30 50 40 20 10
output:
1 1 4 1 3 5 1 2 5 1 2 5 4 5 1 2 3 4 5 1 2 3 4 5
result:
ok 9 lines
Test #2:
score: 0
Accepted
time: 868ms
memory: 10636kb
input:
30713 38 30 2 3 8 2 0 0 4 4 5 4 8 1 2 1 3 2 0 9 5 6 6 4 8 8 1 4 6 2 3 2 0 9 2 5 8 2 3 2 8 1 5222 2247 8017 1046 1922 9545 1396 2722 8 4 99 99 99 9999 999 9 1 9999 99 98 1 3 3 3 8 8 9 7 3 8 0 6 5 6 1 5 3 7 8 6 4 5 4 5 7 9 6 7 0 5 0 0 9 0 9 0 1 1 3 1 4 5 9 2 8 8 1 2 8 0 1 2 3 6 3 9 1 6 2 7 8 0 5 3 8 7...
output:
3 5 6 11 17 18 31 32 2 3 4 5 6 7 8 4 6 7 8 1 1 2 4 5 7 8 1 5 1 2 3 1 2 3 4 6 7 8 1 11 14 19 22 28 31 35 37 38 39 45 46 47 51 54 59 64 68 74 77 82 85 86 91 95 1 1 3 1 3 4 5 6 7 8 10 13 14 17 19 23 24 25 26 27 33 39 40 1 1 2 3 4 5 6 7 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 23 24 25 26 27 28 ...
result:
ok 30713 lines
Extra Test:
score: -3
Extra Test Failed : Time Limit Exceeded on 2
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...