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 "bits/stdc++.h"
#include "tickets.h"
using namespace std;
#define ll long long
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define forn(i, n) for(int i=0;i < n; ++i)
ll calc(vector<int> v) {
sort(all(v));
int n = sz(v);
int mid = v[n / 2];
ll ans = 0;
for(int i = 0; i < n; ++i) ans += abs(v[i] - mid);
return ans;
}
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(n, vector<int>(m, -1));
if(m == 1) {
answer.assign(n, vector<int>(m, 0));
vector<ll> a;
forn(i, n) a.pb(x[i][0]);
sort(all(a));
ll ans = 0;
forn(i, n) ans += abs(a[i] - a[n / 2]);
allocate_tickets(answer);
return ans;
}
if(k == 1) {
vector<int> v;
vector<int> mn(n, 0), mx(n, 0);
vector<pair<int, int>> amogus;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(mn[i] > x[i][j]) mn[i] = j;
if(mx[i] < x[i][j]) mx[i] = j;
}
amogus.pb({x[i][mn[i]] + x[i][mx[i]], i});
}
sort(all(amogus));
for(int i = 0; i < n; ++i) {
int j = amogus[i].second;
if(i < n / 2) {
v.pb(x[j][mn[j]]);
answer[j][mn[j]] = 0;
} else {
v.pb(x[j][mx[j]]);
answer[j][mx[j]] = 0;
}
}
ll ans = calc(v);
allocate_tickets(answer);
return ans;
}
vector<int> zeroes[n], ones[n];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(x[i][j] == 0) zeroes[i].pb(j);
else ones[i].pb(j);
}
}
priority_queue<pair<int, int>> q;
for(int i = 0; i < n; ++i) {
if(sz(zeroes[i])) q.push({sz(zeroes[i]), i});
}
ll ans = 0;
for(int kk = 0; kk < k; ++kk) {
int cnt = 0;
vector<int> v;
vector<bool> vis(n, false);
while(cnt < n / 2 && sz(q)) {
answer[q.top().second][zeroes[q.top().second].back()] = kk;
zeroes[q.top().second].pop_back();
assert(vis[q.top().second] == false);
vis[q.top().second] = true;
q.pop();
++cnt;
v.pb(0);
}
for(int i = 0; i < n && cnt < n; ++i) {
if(sz(ones[i]) && !vis[i]) {
v.pb(1);
++cnt;
answer[i][ones[i].back()] = kk;
ones[i].pop_back();
vis[i] = true;
}
}
for(int i = 0; i < n && cnt < n; ++i) {
if(!vis[i]) {
assert(sz(zeroes[i]));
v.pb(0);
answer[i][zeroes[i].back()] = kk;
zeroes[i].pop_back();
++cnt;
vis[i] = true;
}
}
while(sz(q)) q.pop();
for(int i = 0; i < n; ++i) {
if(sz(zeroes[i])) {
q.push({sz(zeroes[i]), i});
}
}
assert(sz(v) == n);
ans += calc(v);
}
allocate_tickets(answer);
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... |