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 (md < x[i].back()) xt.PB(x[i].back()); else xt.PB(x[i][0]);
sort(xt.begin(), xt.end());
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);
}
//void allocate_tickets(vector <vector <int> > &x)
//{
// for (int i = 0; i < sz(x); i++)
// {
// for (int j = 0; j < sz(x); j++) cout << x[i][j] << " ";
//
// cout << endl;
// }
//}
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)
{
ll ans = 0;
vector <vector <int> > r;
r.resize(n);
int mn = 1e9, mx = 0;
vector <int> grt; grt.clear();
for (int i = 0; i < n; i++)
{
r[i].resize(m);
grt.PB(x[i][0]);
grt.PB(x[i].back());
for (int j = 0; j < m; j++) {mn = min(mn, x[i][j]); mx = max(mx, x[i][j]); r[i][j] = -1;}
}
sort(grt.begin(), grt.end());
grt.resize(unique(all(grt)) - grt.begin());
ans = -1;
ll nm;
for (int i = 0; i < sz(grt); i++)
{
ll val = calcer(x, grt[i]);
if (ans < val) {ans = val; nm = i;}
}
for (int i = 0; i < sz(x); i++)
if (x[i].back() > grt[nm]) r[i][sz(r[i]) - 1] = 0; else r[i][0] = 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][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;
}
}
//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);
//
//
//}
Compilation message (stderr)
tickets.cpp: In function 'll find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:157:37: warning: 'nm' may be used uninitialized in this function [-Wmaybe-uninitialized]
157 | if (x[i].back() > grt[nm]) r[i][sz(r[i]) - 1] = 0; else r[i][0] = 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... |