// #include "A.cpp"
#include <bits/stdc++.h>
// #include <vector>
using namespace std;
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(x) cout << #x << " " << x << endl;
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define pb push_back
#define ld long double
#define ll long long
// #include "tickets.h"
// #include <vector>
void allocate_tickets(vector<vector<int>> a);
struct node {
long long i, j, value;
bool operator!=(const node& other) const {
return (other.i != i || other.j != j || other.value != value);
}
bool operator==(const node& other) const {
return (other.i == i && other.j == j && other.value == value);
}
bool operator<(const node& other) const {
if(value != other.value) {
return value < other.value;
}
if(i != other.i) {
return i < other.i;
}
return j < other.j;
}
};
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;
// dbg(k)
if(m == 1) {
vector<vector<int>> answ;
long long pat = 0;
vector<long long> values;
map<long long, int> mp;
for(int i = 0; i < n; i++) {
answ.push_back({0});
values.push_back(x[i][0]);
mp[x[i][0]]++;
pat += x[i][0];
}
sort(all(values));
ll s = INT64_MAX, left = 0, leftCnt = 0;
for(auto i: mp) {
leftCnt += i.second;
left += i.second * i.first;
ll rightCnt = n - leftCnt;
pat -= i.second * i.first;
s = min(s, leftCnt * i.first - left + pat - rightCnt * i.first);
}
allocate_tickets(answ);
return s;
} else if(k == m) {
vector<node> all_members;
vector<deque<int>> plus_or_minus;
vector<int> poped_front(n);
plus_or_minus.resize(n);
answer.resize(n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
//
plus_or_minus[i].resize(m);
answer[i].resize(m);
//
all_members.push_back({i, j, x[i][j]});
}
}
sort(all(all_members));
int mid = (n * m) / 2;
long long answ = 0;
for(int i = 0; i < n * m; i++) {
if(i < mid) {
plus_or_minus[all_members[i].i][all_members[i].j] = 0;
answ -= all_members[i].value;
} else {
plus_or_minus[all_members[i].i][all_members[i].j] = 1;
answ += all_members[i].value;
}
}
for(int i = 0; i < m; i++) {
int plus = n / 2, minus = n / 2;
vector<node> new_round;
vector<bool> vis(n);
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 1 && plus_or_minus[j].back() == 1) {
int k = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][k] = i, vis[j] = true;
plus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0 && plus_or_minus[j].back() == 0) {
int k = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][k] = i, vis[j] = true;
minus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j].back() == 1) {
int k = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
answer[j][k] = i, vis[j] = true;
plus--;
} else if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0) {
int k = poped_front[j];
plus_or_minus[j].pop_front();
answer[j][k] = i, vis[j] = true;
poped_front[j]++;
minus--;
}
}
assert(minus == 0 && plus == 0);
}
allocate_tickets(answer);
return answ;
} else {
vector<vector<node>> all;
for(int i = 0; i < n; i++) {
vector<node> row;
for(int j = 0; j < m; j++) {
row.push_back({i, j, x[i][j]});
}
sort(all(row));
all.push_back(row);
}
deque<deque<node>> last_n_k, first_k;
for(int i = 0; i < n; i++) {
deque<node> curr_row, curr_st;
for(int j = 0; j < k; j++) {
curr_row.push_back(all[i][j]);
}
for(int j = k; j < m; j++) {
curr_st.push_back(all[i][j]);
}
first_k.push_back(curr_row);
last_n_k.push_back(curr_st);
}
// dbg((n * k) / 2)
priority_queue<pair<long long, int>, vector<pair<long long, int>>> candidates;
for(int j = 0; j < n; j++) {
if(first_k[j].empty()) continue;
candidates.push({first_k[j].back().value + last_n_k[j].back().value, j});
}
for(int i = 0; i < ((n * k) / 2); i++) {
int more_valuable = candidates.top().second;
candidates.pop();
node last = first_k[more_valuable].back();
last_n_k[more_valuable].pop_back();
first_k[more_valuable].pop_back();
last_n_k[more_valuable].push_front(last);
if(!first_k[more_valuable].empty()) {
candidates.push({first_k[more_valuable].back().value + last_n_k[more_valuable].back().value, more_valuable});
}
}
int all_sz = 0;
for(int j = 0; j < n; j++) {
all_sz += sz(first_k[j]);
}
assert(all_sz == ((n * k) / 2));
{
//
vector<node> all_members;
vector<deque<int>> plus_or_minus;
vector<int> poped_front(n);
map<pair<int, int>, pair<int, int>> mp;
plus_or_minus.resize(n);
answer.resize(n);
for(int i = 0; i < n; i++) {
//
plus_or_minus[i].resize(k);
answer[i].resize(k);
int curr_index = 0;
vector<long long> new_values;
for(int j = 0; j < sz(first_k[i]); j++) {
mp[{i, curr_index++}] = {all[i][j].i, all[i][j].j};
// dbg(all[i][j].value)
new_values.push_back(all[i][j].value);
}
// dbg()
int cnt = k - sz(first_k[i]);
for(int j = m - cnt; j < m; j++) {
// dbg(all[i][j].value)
mp[{i, curr_index++}] = {all[i][j].i, all[i][j].j};
new_values.push_back(all[i][j].value);
}
sort(all(new_values));
for(int j = 0; j < sz(new_values); j++) {
all_members.push_back({i, j, new_values[j]});
}
}
sort(all(all_members));
int mid = (n * k) / 2;
long long answ = 0;
for(int i = 0; i < n * k; i++) {
if(i < mid) {
plus_or_minus[all_members[i].i][all_members[i].j] = 0;
answ -= all_members[i].value;
} else {
plus_or_minus[all_members[i].i][all_members[i].j] = 1;
answ += all_members[i].value;
}
}
for(int i = 0; i < k; i++) {
int plus = n / 2, minus = n / 2;
vector<node> new_round;
vector<bool> vis(n + 1, false);
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 1 && plus_or_minus[j].back() == 1) {
int ki = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
assert(ki < k);
answer[j][ki] = i, vis[j] = true;
plus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0 && plus_or_minus[j].back() == 0) {
int ki = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
assert(ki < k);
answer[j][ki] = i, vis[j] = true;
minus--;
}
}
for(int j = 0; j < n; j++) {
if(!vis[j] && plus && sz(plus_or_minus[j]) && plus_or_minus[j].back() == 1) {
int ki = plus_or_minus[j].size() - 1 + poped_front[j];
plus_or_minus[j].pop_back();
assert(ki < k);
answer[j][ki] = i, vis[j] = true;
plus--;
} else if(!vis[j] && minus && sz(plus_or_minus[j]) && plus_or_minus[j][0] == 0) {
int ki = poped_front[j];
plus_or_minus[j].pop_front();
assert(ki < k);
answer[j][ki] = i, vis[j] = true;
poped_front[j]++;
minus--;
}
}
assert(minus == 0 && plus == 0);
}
vector<vector<int>> answii(n, vector<int> (m, -1));
for(int i = 0; i < sz(answer); i++) {
for(int j = 0; j < sz(answer[i]); j++) {
auto curr_pos = mp[{i, j}];
assert(curr_pos.first < n && curr_pos.second < m);
answii[curr_pos.first][curr_pos.second] = answer[i][j];
}
}
allocate_tickets(answii);
return answ;
}
}
//
allocate_tickets(answer);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
24 ms |
7280 KB |
Output is correct |
6 |
Correct |
560 ms |
161984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
1108 KB |
Output is correct |
5 |
Correct |
39 ms |
10664 KB |
Output is correct |
6 |
Correct |
1040 ms |
246876 KB |
Output is correct |
7 |
Correct |
1198 ms |
277796 KB |
Output is correct |
8 |
Correct |
5 ms |
936 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
12 ms |
3216 KB |
Output is correct |
13 |
Correct |
38 ms |
10428 KB |
Output is correct |
14 |
Correct |
40 ms |
10640 KB |
Output is correct |
15 |
Correct |
1324 ms |
306700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
680 KB |
Output is correct |
5 |
Correct |
31 ms |
4708 KB |
Output is correct |
6 |
Correct |
5 ms |
1000 KB |
Output is correct |
7 |
Correct |
6 ms |
2192 KB |
Output is correct |
8 |
Correct |
870 ms |
134016 KB |
Output is correct |
9 |
Correct |
867 ms |
132556 KB |
Output is correct |
10 |
Correct |
803 ms |
132740 KB |
Output is correct |
11 |
Correct |
861 ms |
133920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
1108 KB |
Output is correct |
6 |
Correct |
4 ms |
1196 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
1236 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
5 ms |
1236 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
1108 KB |
Output is correct |
6 |
Correct |
4 ms |
1196 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
1236 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
5 ms |
1236 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
13 |
Correct |
24 ms |
7436 KB |
Output is correct |
14 |
Correct |
30 ms |
7508 KB |
Output is correct |
15 |
Correct |
43 ms |
10608 KB |
Output is correct |
16 |
Correct |
30 ms |
4720 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
3 ms |
1620 KB |
Output is correct |
19 |
Correct |
2 ms |
1236 KB |
Output is correct |
20 |
Correct |
50 ms |
11512 KB |
Output is correct |
21 |
Correct |
48 ms |
11332 KB |
Output is correct |
22 |
Correct |
63 ms |
13524 KB |
Output is correct |
23 |
Correct |
59 ms |
13308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
24 ms |
7280 KB |
Output is correct |
12 |
Correct |
560 ms |
161984 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
1108 KB |
Output is correct |
17 |
Correct |
39 ms |
10664 KB |
Output is correct |
18 |
Correct |
1040 ms |
246876 KB |
Output is correct |
19 |
Correct |
1198 ms |
277796 KB |
Output is correct |
20 |
Correct |
5 ms |
936 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
12 ms |
3216 KB |
Output is correct |
25 |
Correct |
38 ms |
10428 KB |
Output is correct |
26 |
Correct |
40 ms |
10640 KB |
Output is correct |
27 |
Correct |
1324 ms |
306700 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
680 KB |
Output is correct |
32 |
Correct |
31 ms |
4708 KB |
Output is correct |
33 |
Correct |
5 ms |
1000 KB |
Output is correct |
34 |
Correct |
6 ms |
2192 KB |
Output is correct |
35 |
Correct |
870 ms |
134016 KB |
Output is correct |
36 |
Correct |
867 ms |
132556 KB |
Output is correct |
37 |
Correct |
803 ms |
132740 KB |
Output is correct |
38 |
Correct |
861 ms |
133920 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
852 KB |
Output is correct |
41 |
Correct |
2 ms |
852 KB |
Output is correct |
42 |
Correct |
3 ms |
980 KB |
Output is correct |
43 |
Correct |
3 ms |
1108 KB |
Output is correct |
44 |
Correct |
4 ms |
1196 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
4 ms |
1236 KB |
Output is correct |
48 |
Correct |
4 ms |
1108 KB |
Output is correct |
49 |
Correct |
5 ms |
1236 KB |
Output is correct |
50 |
Correct |
6 ms |
1236 KB |
Output is correct |
51 |
Correct |
24 ms |
7436 KB |
Output is correct |
52 |
Correct |
30 ms |
7508 KB |
Output is correct |
53 |
Correct |
43 ms |
10608 KB |
Output is correct |
54 |
Correct |
30 ms |
4720 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
3 ms |
1620 KB |
Output is correct |
57 |
Correct |
2 ms |
1236 KB |
Output is correct |
58 |
Correct |
50 ms |
11512 KB |
Output is correct |
59 |
Correct |
48 ms |
11332 KB |
Output is correct |
60 |
Correct |
63 ms |
13524 KB |
Output is correct |
61 |
Correct |
59 ms |
13308 KB |
Output is correct |
62 |
Correct |
70 ms |
21452 KB |
Output is correct |
63 |
Correct |
79 ms |
21644 KB |
Output is correct |
64 |
Correct |
99 ms |
14596 KB |
Output is correct |
65 |
Correct |
542 ms |
114528 KB |
Output is correct |
66 |
Correct |
374 ms |
51828 KB |
Output is correct |
67 |
Correct |
7 ms |
2244 KB |
Output is correct |
68 |
Correct |
6 ms |
1128 KB |
Output is correct |
69 |
Correct |
581 ms |
164288 KB |
Output is correct |
70 |
Correct |
1242 ms |
248816 KB |
Output is correct |
71 |
Correct |
907 ms |
136064 KB |
Output is correct |
72 |
Correct |
1690 ms |
348980 KB |
Output is correct |
73 |
Correct |
1755 ms |
317444 KB |
Output is correct |
74 |
Correct |
1276 ms |
252416 KB |
Output is correct |
75 |
Correct |
1210 ms |
247076 KB |
Output is correct |