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 <bits/stdc++.h>
using namespace std;
#define k K
#define x X
#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
int n, m, k;
int ans_val;
vector<vector<int> > x;
vector<vector<int> > ans;
bool cmp(const int &a, const int &b) {
return x[a][0] + x[a][m - 1] < x[b][0] + x[b][m - 1];
}
void solve_1_2() {
vector<int> v(n, 0);
for(int i = 0; i < n; i++) {
v[i] = i;
}
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < n / 2; i++) {
ans[v[i]][0] = 0;
ans_val -= x[v[i]][0];
}
for(int i = n / 2; i < n; i++) {
ans[v[i]][m - 1] = 0;
ans_val += x[v[i]][m - 1];
}
}
void re_sort(vector<pair<int, int> > &v) {
/*for(auto tmp : v) {
cout << tmp.fi << " ";
}
cout << "\n";*/
vector<pair<int, int> > w;
int a = 0, b = n / 2;
while(a < n / 2 || b < n) {
if(a == n / 2) {
w.pb(v[b]);
b++;
} else if(b == n) {
w.pb(v[a]);
a++;
} else if(v[a].fi < v[b].fi) {
w.pb(v[a]);
a++;
} else {
w.pb(v[b]);
b++;
}
}
v = w;
/*for(auto tmp : v) {
cout << tmp.fi << " ";
}
cout << "\n";*/
}
void solve_3() {
vector<pair<int, int> > v(n, mp(0, 0));
for(int i = 0; i < n; i++) {
v[i].se = i;
for(int j = 0; j < m; j++) {
if(x[i][j]) {
v[i].fi++;
}
}
}
sort(v.begin(), v.end());
vector<vector<int> > which_0(n);
vector<vector<int> > which_1(n);
/*for(pair<int, int> y : v) {
cout << y.fi << " ";
}
cout << "\n";*/
for(int idx = 0; idx < k; idx++) {
int cnt = 0;
for(int i = 0; i < n / 2; i++) {
if(v[i].fi + (int) which_0[v[i].se].size() + (int) which_1[v[i].se].size() < m) {
which_0[v[i].se].pb(idx);
} else {
which_1[v[i].se].pb(idx);
cnt++;
}
}
for(int i = n / 2; i < n; i++) {
if(v[i].fi) {
v[i].fi--;
which_1[v[i].se].pb(idx);
cnt++;
} else {
which_0[v[i].se].pb(idx);
}
}
ans_val += min(cnt, n - cnt);
re_sort(v);
}
for(int i = 0; i < n; i++) {
//cout << i << ": ";
for(int j = 0; j < (int) which_0[i].size(); j++) {
//cout << which_0[i][j] << " ";
ans[i][j] = which_0[i][j];
}
//cout << "- ";
for(int j = 0; j < (int) which_1[i].size(); j++) {
//cout << which_1[i][j] << " ";
ans[i][m - j - 1] = which_1[i][j];
}
//cout << "\n";
}
}
/*
4 8 4
0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 1
0 0 0 1 1 1 1 1
0 0 0 0 0 0 1 1
*/
#undef k
#undef x
int find_maximum(signed k, vector<vector<signed> > x) {
n = (int) x.size();
m = (int) x[0].size();
K = k;
X.assign(n, vector<int> (m, -2));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
X[i][j] = x[i][j];
}
}
#define k K
#define x X
ans.assign(n, vector<int> (m, -1));
if(k == 1) {
solve_1_2();
} else {
solve_3();
}
vector<vector<signed> > real_ans(n, vector<signed> (m, -2));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
real_ans[i][j] = ans[i][j];
}
}
allocate_tickets(real_ans);
return ans_val;
}
# | 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... |