# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
928089 | EJIC_B_KEDAX | Carnival Tickets (IOI20_tickets) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "tickets.h"
using ll = long long;
using namespace std;
ll find_maximum(int k, vector<vector<int>> t) {
int n = t.size(), m = t[0].size();
vector<int> all;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
all.push_back(t[i][j]);
}
}
sort(all.begin(), all.end());
int middle = all[all.size() / 2];
ll res = 0;
vector<pair<ll, int>> a(n);
vector<int> bg(n, 0), end(n, m - 1);
vector<vector<int>> s(n, vector<int>(m, -1));
for (int i = 0; i < n; i++) {
a[i].first = 0;
a[i].second = i;
for (int j = 0; j < m; j++) {
t[i][j] -= middle;
}
}
for (int r = 0; r < k; r++) {
vector<pair<int, int>> l, r;
for (int i = 0; i < n; i++) {
if (abs(t[i][bg[i]]) > abs(t[i][end[i]])) {
if (t[i][end[i]] < 0) {
l.emplace_back(INT32_MAX, i);
} else {
l.emplace_back(abs(abs(t[i][bg[i]]) - abs(t[i][end[i]])), i);
}
} else {
if (t[i][bg[i]] > 0) {
l.emplace_back(INT32_MAX, i);
} else {
l.emplace_back(abs(abs(t[i][bg[i]]) - abs(t[i][end[i]])), i);
}
}
}
sort(l.begin(), l.end(), greater<>());
sort(r.begin(), r.end(), greater<>());
for (auto [w, i] : l) {
li.insert(i);
}
for (auto [w, i] : r) {
ri.insert(i);
}
set<int> li, ri;
while (l.size() > n / 2) {
li.erase(l.back().second);
ri.insert(l.back().second);
l.pop_back();
}
while (r.size() > n / 2) {
ri.erase(r.back().second);
li.insert(r.back().second);
r.pop_back();
}
vector<int> nw;
for (int i : li) {
nw.push_back(t[i][bg[i]++]);
}
for (int i : ri) {
nw.push_back(t[i][end[i]--]);
}
sort(nw.begin(), nw.end());
for (int i : nw) {
ans += abs(i);
}
assert(0 >= nw[n / 2 - 1] && 0 <= nw[n / 2]);
}
allocate_tickets(s);
return res;
}