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;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define RE(a,b) REP(a,0,b)
#define FOR(a,b) for(auto& a:b)
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int INF=1e9;
const int MX=5000;
vi r, c, le, ri;
int n, m, k;
int SA[MX];
ll dp[MX][MX];
ll getCost(int i, int j) {
ll w = ri[j]-le[i]+1;
ll res = w*w;
if(j != n-1) {
ll sw = ri[j]-le[j+1]+1;
if(sw > 0)
res -= sw*sw;
}
return res;
}
void computeDP(int i, int l, int r, int optl, int optr) {
if(r < l) return;
int m = (l+r)/2;
int opt = -1;
dp[m][i] = 1e18;
REP(j,optl,min(optr+1,m)) {
int ndp = dp[j][i-1] + getCost(j,m-1);
if(ndp < dp[m][i]) {
dp[m][i] = ndp;
opt = j;
}
}
computeDP(i, l, m-1, optl, opt);
computeDP(i, m+1, r, opt, optr);
}
ll take_photos(int _n, int _m, int _k, vi _r, vi _c) {
n=_n; m=_m; k=_k; r=_r; c=_c;
RE(i,n) if(c[i] < r[i]) swap(c[i], r[i]);
RE(i,n) SA[i] = i;
sort(SA,SA+n,[](int i, int j) {
if(r[i] == r[j]) return c[i] > c[j];
return r[i] < r[j];
});
ll mxr = -1;
RE(i,n) {
if(c[SA[i]] <= mxr) continue;
le.pb(r[SA[i]]);
ri.pb(c[SA[i]]);
mxr = c[SA[i]];
}
n = le.size();
RE(i,n+1) RE(j,n+1) dp[i][j] = 1e18;
if(k > n) k = n;
RE(i,n+1) dp[i][0] = 1e18;
RE(i,k+1) dp[0][i] = 0;
REP(i,1,k+1) computeDP(i,1,n,0,n-1);
return dp[n][k];
}
# | 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... |