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;
using ll = long long;
ll find_maximum(int K, vector<vector<int>> x) {
int N = x.size();
int M = x[0].size();
if (M == 1) {
allocate_tickets(vector(N, vector{0}));
vector<int> v(N);
for (int i = 0; i < N; i++) {
v[i] = x[i][0];
}
sort(v.begin(), v.end());
ll s = 0;
for (int i = 0; i < N; i++) {
s += abs(v[N / 2] - v[i]);
}
return s;
}
if (K == 1) {
vector dp(N + 1, vector<ll>(N / 2 + 1, -1e18));
dp[0][0] = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j <= N / 2; j++) {
if (j < N / 2) {
dp[i + 1][j + 1] = dp[i][j] + x[i][M - 1];
}
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - x[i][0]);
}
}
vector a(N, vector<int>(M, -1));
for (int i = N - 1, j = N / 2; i >= 0; i--) {
if (dp[i][j] - x[i][0] == dp[i + 1][j]) {
a[i][0] = 0;
} else {
a[i][M - 1] = 0;
j--;
}
}
allocate_tickets(a);
return dp[N][N / 2];
}
if (K == M) {
vector<tuple<int, int, int>> v;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
v.emplace_back(x[i][j], i, j);
}
}
sort(v.begin(), v.end());
vector f(N, vector<bool>(M));
ll ans = 0;
for (int i = 0; i < N * M / 2; i++) {
auto [t, x, y] = v[i];
ans -= t;
}
for (int i = N * M / 2; i < N * M; i++) {
auto [t, x, y] = v[i];
ans += t;
f[x][y] = 1;
}
vector a(N, vector<int>(M));
int x = 0, y = K - 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (!f[i][j]) {
a[i][j] = y--;
} else {
a[i][j] = x++;
}
if (y == -1) {
y = K - 1;
}
if (x == K) {
x = 0;
}
}
}
allocate_tickets(a);
return ans;
}
if (N <= 80 && M <= 80) {
vector pre(N, vector<ll>(K + 1));
vector suf(N, vector<ll>(K + 1));
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
pre[i][j + 1] = pre[i][j] + x[i][j];
suf[i][j + 1] = suf[i][j] + x[i][M - 1 - j];
}
}
vector dp(N + 1, vector<ll>(N * K / 2 + 1, -1e18));
dp[0][0] = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j <= K; j++) {
for (int k = j; k <= N * K / 2; k++) {
dp[i + 1][k] = max(dp[i + 1][k], dp[i][k - j] + suf[i][j] - pre[i][K - j]);
}
}
}
vector a(N, vector<int>(M, -1));
int s = 0, t = K - 1;
for (int i = N - 1, j = N * K / 2; i >= 0; i--) {
for (int k = 0; k <= min(j, K); k++) {
if (dp[i][j - k] + suf[i][k] - pre[i][K - k] == dp[i + 1][j]) {
for (int p = 0; p < K - k; p++) {
a[i][p] = s++;
if (s == K) {
s = 0;
}
}
for (int p = K - 1; p >= K - k; p--) {
a[i][p] = t--;
if (t == -1) {
t = K - 1;
}
}
j -= k;
break;
}
}
}
allocate_tickets(a);
return dp[N][N * K / 2];
}
}
Compilation message (stderr)
tickets.cpp: In function 'll find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:126:1: warning: control reaches end of non-void function [-Wreturn-type]
126 | }
| ^
# | 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... |