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;
}
vector<int> lo(N + 1), hi(N + 1);
for (int i = 0; i < N; i++) {
int cnt = count(x[i].begin(), x[i].end(), 1);
lo[i + 1] = lo[i] + K - min(K, M - cnt);
hi[i + 1] = hi[i] + min(cnt, K);
}
int ans = 0, q = 0;
if (lo[N] <= N * K / 2 && N * K / 2 <= hi[N]) {
ans = N * K / 2;
q = N * K / 2;
} else if (lo[N] > N * K / 2) {
ans = N * K - lo[N];
q = lo[N];
} else {
ans = q = hi[N];
}
int s = 0, t = K - 1;
vector a(N, vector<int>(M, -1));
for (int i = N - 1; i >= 0; i--) {
int cnt = count(x[i].begin(), x[i].end(), 1);
for (int j = K - min(K, M - cnt); j <= min(cnt, K); j++) {
if (lo[i] <= q - j && q - j <= hi[i]) {
for (int k = 0; k < K - j; k++) {
a[i][k] = s++;
if (s == K) {
s = 0;
}
}
for (int k = M - 1; k >= M - j; k--) {
a[i][k] = t--;
if (t == -1) {
t = K - 1;
}
}
q -= j;
break;
}
}
}
allocate_tickets(a);
return ans;
}
# | 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... |