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 "A.cpp"
#include <bits/stdc++.h>
// #include <vector>
using namespace std;
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(x) cout << #x << " " << x << endl;
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define pb push_back
#define ld long double
#define ll long long
// #include "tickets.h"
// #include <vector>
void allocate_tickets(vector<vector<int>> a);
struct node {
long long i, j, value;
bool operator!=(const node& other) const {
return (other.i != i || other.j != j || other.value != value);
}
bool operator==(const node& other) const {
return (other.i == i && other.j == j && other.value == value);
}
bool operator<(const node& other) const {
if(value < other.value) {
return value < other.value;
}
return j < other.j;
}
};
long long find_maximum(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
std::vector<std::vector<int>> answer;
// dbg(k)
if(m == 1) {
vector<vector<int>> answ;
long long pat = 0;
vector<long long> values;
map<long long, int> mp;
for(int i = 0; i < n; i++) {
answ.push_back({0});
values.push_back(x[i][0]);
mp[x[i][0]]++;
pat += x[i][0];
}
sort(all(values));
ll s = INT64_MAX, left = 0, leftCnt = 0;
for(auto i: mp) {
leftCnt += i.second;
left += i.second * i.first;
ll rightCnt = n - leftCnt;
pat -= i.second * i.first;
s = min(s, leftCnt * i.first - left + pat - rightCnt * i.first);
}
allocate_tickets(answ);
return s;
} else if(k == m) {
vector<node> all_members;
vector<deque<int>> plus_or_minus;
vector<int> poped_front(n);
plus_or_minus.resize(n);
answer.resize(n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
//
plus_or_minus[i].resize(m);
answer[i].resize(m);
//
all_members.push_back({i, j, x[i][j]});
}
}
sort(all(all_members));
int mid = (n * m) / 2;
long long answ = 0;
for(int i = 0; i < n * m; i++) {
if(i < mid) {
plus_or_minus[all_members[i].i][all_members[i].j] = 0;
answ -= all_members[i].value;
} else {
plus_or_minus[all_members[i].i][all_members[i].j] = 1;
answ += all_members[i].value;
}
}
for(int i = 0; i < m; i++) {
int plus = n / 2, minus = n / 2;
vector<node> new_round;
vector<bool> vis(n);
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 1 && plus_or_minus[j].back() == 1) {
int k = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][k] = i, vis[j] = true;
plus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0 && plus_or_minus[j].back() == 0) {
int k = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][k] = i, vis[j] = true;
minus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j].back() == 1) {
int k = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][k] = i, vis[j] = true;
plus--;
} else if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0) {
int k = poped_front[j];
plus_or_minus[j].pop_front();
answer[j][k] = i, vis[j] = true;
poped_front[j]++;
minus--;
}
}
assert(minus == 0 && plus == 0);
}
allocate_tickets(answer);
return answ;
} else {
vector<vector<node>> all;
for(int i = 0; i < n; i++) {
vector<node> row;
for(int j = 0; j < m; j++) {
row.push_back({i, j, x[i][j]});
}
sort(all(row));
all.push_back(row);
}
vector<set<node>> last_n_k, first_k;
// node a = {1, 0, 1};
// node b = {1, 1, 1};
// set<node> st;
// st.insert(a);
// st.insert(b);
// assert(a != b);
// dbg(sz(st))
for(int i = 0; i < n; i++) {
set<node> curr_st, curr_row;
for(int j = 0; j < k; j++) {
// cout << i << " " << j << " " << x[i][j] << endl;
curr_row.insert({i, j, x[i][j]});
}
for(int j = k; j < m; j++) {
curr_st.insert({i, j, x[i][j]});
}
// dbg(sz(curr_row))
first_k.push_back(curr_row);
last_n_k.push_back(curr_st);
}
// dbg((n * k) / 2)
for(int i = 0; i < ((n * k) / 2); i++) {
priority_queue<pair<long long, int>, vector<pair<long long, int>>> candidates;
for(int j = 0; j < n; j++) {
if(first_k[j].empty()) continue;
candidates.push({first_k[j].rbegin()->value + last_n_k[j].rbegin()->value, j});
}
int more_valuable = candidates.top().second;
node last = *first_k[more_valuable].rbegin();
last_n_k[more_valuable].erase(prev(last_n_k[more_valuable].end()));
last_n_k[more_valuable].insert(last);
first_k[more_valuable].erase(prev(first_k[more_valuable].end()));
}
int all_sz = 0;
for(int j = 0; j < n; j++) {
all_sz += sz(first_k[j]);
}
// dbg(all_sz)
assert(all_sz == (n * k) / 2);
{
//
vector<node> all_members;
vector<deque<int>> plus_or_minus;
vector<int> poped_front(n);
map<pair<int, int>, pair<int, int>> mp;
plus_or_minus.resize(n);
answer.resize(n);
for(int i = 0; i < n; i++) {
//
plus_or_minus[i].resize(k);
answer[i].resize(k);
// dbg(sz(first_k[i]))
int curr_index = 0;
vector<long long> new_values;
for(int j = 0; j < sz(first_k[i]); j++) {
// cout << x[i][j] << " ";
mp[{i, curr_index++}] = {i, j};
// all_members.push_back({i, j, x[i][j]});
new_values.push_back(x[i][j]);
}
int cnt = k - sz(first_k[i]);
for(int j = m - 1; cnt && j >= 0; j--, cnt--) {
// cout << x[i][j] << " ";
mp[{i, curr_index++}] = {i, j};
// all_members.push_back({i, j, x[i][j]});
new_values.push_back(x[i][j]);
}
for(int j = 0; j < sz(new_values); j++) {
all_members.push_back({i, j, new_values[j]});
}
// cout << endl;
}
sort(all(all_members));
int mid = (n * k) / 2;
long long answ = 0;
for(int i = 0; i < n * k; i++) {
if(i < mid) {
plus_or_minus[all_members[i].i][all_members[i].j] = 0;
answ -= all_members[i].value;
} else {
plus_or_minus[all_members[i].i][all_members[i].j] = 1;
answ += all_members[i].value;
}
}
for(int i = 0; i < k; i++) {
int plus = n / 2, minus = n / 2;
vector<node> new_round;
vector<bool> vis(n);
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 1 && plus_or_minus[j].back() == 1) {
int ki = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][ki] = i, vis[j] = true;
plus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0 && plus_or_minus[j].back() == 0) {
int ki = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][ki] = i, vis[j] = true;
minus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j].back() == 1) {
int ki = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][ki] = i, vis[j] = true;
plus--;
} else if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0) {
int ki = poped_front[j];
plus_or_minus[j].pop_front();
answer[j][ki] = i, vis[j] = true;
poped_front[j]++;
minus--;
}
}
assert(minus == 0 && plus == 0);
}
vector<vector<int>> answii(n, vector<int> (m, -1));
for(int i = 0; i < sz(answer); i++) {
for(int j = 0; j < sz(answer[i]); j++) {
auto curr_pos = mp[{i, j}];
answii[curr_pos.first][curr_pos.second] = answer[i][j];
}
}
allocate_tickets(answii);
return answ;
}
}
//
allocate_tickets(answer);
return 1;
}
# | 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... |