# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
422066 | idk321 | 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 "tickets.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1000000000000000000LL;
struct Comp
{
bool operator() (const array<int, 4>& ar1, const array<int, 4>& ar2) const
{
return ar1[3] < ar2[3];
}
};
long long find_maximum(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
vector<vector<int>> answer;
answer.resize(n, vector<int>(m, -1));
int cmax = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cmax = max(cmax, x[i][j]);
}
}
if (big <= 1)
{
int turn = 0;
vector<vector<vector<int>>> isAt(n, vector<vector<int>>(2));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
isAt[i][x[i][j]].push_back(j);
}
}
int res = 0;
for (int a = 0; a < k; a++)
{
vector<array<int, 3>> byFreq;
for (int i = 0; i < n; i++) byFreq.push_back({isAt[i][0].size(), isAt[i][1].size(), i});
sort(byFreq.rbegin(), byFreq.rend());
int f0 = 0;
int f1 = 0;
for (int i = 0; i < n / 2; i++)
{
int node = byFreq[i][2];
if (!isAt[node][0].empty())
{
answer[node][isAt[node][0].back()] = a;
isAt[node][0].pop_back();
f0++;
} else
{
answer[node][isAt[node][1].back()] = a;
isAt[node][1].pop_back();
f1++;
}
}
for (int i = n / 2; i < n; i++)
{
int node = byFreq[i][2];
if (!isAt[node][1].empty())
{
answer[node][isAt[node][1].back()] = a;
isAt[node][1].pop_back();
f1++;
} else
{
answer[node][isAt[node][0].back()] = a;
isAt[node][0].pop_back();
f0++;
}
}
res += min(f1, f0);
}
allocate_tickets(answer);
return res;
}
priority_queue<array<int, 4>, vector<array<int, 4>>, Comp> pq;
vector<vector<int>> type(n, vector<int>(m));
ll res = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < k; j++)
{
res -= x[i][j];
type[i][j] = -1;
}
for (int j = m - 1, l = k - 1; l >= 0; l--, j--)
{
pq.push({l, j, i, x[i][j] + x[i][l]});
}
}
for (int i = 0; i < n * k / 2; i++)
{
auto cur = pq.top();
pq.pop();
res += cur[3];
type[cur[2]][cur[0]] = 0;
type[cur[2]][cur[1]] = 1;
}
vector<vector<int>> small(n);
vector<vector<int>> big(n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (type[i][j] == -1) small[i].push_back(j);
if (type[i][j] == 1) big[i].push_back(j);
}
}
for (int a = 0; a < k; a++)
{
vector<array<int, 3>> byFreq;
for (int i = 0; i < n; i++)
{
byFreq.push_back({small[i].size(), big[i].size(), i});
}
sort(byFreq.rbegin(), byFreq.rend());
for (int i = 0; i < n / 2; i++)
{
int y = byFreq[i][2];
answer[y][small[y].back()] = a;
small[y].pop_back();
}
for (int i = n / 2; i < n; i++)
{
int y = byFreq[i][2];
answer[y][big[y].back()] = a;
big[y].pop_back();
}
}
allocate_tickets(answer);
return res;
}
/*
5 3 1
1 8 1000
1 3 4
3 5 6
2 5 7
1 5 10
*/