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 <vector <int> > &x, ll md)
{
vector <int> xt; xt.clear();
for (int i = 0; i < sz(x); i++)
if (x[i][0] <= md) xt.PB(x[i][0]); else xt.PB(x[i].back());
sort(xt.begin(), xt.end());
ll l = 0, r = 1e9;
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)
{
int n = sz(x);
int m = sz(x[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 (k == 1)
{
ll ans = 0;
vector <vector <int> > r;
r.resize(n);
int mn = 1e9, mx = 0;
for (int i = 0; i < n; i++)
{
r[i].resize(m);
for (int j = 0; j < m; j++) {mn = min(mn, x[i][j]); mx = max(mx, x[i][j]); r[i][j] = -1;}
}
ll l = mn - 1, rr = mx + 1;
while (l + 100 < rr)
{
ll mdl = l + (rr - l) / 3;
ll mdr = rr - (rr - l) / 3;
if (calcer(x, mdl) > calcer(x, mdr)) rr = mdr;
else l = mdl;
}
ans = calcer(x, l);
ll nm = l;
for (int i = l; i <= rr; i++)
{
ll val = calcer(x, i);
if (ans < val) {ans = val; nm = i;}
}
for (int i = 0; i < sz(x); i++)
if (x[i][0] <= nm) r[i][0] = 0; else r[i][sz(r[i]) - 1] = 0;
allocate_tickets(r);
return ans;
}
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][2] > 0) {gr[i][2]--; r[gr[i][2]][pos[gr[i][2]].S--] = u; i++;}
int j = sz(gr) - i;
ans += min(i, j);
while (i < sz(gr)) {gr[i][0]--; r[gr[i][2]][pos[gr[i][2]].F++] = u; i++;}
sort(gr.begin(), gr.end());
}
allocate_tickets(r);
return ans;
}
}
//int main()
//{
//// freopen("help.in", "r", stdin); freopen("help.out", "w", stdout);
//
// ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//
// cout << find_maximum(1, {{0}, {10}, {3}, {5}, {8}, {1}}) << endl;
//}
# | 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... |