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 <bits/stdc++.h>
#include "tickets.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define N 1005
#define PB push_back
#define sz(x) int(x.size())
#define P 31
#define F first
#define M ll(1e9 + 7)
#define S second
#define all(x) x.begin(), x.end()
#define endl '\n'
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
//typedef tree<int, null_type, less_equal <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll mlt(ll a, ll b) {return (a * b) % M;}
ll sm(ll a, ll b) {return (a + b) % M;}
ll calc(vector <vector <int> > &x, ll md)
{
ll sum = 0;
for (int i = 0; i < sz(x); i++) sum += abs(md - x[i][0]);
return sum;
}
ll clc(vector <int> &x, ll md)
{
ll sum = 0;
for (int i = 0; i < sz(x); i++) sum += abs(md - x[i]);
return sum;
}
ll calcer(vector <int> &xt)
{
ll l = xt[0], r = xt.back();
while (l < r)
{
ll mdl = (l + r) / 2;
ll mdr = mdl + 1;
if (clc(xt, mdl) > clc(xt, mdr)) l = mdr;
else r = mdl;
}
return clc(xt, l);
}
ll find_maximum(int k, vector<vector<int> > x)
{
bool f = 1;
int n = sz(x);
int m = sz(x[0]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) if (x[i][j] > 1) f = 0;
if (m == 1)
{
ll l = 0, r = 1e9;
while (l < r)
{
ll mdl = (l + r) / 2;
ll mdr = mdl + 1;
if (calc(x, mdl) < calc(x, mdr)) r = mdl;
else l = mdr;
}
ll ans = calc(x, l);
for (int i = 0; i < n; i++)
x[i][0] = 0;
allocate_tickets(x);
return ans;
}
else if (!f && k == 1)
{
vector <vector <int> > r;
r.resize(n);
for (int i = 0; i < n; i++) r[i].resize(m, -1);
vector <array <int, 3> > gr; gr.clear();
for (int i = 0; i < n; i++)
gr.PB({-x[i][0], -x[i][m - 1], i});
sort(all(gr));
vector <int> xt; xt.clear();
for (int i = n / 2; i < n; i++) {xt.PB(x[gr[i][2]][0]); r[gr[i][2]][0] = 0;}
for (int i = 0; i < n / 2; i++) {xt.PB(x[gr[i][2]][m - 1]); r[gr[i][2]][m - 1] = 0;}
sort(all(xt));
allocate_tickets(r);
return calcer(xt);
}
else
{
vector <vector <int> > r;
r.resize(n);
pair <int, int> pos[n];
for (int i = 0; i < n; i++)
{
r[i].resize(m);
for (int j = 0; j < m; j++) r[i][j] = -1;
pos[i].F = 0;
pos[i].S = m - 1;
}
vector <array <int, 3> > gr; gr.clear();
for (int i = 0; i < n; i++)
{
int kl = 0;
for (int j = 0; j < m; j++) if (x[i][j] == 0) kl++;
gr.PB({kl, m - kl, i});
}
sort(gr.begin(), gr.end());
int ans = 0;
for (int u = 0; u < k; u++)
{
int i = 0;
while (i < (sz(gr) + 1) / 2 && gr[i][1] > 0) {gr[i][1]--; r[gr[i][2]][pos[gr[i][2]].S--] = u; i++;}
int kl = i, kr = 0;
while (i < sz(gr)) {if (gr[i][0] > 0) {gr[i][0]--; r[gr[i][2]][pos[gr[i][2]].F++] = u; kr++;} else {gr[i][1]--; r[gr[i][2]][pos[gr[i][2]].S--] = u; kl++;} i++;}
ans += min(kl, kr);
sort(gr.begin(), gr.end());
}
allocate_tickets(r);
return ans;
}
}
# | 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... |