This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
#else
#include "tickets.h"
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifdef LOCAL
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define dbg(...) 17;
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; }
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) { bool f = true; os << "{"; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "}"; }
template<typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; }
template<typename T, typename... Args> void debug(string s, T x, Args... args) { cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...); }
#ifdef LOCAL
void allocate_tickets(vector<vector<int>> s) {
return;
}
#else
#endif
ll find_maximum(int k, vector<vector<int>> x) {
const ll INF = 1e15;
int n = x.size();
int m = x[0].size();
vector<vector<int>> res(n, vector<int>(m, -1));
if (m == 1) {
vector<int> all;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
res[i][j] = 0;
all.push_back(x[i][j]);
}
}
sort(all.begin(), all.end());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (i < n / 2) {
ans -= all[i];
} else {
ans += all[i];
}
}
allocate_tickets(res);
return ans;
} else if (k == 1) {
vector<array<ll, 2>> use;
for (int i = 0; i < n; i++) {
use.push_back({x[i][0] + x[i][m - 1], i});
}
sort(use.begin(), use.end());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (i < n / 2) {
res[use[i][1]][0] = 0;
ans -= x[use[i][1]][0];
} else {
res[use[i][1]][m - 1] = 0;
ans += x[use[i][1]][m - 1];
}
}
allocate_tickets(res);
return ans;
} else {
vector<int> lef(n);
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
lef[i] = k / 2;
} else {
lef[i] = k - k / 2;
}
}
set<array<ll, 2>> shift_left;
set<array<ll, 2>> shift_right;
auto calc_right = [&](int id) -> ll {
// you want to move something to minus
if (lef[id] == k) {
// never advantageous
return INF;
} else {
// what you'll get
int rig = k - lef[id];
return x[id][lef[id]] + x[id][m - rig];
}
};
auto calc_left = [&](int id) -> ll {
// you want to move something to plus
if (lef[id] == 0) {
return -INF;
} else {
int rig = k - lef[id];
return x[id][lef[id] - 1] + x[id][m - rig - 1];
}
};
auto modify = [&](int id, int change) {
ll l = calc_left(id);
ll r = calc_right(id);
shift_left.erase({l, id});
shift_right.erase({r, id});
lef[id] += change;
l = calc_left(id);
r = calc_right(id);
shift_left.insert({l, id});
shift_right.insert({r, id});
};
for (int i = 0; i < n; i++) {
shift_left.insert({calc_left(i), i});
shift_right.insert({calc_right(i), i});
}
while (*shift_right.begin() < *shift_left.rbegin()) {
int add = (*shift_right.begin())[1];
int sub = (*shift_left.rbegin())[1];
modify(sub, -1);
modify(add, 1);
}
ll ans = 0;
vector<array<int, 3>> neg;
vector<array<int, 3>> pos;
vector<int> neg_filled(k);
vector<int> pos_filled(k);
int neg_cur = 0;
int pos_cur = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < lef[i]; j++) {
ans -= x[i][j];
if (neg_filled[neg_cur] == n / 2) {
neg_cur++;
}
neg_filled[neg_cur]++;
res[i][j] = neg_cur;
neg.push_back({x[i][j], i, j});
}
for (int j = m - lef[i]; j < m; j++) {
ans += x[i][j];
if (pos_filled[pos_cur] == n / 2) {
pos_cur++;
}
pos_filled[pos_cur]++;
res[i][j] = pos_cur;
pos.push_back({x[i][j], i, j});
}
}
sort(neg.begin(), neg.end());
sort(pos.begin(), pos.end());
assert(neg.back()[0] <= pos[0][0]);
// allocation
allocate_tickets(res);
return ans;
}
}
#ifdef LOCAL
int main() {
// freopen("file.in", "r", stdin);
// string line; cin >> line;
int n, m, k; cin >> n >> m >> k;
vector<vector<int>> s(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> s[i][j];
}
}
cout << find_maximum(k, s) << '\n';;
return 0;
}
#else
#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... |