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 "squad.h"
#include<algorithm>
using namespace std;
#define INF 1234567890
#define ll long long
struct Line {
ll a, b;
double s;
int idx;
bool operator< (const Line &n) const
{
if (a != n.a) return a < n.a;
return b > n.b;
}
};
int N;
vector<Line> v, re;
double Cross(Line &A, Line &B)
{
return (double)(B.b - A.b) / (A.a - B.a);
}
struct CHT {
vector<Line> cht;
void insert(Line &n)
{
while (!cht.empty())
{
if (cht.back().a == n.a)
{
if (cht.back().b < n.b)
{
re.push_back(cht.back());
cht.pop_back();
}
else return;
}
else
{
n.s = Cross(cht.back(), n);
if (n.s < cht.back().s)
{
re.push_back(cht.back());
cht.pop_back();
}
else break;
}
}
cht.push_back(n);
}
int query(double x)
{
if (cht.empty()) return -1;
int lo = 0, hi = (int)cht.size();
for (int i = 0; i < 20; i++)
{
int mid = lo + hi >> 1;
if (x < cht[mid].s) hi = mid;
else lo = mid;
}
return lo;
}
} x, xx, y, yy;
void Init(std::vector<int> A, std::vector<int> D, std::vector<int> P){
N = A.size();
for (int i = 0; i < N; i++)
{
Line n = { A[i], P[i], 0, i };
v.push_back(n);
}
sort(v.begin(), v.end());
for (int i = 0; i < N; i++)
x.insert(v[i]);
sort(re.begin(), re.end());
int M = (int)re.size();
for (int i = 0; i < M; i++)
xx.insert(re[i]);
v.clear(); re.clear();
for (int i = 0; i < N; i++)
{
Line n = { D[i], P[i], 0, i };
v.push_back(n);
}
sort(v.begin(), v.end());
for (int i = 0; i < N; i++)
y.insert(v[i]);
sort(re.begin(), re.end());
M = (int)re.size();
for (int i = 0; i < M; i++)
yy.insert(re[i]);
}
long long BestSquad(int X, int Y){
double pos = (double)X / Y;
int i = x.query(pos);
int j = y.query(pos);
if (x.cht[i].idx != y.cht[j].idx)
return x.cht[i].a*X + x.cht[i].b*Y + y.cht[j].a*X + y.cht[j].b*Y;
else
{
ll res = 0;
int ii = xx.query(pos);
if (ii != -1)
res = max(res, xx.cht[ii].a*X + xx.cht[ii].b*Y + y.cht[j].a*X + y.cht[j].b*Y);
int jj = yy.query(pos);
if (jj != -1)
res = max(res, x.cht[i].a*X + x.cht[i].b*Y + yy.cht[jj].a*X + yy.cht[jj].b*Y);
if (i - 1 >= 0)
res = max(res, x.cht[i - 1].a*X + x.cht[i - 1].b*Y + y.cht[j].a*X + y.cht[j].b*Y);
if (i + 1 < x.cht.size())
res = max(res, x.cht[i + 1].a*X + x.cht[i + 1].b*Y + y.cht[j].a*X + y.cht[j].b*Y);
if (j - 1 >= 0)
res = max(res, x.cht[i].a*X + x.cht[i].b*Y + y.cht[j - 1].a*X + y.cht[j - 1].b*Y);
if (j + 1 < y.cht.size())
res = max(res, x.cht[i].a*X + x.cht[i].b*Y + y.cht[j + 1].a*X + y.cht[j + 1].b*Y);
return res;
}
return -1;
}
Compilation message (stderr)
squad.cpp: In member function 'int CHT::query(double)':
squad.cpp:63:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = lo + hi >> 1;
~~~^~~~
squad.cpp: In function 'long long int BestSquad(int, int)':
squad.cpp:120:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i + 1 < x.cht.size())
~~~~~~^~~~~~~~~~~~~~
squad.cpp:124:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j + 1 < y.cht.size())
~~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |