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 <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <utility>
#include "aliens.h"
#define N 100100
#define M 1001000
#define ll long long
#define ii std::pair<int,int>
ll over[N];
int line[M];
std::vector<ii> pt;
std::vector<std::vector<ll>> dp;
ll minl(ll a, ll b){
if(a == -1)return b;
if(b == -1)return a;
return a < b ? a:b;
};
void solvedc(int kl, int kr, int l, int r, int j){
if(kl >= kr)return;
if(l > kl)return;
if(l >= r)return;
ll infl;
if(l == r-1){
for(int i = kl;i<kr;i++){
if(over[i]){
infl = over[i] + (pt[i].first-pt[l].first);
}else{
infl = std::max(over[i-1],(ll)pt[i-1].second) + (pt[i-1].first-pt[l].first);
};
dp[i][j] = dp[l][j-1] + infl * infl - over[l] * over[l];
//printf("dc: %d %d %d %d %lld %d %d\n",j,i,l,r,dp[i][j],kl,kr);
};
}else{
int km = (kl+kr)>>1;
int bstId;
int rr = std::min(r,km);
ll bst = -1;
if(over[km]){
infl = over[km] + pt[km].first;
}else{
infl = std::max(over[km - 1],(ll)pt[km-1].second) + pt[km-1].first;
};
ll tmp;
for(int i = l;i<rr;i++){
tmp = infl - pt[i].first;
if(dp[i][j-1] != -1){
if(dp[i][j-1] + tmp * tmp - over[i] * over[i] <= bst || bst == -1){
bst = dp[i][j-1] + tmp * tmp - over[i] * over[i];
bstId = i;
};
};
};
//printf("dc: %d %d %d %d %lld %d %d %d\n",j,km,l,r,bst,kl,kr,bstId);
dp[km][j] = bst;
solvedc(kl,km,l,bstId+1,j);
solvedc(km+1,kr,bstId,r,j);
};
};
ll take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c){
memset(over,0,sizeof(over));
memset(line,0,sizeof(line));
pt.clear();
dp = std::vector<std::vector<ll>>(n+10,std::vector<ll>(k+10,-1));
for(int i = 0;i<n;i++){
if(r[i] >= c[i]){
line[c[i]] = std::max(line[c[i]],r[i]-c[i]+1);
}else if(r[i] < c[i]){
line[r[i]] = std::max(line[r[i]],c[i]-r[i]+1);
};
};
for(int i = 0;i<m;i++){
if(line[i])pt.push_back(ii(i,line[i]));
};
for(int i = 1;i<pt.size();i++){
if(over[i-1] > pt[i-1].second){
over[i] = over[i-1] + pt[i-1].first - pt[i].first;
}else{
over[i] = pt[i-1].second + pt[i-1].first - pt[i].first;
};
if(over[i] < 0)over[i] = 0;
//printf("pt: %d %d %d\n",pt[i].first,pt[i].second,over[i]);
};
over[pt.size()] = 0;
pt.push_back({0,0});
//printf("%d %d %d %d\n",pt.size(),pt[0].first,pt[0].second,over[0]);
for(int i = 0;i<=k;i++)dp[0][i] = 0;
for(int j = 1;j<=k;j++){
solvedc(1,pt.size(),0,pt.size()-1,j);
};
ll mn = -1;
for(int i = 1;i<=k;i++){
//printf("%lld\n",dp[pt.size()-1][i]);
mn = minl(mn,dp[pt.size()-1][i]);
};
return mn;
};
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:85:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1;i<pt.size();i++){
~^~~~~~~~~~
aliens.cpp: In function 'void solvedc(int, int, int, int, int)':
aliens.cpp:62:12: warning: 'bstId' may be used uninitialized in this function [-Wmaybe-uninitialized]
solvedc(kl,km,l,bstId+1,j);
~~~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |