#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> p2;
#define rep(i, high) for (ll i = 0; i < (high); i++)
#define repp(i, lo, high) for (ll i = (lo); i < (high); i++)
#define repe(i, container) for (auto& i : container)
#define sz(x) ((ll)(x).size())
#define all(x) begin(x), end(x)
void allocate_tickets(std::vector<std::vector<int>> _d);
long long find_maximum(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
if (m==1)
{
vector<vector<int>> ans(n, vector<int>(m));
rep(i, n) rep(j, m) ans[i][j] = 0;
allocate_tickets(ans);
vi selection;
rep(i, n) selection.push_back(x[i][0]);
sort(all(selection));
ll mid = selection[n / 2];
ll ret = 0;
rep(i, n)
{
ret += abs(mid - x[i][0]);
}
return ret;
}
vector<vector<int>> ans(n, vector<int>(m, -1));
ll ret = 0;
vector<tuple<int, int, int,int>> trades;
rep(i, n)
{
vector<p2> col(m);
rep(j, m) col[j] = p2(x[i][j], j);
sort(all(col));
ret += col.back().first;
ans[i][col.back().second] = 0;
trades.emplace_back(-col.back().first - col[0].first, i, col[0].second, col.back().second);
}
sort(all(trades));
reverse(all(trades));
rep(i, n / 2)
{
auto [decrease, row, a, b] = trades[i];
ans[row][b] = -1;
ans[row][a] = 0;
ret += decrease;
}
allocate_tickets(ans);
return ret;
}
#if _LOCAL
static int n;
static int m;
static int k;
static std::vector<std::vector<int>> d;
static std::vector<std::vector<int>> x;
static int called = 0;
static void check(bool cond, std::string message) {
if (!cond) {
printf("%s\n", message.c_str());
exit(0);
}
}
void allocate_tickets(std::vector<std::vector<int>> _d) {
check(!called, "allocate_tickets called more than once");
d = _d;
check((int)d.size() == n, "allocate_tickets called with parameter of wrong size");
for (int i = 0; i < n; i++) {
check((int)d[i].size() == m, "allocate_tickets called with parameter of wrong size");
}
called = 1;
}
int main() {
assert(scanf("%d %d %d", &n, &m, &k) == 3);
x.resize(n);
for (int i = 0; i < n; i++) {
x[i].resize(m);
for (int j = 0; j < m; j++) {
assert(scanf("%d", &x[i][j]) == 1);
}
}
fclose(stdin);
long long answer = find_maximum(k, x);
check(called, "failure to call allocate_tickets");
printf("%lld\n", answer);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j) printf(" ");
printf("%d", d[i][j]);
}
printf("\n");
}
fclose(stdout);
return 0;
}
#endif
# | 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... |