이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
used[k] = 1;
}
}
// 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... |