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;
using ll = long long;
ll INF = 1e18;
inline ll sq(ll x) {
return x * x;
}
struct Parabolas {
int ind = 0;
vector<ll> A, B;
void clear() {
ind = 0;
A.clear();
B.clear();
}
void insert(ll a, ll b) {
//~ cout << "ins"<<a << " "<< b << endl;
A.push_back(a);
B.push_back(b);
}
ll query(ll x) {
if(A.size() == 0)
return INF;
while(ind + 1 < A.size() && sq(x - A[ind + 1]) + B[ind + 1] <= sq(x - A[ind]) + B[ind])
ind++;
return sq(x - A[ind]) + B[ind];
}
};
int l, k;
int d[100007], x[100007];
int maxx[1000007];
Parabolas P;
ll dp[507][507]; // k, n
ll calc_dp() {
for(int i = 0 ; i < 507 ; i++)
for(int j = 0 ; j < 507 ; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for(int j = 1 ; j <= k ; j++) {
P.clear();
for(int i = 0 ; i <= l ; i++) {
if(i > 0)
dp[j][i] = P.query(x[i]);
//~ cout << i << " " << j << " " << dp[j][i] << endl;
if(i < l && dp[j - 1][i] != INF)
P.insert(d[i + 1] - 1, dp[j - 1][i] - sq(max(0, x[i] - d[i + 1] + 1)));
}
}
ll res = INF;
for(int i = 0 ; i <= k ; i++)
res = min(res, dp[i][l]);
return res;
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
::k = k;
for(int i = 0 ; i < n ; i++) {
if(c[i] >= r[i]) maxx[r[i] + 1] = max(maxx[r[i] + 1], c[i] + 1);
else maxx[c[i] + 1] = max(maxx[c[i] + 1], r[i] + 1);
}
for(int i = 1 ; i <= m ; i++) {
if(maxx[i] && (l == 0 || x[l] < maxx[i])) {
l++;
d[l] = i;
x[l] = maxx[i];
//~ cout << d[l] << " " << x[l] << endl;
}
}
return calc_dp();
}
Compilation message (stderr)
aliens.cpp: In member function 'll Parabolas::query(ll)':
aliens.cpp:34:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind + 1 < A.size() && sq(x - A[ind + 1]) + B[ind + 1] <= sq(x - A[ind]) + B[ind])
~~~~~~~~^~~~~~~~~~
# | 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... |