/* Author : Mychecksdead */
#include<bits/stdc++.h>
//#include"aliens.h"
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 2e5+100, M = 1e5+10, K = 105, MX = 30;
const ll INF = 1e18;
struct P{
ll m, b, idx, num_k;
double eval(double x){
return x*m + b;
}
};
double intersection(P x, P y){
return (x.b - y.b) / (double)(y.m - x.m);
}
struct CH{
vector<P> C;
int idx=0;
void insert(ll c, ll m, int id, int numk){
P x = {m, c, id, numk};
if(C.size() && C.back().m == x.m && C.back().b < x.b) return;
while(C.size() && C.back().m == x.m && C.back().b >= x.b) C.pop_back();
while(C.size() > 1 && (x.eval(intersection(C.back(), C[int(C.size())-2])) <= C.back().eval(intersection(C.back(), C[int(C.size())-2]))))
C.pop_back();
C.pb(x);
idx=min(idx,int(C.size())-1);
}
P get(ll fi){
while(idx+1<C.size() && C[idx+1].eval(fi) <= C[idx].eval(fi)) ++idx;
return C[idx];
}
};
int n, m, k;
array<ll, 2> a[N];
ll go[2*N];
vector<ll> val;
pair<ll, int> f(ll cost){
CH cht;
cht.insert(go[0]*go[0], -2*go[0], 0, 0);
ll ans = INF;
int num_k = 0;
for(ll i = 1; i <= m; ++i){
auto p = cht.get(val[i]);
ll dp = p.eval(val[i]) + val[i] * val[i] + cost;
ll best = go[p.idx];
//cout << i << ' ' << j << ' ' << dp[i][j] << ' ' << best << ' ';
if(go[i] == MOD){
if(ans > dp){
ans = dp;
num_k = p.num_k + 1;
}else if(ans == dp) num_k = min(num_k, (int)p.num_k + 1);
}
best = max(best, (go[i]));
if(go[i] < val[i]){
dp -= (best - val[i]) * (best - val[i]);
dp += go[i] * go[i];
cht.insert(dp, -2*go[i], i, p.num_k + 1);
}else{
dp += go[i] * go[i];
cht.insert(dp, -2*go[i], i, p.num_k + 1);
}
//cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
}
return pair<ll, int>{ans, num_k};
}
ll take_photos(int nn, int mm, int kk, vector<int> r, vi c){
n=nn,m=mm,k=kk;
for(int i=0;i<n;++i){ a[i][0]=r[i],a[i][1]=c[i]; }
/*void solve(){
cin >> n >> m >> k;
for(int i = 0; i < n; ++i) cin >> a[i][0] >> a[i][1];
*/
for(int i = 0; i < n; ++i){
if(a[i][0] > a[i][1]){
swap(a[i][0], a[i][1]);
}
val.pb(a[i][0]), val.pb(a[i][1]+1);
}
val.pb(0);
sort(all(val));
val.erase(unique(all(val)), val.end());
sort(a, a+n, [&](const array<ll,2> &x, const array<ll, 2> &y){
return array<ll,2>{x[1],x[0]}<array<ll,2>{y[1],y[0]};
});
//for(int i = 0; i < n; ++i) cout << a[i][0] << ' ' << a[i][1] << '\n';
ll mn = MOD;
int ptr = n - 1;
m = int(val.size()) - 1;
for(int i = m; i >= 0; --i){
while(ptr >= 0 && a[ptr][1] >= val[i]){
mn = min(mn, a[ptr][0]);
--ptr;
}
go[i] = mn;
}
//for(int j = 0; j <= m; ++j) cout << go[j] << ' '; en;
ll ans = 0;
ll L = 0, R = 1e12;
while(L<=R){
ll m = (L+R)/2;
auto [cost, num_k] = f(m);
//cout << cost - num_k * m << ' ' << num_k << ' ' << m << '\n';
if(num_k >= k){
ans = m;
L = m + 1;
}else{
R = m - 1;
}
}
auto res = f(ans);
return res.ff - k * ans;
}
/*
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
} */
Compilation message (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... |