# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
976635 | leanchec | Aliens (IOI16_aliens) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "aliens.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> dp;
vector<pair<int,int>> aux;
ll sq(ll a){return a*a;}
void solve(int qtd,int l, int r, int p, int q){
int mid=(l+r)>>1, opt=-1;
for(int i=p; i<=min(q, mid-1); i++){
if(dp[qtd][mid]>dp[qtd-1][i]+sq(aux[mid].second-aux[i+1].first+1)-sq(aux[i].second-aux[i+1].first+1)*(aux[i].second>=aux[i+1].first)){
dp[qtd][mid]=dp[qtd-1][i]+sq(aux[mid].second-aux[i+1].first+1)-sq(aux[i].second-aux[i+1].first+1)*(aux[i].second>=aux[i+1].first);
opt=i;
}
}
if(l<mid)solve(qtd, l, mid-1, p, opt);
if(mid<r)solve(qtd, mid+1, r, opt, q);
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
aux.clear();
dp.resize(k+1);
for(int i=i; i<=k; i++)
dp.resize(n+1);
for(int i=0; i<n; i++){
aux.push_back({min(r[i], c[i]), -max(r[i], c[i])});
}
sort(aux.begin(), aux.end());
vector<pair<int,int>> temp;
temp.push_back({-1, -1});
for(auto cur:aux){
if(-cur.second>temp.back().second)temp.push_back({cur.first, -cur.second});
}
aux=temp;
for(int i=1; i<=k; i++){
for(int j=1; j<=aux.size(); j++){
dp[i][j]=1e18;
}
}
for(int i=1; i<aux.size(); i++){
dp[1][i]=sq(aux[i].second-aux[1].first+1);
}
for(int i=2; i<=min((int)aux.size()-1, k); i++)
solve(i, 1, aux.size()-1, 1, aux.size()-1);
return dp[min((int)aux.size()-1, k)][aux.size()-1];
}