이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<ll,ll> pii;
typedef tuple<int,int,int> tp;
const int N = 1e6 + 5;
//const int mod = 1e9 + 7;
const ll oo = 5e18;
ll n, m, k, val[N], f[5][N], pre, nx, p[N], d[N], g[N];
struct line{
ll x, y;
int id;
};
vector<line> hull;
int ptr = 0;
bool kt(line a, line b, line c){
return (double)(c.y - b.y) / (double)(b.x - c.x) < (double)(b.y - a.y) / (double)(a.x - b.x);
}
ll cal(int w, ll a){
return 1ll * hull[w].x * a + hull[w].y;
}
pii get(ll x){
if(ptr > hull.size() - 1) ptr = hull.size() - 1;
while(ptr < hull.size() - 1 && cal(ptr + 1, x) <= cal(ptr, x)) ptr++;
return {cal(ptr, x), hull[ptr].id};
}
void add(line x){
while(hull.size() >= 2 && kt(hull[hull.size() - 2], hull.back(), x)) hull.pop_back();
hull.push_back(x);
}
pii pc(ll C){
hull.clear();
ptr = 0;
// d[1] = 1ll * val[p[1]] * val[p[1]] + C;
// g[1] = 1;
for(int i = 1; i <= n; i ++){
ll X = val[p[i]] - p[i];
ll w = 1ll * X * X - 1ll * max(0ll, val[p[i]] - p[i] + p[i - 1])
* max(0ll, val[p[i]] - p[i] + p[i - 1]);
line tmp = {2 * X, d[i - 1] + w, i - 1};
add(tmp);
auto kq = get(p[i]);
d[i] = kq.fi + p[i] * p[i] + C;
g[i] = g[kq.se] + 1;
// cerr << i << " " << d[i] << " " << g[i] << " u\n";
// if(1ll * (p[i] - p[1] + val[p[1]]) * (p[i] - p[1] + val[p[1]]) + C <= d[i]){
// d[i] = 1ll * (p[i] - p[1] + val[p[1]]) * (p[i] - p[1] + val[p[1]]) + C;
// g[i] = 1;
// }
}
return {d[n], g[n]};
}
ll take_photos(int _n,int _m,int _k,vector<int> R,vector<int> C){
n = _n, m = _m, k = _k;
deque<int> sk;
for(int i = 0; i < n; i ++){
ll r = ++R[i], c = ++C[i];
if(r <= c) val[c] = max(val[c], c - r + 1);
else val[r] = max(val[r], r - c + 1);
}
for(int i = 1; i <= m; i ++){
if(!val[i]) continue;
while(!sk.empty() && i - val[i] + 1 <= sk.front() - val[sk.front()] + 1) sk.pop_front();
sk.push_front(i);
}
vector<pii> vr;
int t = 0;
while(!sk.empty()){
p[++t] = sk.back();
sk.pop_back();
}
n = t;
if(n <= k){
ll ans = 1ll * val[p[1]] * val[p[1]];
for(int i = 2; i <= n; i ++){
ans += 1ll * val[p[i]] * val[p[i]] - 1ll * max(0ll, val[p[i]] - p[i] + p[i - 1]) * max(0ll, val[p[i]] - p[i] + p[i - 1]);
}
return ans;
}
if(n * k <= 1e7){
nx = 1, pre = 0;
for(int i = 1; i <= n; i ++) f[0][i] = 1ll * (p[i] - p[1] + val[p[1]]) * (p[i] - p[1] + val[p[1]]);
for(int j = 2; j <= k; j ++){
hull.clear();
ptr = 0;
for(int i = j; i <= n; i ++){
ll X = val[p[i]] - p[i];
ll w = 1ll * X * X - 1ll * max(0ll, val[p[i]] - p[i] + p[i - 1])
* max(0ll, val[p[i]] - p[i] + p[i - 1]);
line tmp = {2 * X, f[pre][i - 1] + w, i - 1};
add(tmp);
f[nx][i] = get(p[i]).first + p[i] * p[i];
}
swap(pre, nx);
}
return f[pre][n];
}
// for(int i = 1; i <= n; i ++) cout << p[i] << " " << val[p[i]] << " o\n";
ll l = 0, r = 1e18, mid, ans = 1e18;
while(l <= r){
mid = (l + r) / 2;
pii kq = pc(mid);
// cerr << mid << " " << kq.fi << " " << kq.se << " i\n";
if(kq.se <= k){
r = mid - 1;
ans = kq.fi - 1ll * k * mid;
// if(kq.fi - 1ll * k * mid == 1660355) cerr << kq.fi << " " << mid << " " << kq.se << " dcm\n";
}else l = mid + 1;
}
return ans;
}
//#define lpv
#ifdef lpv
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
int n, m, k; cin >> n >> m >> k;
vector<int> r(n), c(n);
for (int i = 0; i < n; ++i) cin >> r[i] >> c[i];
cout << take_photos(n, m, k, r, c) << "\n";
}
#endif // lpv
컴파일 시 표준 에러 (stderr) 메시지
aliens.cpp: In function 'pii get(long long int)':
aliens.cpp:38:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if(ptr > hull.size() - 1) ptr = hull.size() - 1;
| ~~~~^~~~~~~~~~~~~~~~~
aliens.cpp:39:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | while(ptr < hull.size() - 1 && cal(ptr + 1, x) <= cal(ptr, x)) ptr++;
| ~~~~^~~~~~~~~~~~~~~~~
# | 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... |