#include <bits/stdc++.h>
#include "aliens.h"
using namespace std;
//#define int long long
using Maxint = long long;
#define FOR(I, L, R) for(Maxint I(L) ; I <= (Maxint)R ; ++I)
#define FOD(I, R, L) for(Maxint I(R) ; I >= (Maxint)L ; --I)
#define FOA(I, A) for(auto &I : A)
#define print(A,L,R) FOR(OK, L, R){if(abs(A[OK]) >= oo / 10)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 0, R){if(abs(A[OK][KO]) <= oo / 10)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';
#define fs first
#define sd second
#define ii pair<Maxint, Maxint>
#define iii pair<Maxint, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILE "BUFF"
const Maxint N = 1e5 + 5;
const Maxint oo = 1e18;
Maxint n, m, k;
ii ary[N], rl[N], dp[N];
Maxint cost[N];
void PreWork(){
sort(ary + 1, ary + 1 + n, [&](ii &u, ii &v){
if(u.sd == v.sd){
return u.fs > v.fs;
}
return u.sd < v.sd;
});
stack<Maxint> st;
FOR(i, 1, n){
while(!st.empty() && ary[st.top()].fs >= ary[i].fs){
st.pop();
}
st.push(i);
}
n = 0;
while(!st.empty()){
rl[++n] = ary[st.top()];
st.pop();
}
reverse(rl + 1, rl + 1 + n);
FOR(i, 2, n){
Maxint x = rl[i].fs;
Maxint y = rl[i - 1].sd;
if(x <= y){
cost[i] = (y - x + 1) * (y - x + 1);
}
}
}
struct line{
Maxint a, b, c;
line(){a = b = 0;}
line(Maxint _a, Maxint _b, Maxint _c){a = _a, b = _b, c = _c;}
Maxint eval(Maxint x) const { return a * x + b; }
long double intersectX(const line &other) const{
return (long double)(other.b - b) / (long double)(a - other.a);
}
};
ii compare(ii u, ii v){
if(u.fs == v.fs){
return {u.fs, max(u.sd, v.sd)};
}
return min(u, v);
}
struct ConvexHullTrick{
vector<line> hull;
Maxint ptr = 0;
bool bad(const line &L2, const line &L1, const line &L3){
long double x1 = L2.intersectX(L1);
long double x2 = L1.intersectX(L3);
return x1 >= x2;
}
void add_line(line L){
while(hull.size() >= 2){
if(bad(hull[hull.size() - 2], hull[hull.size() - 1], L)){
hull.pop_back();
if(ptr > (Maxint)hull.size() - 1) ptr = max<Maxint>(0, (Maxint)hull.size() - 1);
}
else{
break;
}
}
hull.push_back(L);
}
ii get(Maxint x){
if(hull.empty()) return {oo, 0};
while(ptr + 1 < (Maxint)hull.size() && hull[ptr].eval(x) >= hull[ptr + 1].eval(x)) ++ptr;
return ii(hull[ptr].eval(x), hull[ptr].c);
}
void clear(){
hull.clear();
ptr = 0;
}
} cht;
ii calc(Maxint lambda){
cht.clear();
FOR(i, 1, n){
dp[i] = {oo, 0};
}
FOR(i, 1, n){
Maxint x = rl[i].fs;
Maxint y = rl[i].sd + 1;
cht.add_line(line(-2 * x, x * x + dp[i - 1].fs - cost[i], dp[i - 1].sd));
ii val = cht.get(y);
dp[i] = ii(val.fs + y * y + lambda, val.sd + 1);
}
return dp[n];
}
Maxint Alien(){
Maxint l = 0, r = 3 * oo;
Maxint res = 0;
while(l <= r){
Maxint mid = (l + r) >> 1;
if(calc(mid).sd >= k){
l = mid + 1;
res = mid;
}
else{
r = mid - 1;
}
}
return calc(res).fs - k * res;
}
Maxint take_photos(int _n, int _m, int _k, vector<int> r, vector<int> c){
n = _n;
m = _m;
k = _k;
FOR(i, 1, n){
ary[i].fs = r[i - 1];
ary[i].sd = c[i - 1];
if(ary[i].fs > ary[i].sd) swap(ary[i].fs, ary[i].sd);
}
PreWork();
return Alien();
}
컴파일 시 표준 에러 (stderr) 메시지
aliens.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
aliens_c.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~| # | 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... |