#include "tickets.h"
#include <vector>
#include <bits/stdc++.h>
using i64 = long long;
std::pair<i64, std::vector<std::vector<int>>> solve_simple(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
std::vector<std::pair<int, int>> all;
std::vector tag(n, std::vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
all.emplace_back(i, j);
}
}
std::sort(all.begin(), all.end(), [&](auto a, auto b) {
auto [i0, j0] = a;
auto [i1, j1] = b;
if (x[i0][j0] == x[i1][j1]) {
return j0 > j1;
} else {
return x[i0][j0] > x[i1][j1];
}
});
all.resize(k * n / 2);
for (auto pair : all) {
auto [i, j] = pair;
tag[i][j] = 1;
}
std::vector pre(n, std::vector<i64>(m + 1));
for (int i = 0; i < n; i++) {
for (int j = 1; j <= m; j++) {
pre[i][j] = pre[i][j - 1] + tag[i][j - 1];
}
}
i64 S = 0;
std::vector<int> l(n, 0), r(n, m - 1);
std::vector ans(n, std::vector<int>(m, -1));
for (int round = 0; round < k; round++) {
for (int i = 0; i < n; i++) {
ans[i][l[i]] = round;
S -= x[i][l[i]++];
}
std::vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
std::sort(ord.begin(), ord.end(), [&](int i, int j) {
return pre[i][r[i] + 1] - pre[i][l[i] - 1] > pre[j][r[j] + 1] - pre[j][l[j] - 1];
});
ord.resize(n / 2);
for (int i : ord) {
S += x[i][--l[i]] + x[i][r[i]];
ans[i][l[i]] = -1;
ans[i][r[i]--] = round;
}
}
return {S, ans};
}
long long find_maximum(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
std::vector<int> l(n, k - 1), r(n, m - 1);
auto cmp = [&](int i, int j) {
return x[i][l[i]] + x[i][r[i]] < x[j][l[j]] + x[j][r[j]];
};
std::priority_queue<int, std::vector<int>, decltype(cmp)> pq(cmp);
for (int i = 0; i < n; i++) {
pq.push(i);
}
i64 rep = k * n / 2;
while (rep--) {
int i = pq.top();
pq.pop();
l[i]--;
r[i]--;
if (l[i] < r[i]) {
pq.push(i);
}
}
std::vector<std::vector<int>> opt_x;
for (int i = 0; i < n; i++) {
std::vector<int> row;
for (int j = 0; j <= l[i]; j++) {
row.push_back(x[i][j]);
}
for (int j = r[i] + 1; j < m; j++) {
row.push_back(x[i][j]);
}
opt_x.push_back(row);
}
auto [S, par_ans] = solve_simple(k, opt_x);
std::vector ans(n, std::vector<int>(m, -1));
for (int i = 0; i < n; i++) {
int j = 0;
for (int jj = 0; jj <= l[i]; jj++) {
ans[i][jj] = par_ans[i][j++];
}
for (int jj = r[i] + 1; jj < m; jj++) {
ans[i][jj] = par_ans[i][j++];
}
}
allocate_tickets(ans);
return S;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |