/* 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;
ll eval(ll x){
return x*m + b;
}
};
double intersection(P x, P y){
return (x.b - y.b) / (y.m - x.m);
}
struct CH{
vector<P> C;
void insert(ll c, ll m, int idx){
P x = {m, c, idx};
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);
}
P get(ll fi){
if(C.size() == 1) return C[0];
P RES = C[0];
int l = 1, r = int(C.size()) - 1;
while(l <= r){
int m = l+r>>1;
double pos = intersection(C[m-1], C[m]);
if(fi <= pos){
if(RES.eval(fi) > C[m-1].eval(fi)) RES = C[m-1];
r = m - 1;
}else{
if(RES.eval(fi) > C[m].eval(fi)) RES = C[m];
l = m + 1;
}
}
for(int j = max(l-4, 0); j < min(int(C.size()), r+4);++j){
if(RES.eval(fi) > C[j].eval(fi)) RES = C[j];
}
return RES;
}
};
int n, m, k;
array<ll, 2> a[N];
ll take_photos(int nn, int mm, int kk, vector<int> r, vi c){
n=nn,m=mm,k=kk;
vector<CH> cht(k + 2);
vector<ll> go(2*n+5);
vector<vector<ll>> dp(n *2 + 15, vector<ll>(k + 5));
vector<ll> val;
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(a[i][1]);
//val.pb(a[i][0] + 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;
for(int j = 0; j <= k; ++j){
for(int i = 0; i <= m; ++i) dp[i][j] = INF;
}
cht[0].insert(go[0]*go[0], -2*go[0], 0);
ll ans = INF;
for(int j = 1; j <= k; ++j){
for(ll i = 0; i <= m; ++i){
auto p = cht[j-1].get(val[i]);
dp[i][j] = p.eval(val[i]) + val[i] * val[i];
ll best = go[p.idx];
//cout << i << ' ' << j << ' ' << dp[i][j] << ' ' << best << ' ';
if(go[i] == MOD) ans=min(ans,dp[i][j]);
best = max(best, (go[i]));
if(j < k && go[i] < val[i]){
dp[i][j] -= (best - val[i]) * (best - val[i]);
dp[i][j] += go[i] * go[i];
cht[j].insert(dp[i][j], -2*go[i], i);
}else if(j < k){
dp[i][j] += go[i] * go[i];
cht[j].insert(dp[i][j], -2*go[i], i);
}
//cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
}
}
for(int i = 0; i <= m; ++i) if(go[i] == MOD) ans = min(ans, dp[i][k]);
//cout << ans;
return 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... |