This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// moreflags=grader.cpp
// it's too late.
//
// ...
#include "aliens.h"
// 15
#include<algorithm>
#include<map>
#include<cmath>
#include<cstdint>
#include<climits>
#include<cfloat>
#if not LOCAL
#define NDEBUG
#endif
#include<cassert>
long long take_photos(int n, int m, int numPhoto, std::vector<int> r, std::vector<int> c) {
std::vector<std::pair<int, int>> points(r.size());
for(int index=0; index<(int)r.size(); ++index)
points[index]=std::minmax({r[index], c[index]});
std::sort(begin(points), end(points),
[&](auto first, auto sec){return first.first!=sec.first ? first.first<sec.first: first.second>sec.second;
});
{ // keep only extreme points
auto out=points.begin()+1;
std::for_each(out, points.end(),[&](auto it){
assert(it.first>=out[-1].first);
if(it.second>out[-1].second){
assert(it.first>out[-1].first);
*out++=it;
}
});
points.erase(out, points.end());
}
r.clear(); c.clear(); n=-1;
auto const square=[&](int it)->int64_t{return (int64_t)it*it;};
auto const check=[&](double slope)->std::pair<int64_t, int>{
std::vector<std::pair<double, int>> value(points.size()+1);
value[0]={0, 0};
auto const t=[&](int last)->double{
return value[last].first
-(double)square(last==0 ? 0: std::max(0, points[last-1].second-points[last].first+1))
+(double)square(points[last].first);
};
struct Item{int u; double t; int numPhoto;};
std::vector<Item> hull;
auto const add=[&](int pos)->void{
Item const cur{points[pos].first, t(pos), value[pos].second};
if(hull.empty()){hull.push_back(cur); return;}
assert(cur.u>hull.back().u);
auto const bad=[&]{
auto [u1, t1, n1]=hull.end()[-2];
auto [u2, t2, n2]=hull.end()[-1];
auto [u3, t3, n3]=cur;
assert(u1<u2); assert(u2<u3);
u1-=u2; t1-=t2;
u3-=u2; t3-=t2;
return u3*t1-u1*t3<=0;
};
while(hull.size()>=2 and bad()) hull.pop_back();
hull.push_back(cur);
};
auto const query=[&](double slope){
int result=0;
auto const value=[&](int pos){
auto [u, t, numPhoto]=hull[pos];
return t+u*slope;
};
auto curValue=value(result);
for(int step=1<<(31^__builtin_clz((int)hull.size())); step; step>>=1){
if(result+step<(int)hull.size() and value(result+step)<curValue){
curValue=value(result+=step);
}
if(result-step>=0 and value(result-step)<curValue){
curValue=value(result-=step);
}
}
return std::make_pair(curValue, hull[result].numPhoto+1);
};
add(0);
for(int pos=1; ; ++pos){
auto& cur=value[pos];
cur={DBL_MAX, 0};
double const tmp2=-double(points[pos-1].second+1)*2;
/*
for(int last=0; last<pos; ++last){
cur=std::min(cur, std::make_pair(
(double)t(last) + points[last].first * tmp2
, value[last].second+1));
}
*/
cur=query(tmp2);
cur.first+=tmp2*tmp2/4+slope;
if(pos==(int)points.size()) break;
add(pos);
}
auto const [result, resultf]=value[points.size()];
return {(int64_t)std::round(result-resultf*slope), resultf};
};
// the numPhoto>=(int)points.size() case can be special-handled
double slope1=0;
auto [v1, nf1]=check(slope1);
if(numPhoto>=nf1) return v1;
assert(nf1>=(int)points.size());
double slope2; int64_t v2; int nf2;
for(slope2=1;; slope2*=2){
std::tie(v2, nf2)=check(slope2);
if(nf2<=numPhoto) break;
}
while(true){
assert(nf2<=numPhoto); assert(numPhoto<=nf1);
if(nf1==numPhoto and nf2==numPhoto){
assert(v1==v2);
return v1;
}
int64_t const lower=(int64_t)std::ceil(
std::max(
(double)v1+slope1*(nf1-numPhoto),
(double)v2+slope2*(nf2-numPhoto)
)-1e-12);
assert(nf1>nf2);
int64_t const upper=(v1*(numPhoto-nf2)+v2*(nf1-numPhoto))/(nf1-nf2);
assert(upper>=0);
assert(lower<=upper);
if(lower==upper) return lower;
assert(nf2<numPhoto); assert(numPhoto<nf1);
auto const slope3=(slope1+slope2)/2;
auto const [v3, nf3]=check(slope3);
(nf3<=numPhoto ? std::tie(v2, nf2, slope2): std::tie(v1, nf1, slope1))=std::tuple(v3, nf3, slope3);
}
}
# | 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... |