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 <vector>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <unordered_map>
#include <functional>
#include <bitset>
#include <sstream>
#include <queue>
#include "tickets.h"
using namespace std;
using int64 = long long;
// void allocate_tickets(std::vector<std::vector<int>> _d) {
// }
struct Swap {
int64 gain;
int row;
int l;
int r;
bool operator < (const Swap& other) const {
if (gain != other.gain)
return gain > other.gain;
return row < other.row;
}
};
long long find_maximum(int K, vector<vector<int>> D) {
int N = D.size();
int M = D[0].size();
set<Swap> cand;
int64 cur = 0;
vector<vector<int>> assig(N, vector<int>(M, -1));
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
cur += (-D[i][j]);
}
cand.insert(Swap{D[i][M-1] + D[i][K-1], i, K - 1, M - 1});
}
vector<int> deg(N);
for (int steps = 0; steps < K * N / 2; steps++) {
auto el = *cand.begin();
cand.erase(cand.begin());
cur += el.gain;
deg[el.row]++;
if (el.l >= 1) {
el.l--;
el.r--;
el.gain = D[el.row][el.r] + D[el.row][el.l];
cand.insert(el);
}
}
auto old_deg = deg;
priority_queue<pair<int, int>> heap;
for (int i = 0; i < N; i++) {
heap.push({deg[i], i});
}
for (int r = 0; r < K; r++) {
// Assign N / 2 to the largest
vector<pair<int, int>> put_back;
vector<char> used(N);
for (int i = 0; i < N / 2; i++) {
auto el = heap.top();
heap.pop();
assig[el.second][M - 1 - (el.first - 1)] = r;
assert(!used[el.second]);
used[el.second] = true;
el.first--;
put_back.push_back(el);
}
for (auto el : put_back)
heap.push(el);
}
for (int i = 0; i < N; i++) {
old_deg[i] = K - old_deg[i];
vector<char> used(K);
for (int j = 0; j < M; j++) {
if (assig[i][j] != -1) {
used[assig[i][j]] = 1;
}
}
int k = 0;
for (int j = 0; j < old_deg[i]; j++) {
while (used[k])
k++;
assig[i][j] = k;
}
}
for (int k = 0; k < K; k++) {
cerr << "round: " << k << "\n";
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (assig[i][j] == k) {
cerr << i << ", " << j << " = " << D[i][j] << "\n";
}
}
}
}
allocate_tickets(assig);
return cur;
}
// int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// // int N, M, K;
// // cin >> N >> M >> K;
// // vector<vector<int>> D(N, vector<int>(M));
// // for (int i = 0; i < N; i++) {
// // for (int j = 0; j < M; j++) {
// // cin >> D[i][j];
// // }
// // }
// // cerr << find_maximum(K, D) << "\n";
// //cerr << find_maximum(2, {{0, 2, 5},{1, 1, 3}}) << "\n";
// //cerr << find_maximum(1, {{5, 9}, {1, 4}, {3, 6}, {2, 7}}) << "\n";
// return 0;
// }
# | 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... |