이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "aliens.h"
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const ll NMAX = 5e5 + 5;
ll n, x, y, bx = -1, dp[NMAX];
vector<pair<ll, ll>> tmp, v;
ll m[NMAX], p[NMAX], sz, ii, cnt[NMAX];
int bad(int a, int b, int c){ return (p[b] - p[a]) * (m[b] - m[c]) >= (m[a] - m[b]) * (p[c] - p[b]);}
ll get(int i, ll x){return m[i] * x + p[i];}
void upd(ll mm, ll pp, int c){
m[sz] = mm;
p[sz] = pp;
cnt[sz] = c;
while(sz > 1 && bad(sz - 2, sz - 1, sz)){
m[sz - 1] = m[sz];
p[sz - 1] = p[sz];
cnt[sz - 1] = cnt[sz];
if(--sz == ii) ii--;
}
sz++;
return;
}
ll f(ll x){
while(ii + 1 < sz && get(ii, x) > get(ii + 1, x)) ii++;
return get(ii, x);
}
int go(ll cost){
sz = ii = 0;
auto&[x, y] = v[0];
upd(-2 * x, x * x, 0);
for(int i = 1; i <= n; i++){
auto&[x, y] = v[i - 1];
dp[i] = f(y + 1) + (y + 1) * (y + 1) + cost;
if(i < n){
ll xx = v[i].first;
ll dy = max(y + 1LL - xx, 0LL);
upd(-2 * xx, xx * xx - dy * dy + dp[i], cnt[ii] + 1);
}
}
return cnt[ii] + 1;
}
ll take_photos(int n_, int m, int k, vector<int> Y, vector<int> X) {
n = n_;
for(int i = 0; i < n; i++){
y = Y[i]; x = X[i];
if(x > y) swap(x, y);
tmp.emplace_back(x, y);
}
sort(all(tmp));
for(auto& [x, y] : tmp){
if(v.size() && v.back().first == x) v.pop_back();
if(v.empty() || v.back().second < y) v.emplace_back(x, y);
bx = x;
}
n = v.size();
ll l = 0, r = 1LL * m * m, mid;
while(l < r){
mid = (l + r) / 2;
if(go(mid) <= k) r = mid;
else l = mid + 1;
}
go(l);
return dp[n] - l * k;
}
/*
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<int> X(n), Y(n);
for(int i = 0; i < n; i++) cin >> X[i] >> Y[i];
cout << take_photos(n, m, k, X, Y);
return 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... |