# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
659022 | evenvalue | 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 "tickets.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
using int64 = long long;
using ld = long double;
constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;
struct ticket {
int value;
int color;
int index;
bool operator<(const ticket &other) const {
return value < other.value;
}
bool operator>(const ticket &other) const {
return value > other.value;
}
};
int64 find_maximum(const int32_t k, const vector<vector<int32_t>> X) {
const int n = (int) X.size(); //number of colours
const int m = (int) X[0].size();//number of tickets
vector<vector<ticket>> tickets(n, vector<ticket>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
tickets[i][j].value = X[i][j];
tickets[i][j].color = i;
tickets[i][j].index = j;
}
}
int64 ans = 0;
max_heap<ticket> suff;
vector<max_heap<ticket>> get_max(n);
for (int i = 0; i < n; i++) {
sort(tickets[i].begin(), tickets[i].end());
for (int j = 0; j < m; j++) {
get_max[i].push(tickets[i][j]);
}
for (int j = 0; j < k; j++) {
suff.push(tickets[i][j]);
ans -= tickets[i][j].value;
}
}
vector<vector<int>> allocate(n, vector<int>(m, -1));
const int kChoose = n * k;
assert(suff.size() == kChoose);
for (int assign = 0; suff.size() > kChoose / 2; assign = (assign + 1) % k) {
const auto x = suff.top();
suff.pop();
const auto y = get_max[x.color].top();
get_max[x.color].pop();
ans += x.value + y.value;
allocate[y.color][y.index] = assign;
}
assert(suff.size() == kChoose / 2);
for (int assign = 0; not suff.empty(); assign = (assign + 1) % k) {
const auto x = suff.top();
suff.pop();
allocate[x.color][x.index] = assign;
}
allocate_tickets(allocate);
return ans;
}