# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
771257 | cadmiumsky | Carnival Tickets (IOI20_tickets) | C++17 | 0 ms | 0 KiB |
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"
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
void partition(int k, vector<vector<int>> wsign) {
int n = sz(wsign), m = sz(wsign[0]);
vector<pii> freq(n, pii{0, 0});
vector<deque<int>> sorted(n);
vector<vector<int>> rez(n, vector<int>(m, -1));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) if(wsign[i][j] == -1) sorted[i].emplace_back(j), freq[i].first++;
for(int j = 0; j < m; j++) if(wsign[i][j] == 1) sorted[i].emplace_back(j), freq[i].second++;
}
for(int Q = 0; Q < k; Q++) {
vector<int> checked(n);
vector<int> atr(n);
iota(all(atr), 0);
sort(all(atr), [&](auto a, auto b) { return freq[a].first > freq[b].first; });
for(int T = 0, cnt = 0; cnt < n / 2 && T < n; T++) {
int i = atr[T];
if(checked[i]) continue;
if(freq[i].first > 0) {
rez[i][sorted[i].front()] = Q;
sorted[i].pop_front();
cnt++;
checked[i] = 1;
freq[i].first--;
}
}
sort(all(atr), [&](auto a, auto b) { return freq[a].second > freq[b].second; });
for(int T = 0, cnt = 0; cnt < n / 2 && T < n; T++) {
int i = atr[T];
if(checked[i]) continue;
if(freq[i].second > 0) {
rez[i][sorted[i].back()] = Q;
sorted[i].pop_back();
cnt++;
checked[i] = 1;
freq[i].second--;
}
}
}
allocate_tickets(rez);
return;
}
const ll infll = 1e18 + 5;
const int nnmax = 300 + 5;
ll S2(int k, vector<vector<int>> d) {
int n = sz(d), m = sz(d[0]);
vector<vector<ll>> delta(n + 1);
vector<int> mcount(n + 1, 0);
reverse(all(d));
d.emplace_back(vector<int>());
reverse(all(d));
set<pair<ll, int>> fmerge;
ll sum = 0;
for(int i = 1; i <= n; i++) {
vector<int> tmp = d[i];
sort(all(tmp));
vector<ll> cost(k + 1);
ll tmpsum = 0;
for(int i = 0; i < k; i++) {
tmpsum += tmp[i];
cost[i + 1] -= tmpsum;
}
tmpsum = 0;
for(int i = 0, j = sz(tmp) - 1; i < k; i++, j--) {
tmpsum += tmp[j];
cost[k - i - 1] += tmpsum;
}
sum += cost[0];
fmerge.emplace(cost[1] - cost[0], i);
//cerr << i << ' ' << cost[1] - cost[0] << '\n';
delta[i] = cost;
}
for(int i = 0; i < n / 2 * k; i++) {
auto [c, ptr] = *fmerge.rbegin();
//cerr << sum << " --> " << sum + c << '\t' << ptr << '\n';
sum += c;
fmerge.erase(*fmerge.rbegin());
mcount[ptr]++;
if(mcount[ptr] == k) continue;
fmerge.emplace(delta[ptr][mcount[ptr] + 1] - delta[ptr][mcount[ptr]], ptr);
}
int current = n / 2 * k;
vector<vector<int>> wsign(n, vector<int>(m, 0));
for(int i = n; i > 0; i--) {
vector<int> atr(m);
iota(all(atr), 0);
sort(all(atr), [&](int a, int b) { return d[i][a] < d[i][b]; });
int u = mcount[i];
for(int j = 0; j < u; j++)
wsign[i - 1][atr[j]] = -1;
for(int j = m - 1, t = 0; t < k - u; t++, j--)
wsign[i - 1][atr[j]] = 1;
}
partition(k, wsign);
return sum;
}
long long find_maximum(int k, std::vector<std::vector<int>> d) {
return S2(k, d);
}