이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#undef local
#include <bits/stdc++.h>
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define fori(i,n) for(int i=0;i<n;i++)
#define ford(i,n) for(int i = n-1;i >= 0;i--)
#define pb push_back
#define ll long long int
//#define mod 998244353
#define pi pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define fi first
#define se second
#define printVector(v) fori(;,v.size()) {cout << v[i] << " ";} cout << "\n";
std::mt19937 rng((unsigned int) std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
struct LineContainer{
vector<pll> lines;
vector<int> ind;
int ptr = 0;
bool check(pll a1,pll a2,pll a3){
assert(a1.fi <= a2.fi && a2.fi <= a3.fi);
__int128_t c1 = a2.se - a1.se;
c1 *= (a2.fi - a3.fi);
__int128_t c2 = a3.se - a2.se;
c2 *= a1.fi - a2.fi;
return c1 >= c2;
}
void add(ll k,ll m,int i){
pll nw = {-k,-m};
int sz;
while(((sz = lines.size()) > 1) && check(lines[sz-2],lines[sz-1],nw)){
lines.pop_back();
ind.pop_back();
ptr = min(ptr,(int)lines.size() - 1);
}
lines.push_back(nw);
ind.pb(i);
}
pll get(ll x){
while(ptr < (int)lines.size()-1){
pll a1 = lines[ptr];
pll a2 = lines[ptr+1];
__int128_t tmp = x;
if((a2.se - a1.se) >= tmp * (a1.fi - a2.fi)) ptr++;
else break;
}
auto p = lines[ptr];
ll res = -(p.fi * x + p.se);
return mp(res,ind[ptr]);
}
};
pll getAns(vector<pll> a,ll lambda){
int n = a.size();
sort(all(a),[&](pll x,pll y){
return mp(x.fi,-x.se) < mp(y.fi,-y.se);
});
int ptr = a[0].se;
vector<pll> b = {a[0]};
for(int i= 1;i < n;i++){
if(a[i].se <= ptr){
continue;
}
b.push_back(a[i]);
ptr = a[i].se;
}
a = b;
n = a.size();
vector<ll> dp(n+1,1e18);
vector<int> ks(n+1,0);
LineContainer L;
{
dp[0] = 0;
ll lt1 = a[0].fi;
ll c = - 2 * lt1 + lt1 * lt1;
ll a = -2 * lt1;
L.add(a,c,0);
}
for(int i = 1;i <= n;i++){
ll ri=a[i-1].se;
auto p = L.get(ri);
dp[i] = p.fi + ri * ri + 2*ri + 1 +lambda;
ks[i] = ks[p.se] + 1;
if(i < n){
ll lt1 = a[i].fi;
ll c = dp[i] - 2 * lt1 + lt1 * lt1;
ll rt = a[i-1].se;
ll tmp = max(0ll,rt - lt1 + 1);
c -= tmp * tmp;
ll a = -2 * lt1;
L.add(a,c,i);
}
}
return mp(dp[n] - ks[n] * lambda,ks[n]);
}
long long take_photos(int n, int m, int k, vector<int> rr, vector<int> cc){
vector<pll> a;
for(int i = 0;i < n;i++){
int x = rr[i];
int y = cc[i];
a.pb(mp(min(x,y),max(x,y)));
}
sort(all(a));
a.erase(unique(all(a)),a.end());
ll l = -1e12-1,r = 1e12+1;
ll sv = getAns(a,r).fi;
while(l + 1 < r){
ll m= (l + r)/2;
auto p = getAns(a,m);
if(p.se <= k){
r = m;
sv = p.fi;
}else{
l = m;
}
}
return sv;
}
# | 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... |