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 <bits/stdc++.h>
#include "aliens.h"
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))
using namespace std;
const ll LINF = 2e18;
struct Line{
ll k, n;
Line(ll _k = 0, ll _n = LINF){ k = _k; n = _n; }
ll operator()(ll x) const { return k*x+n; }
};
struct T{
Line f;
ll cnt;
T(Line _f = Line(), ll _cnt = 0){ f = _f; cnt = _cnt; }
array<ll, 2> operator()(ll x) const { return {f(x), cnt}; }
};
class LiChao{
ll L, R;
vector<T> tree;
vector<array<int, 2> > ch;
int Node(){ tree.pb(T()); ch.pb({-1, -1}); return tree.size()-1; }
int Child(int s, int i){ if (!~ch[s][i]){ int u = Node(); ch[s][i] = u; } return ch[s][i]; }
void Insert(int s, int l, int r, T f){
int m = (l + r)>>1;
if (f(m) < tree[s](m)) swap(tree[s], f);
if (f(l) < tree[s](l)) Insert(Child(s, 0), l, m, f);
if (f(r) < tree[s](r)) Insert(Child(s, 1), m+1, r, f);
}
array<ll, 2> Get(int s, int l, int r, ll x) const {
if (!~s) return {LINF, LINF};
int m = (l + r)>>1;
if (x <= m) return min(tree[s](x), Get(ch[s][0], l, m, x));
else return min(tree[s](x), Get(ch[s][1], m+1, r, x));
}
public:
LiChao(ll l, ll r){ Assign(l, r); }
void Assign(ll l, ll r){
L = l; R = r;
tree.clear(); ch.clear();
Node();
}
void Insert(ll k, ll n, int cnt){ Insert(0, L, R, T(Line(k, n), cnt)); }
array<ll, 2> Get(ll x) const { return Get(0, L, R, x); }
};
const int mxN = 1e6+10, INF = 2e9;
ll n, m, k, dp[mxN], cnt[mxN];
array<int, 2> a[mxN];
ll sq(ll x){ return x*x; }
ll DP(ll penalty){
LiChao cht(0, m);
for (int i = 1; i <= n; i++){
cht.Insert(-2 * (a[i][0] - 1), dp[i-1] + sq(a[i][0] - 1) - sq(max(0, a[i-1][1] - a[i][0] + 1)), cnt[i-1]);
auto x = cht.Get(a[i][1]);
dp[i] = sq(a[i][1]) + x[0] + penalty;
cnt[i] = x[1] + 1;
}
if (cnt[n] > k) return -1;
return dp[n];
}
ll take_photos(int N, int M, int K, vector<int> R, vector<int> C){
n = N; m = M; k = K;
for (int i = 0; i < n; i++)
a[i] = {min(R[i], C[i]), max(R[i], C[i])};
sort(a, a+n);
vector<array<int, 2> > tmp;
for (int i = 0, mx = -1; i < n; i++){
if (a[i][1] > mx) tmp.pb(a[i]);
smax(mx, a[i][1]);
}
a[0] = {-1, -1};
for (int i = 0; i < tmp.size(); i++)
a[i+1] = tmp[i];
n = tmp.size();
ll l = 0, r = sq(m), ans = sq(m)+1;
while (l <= r){
ll p = (l + r + 1)>>1;
if (DP(p) >= 0){
r = p - 1;
ans = p;
}
else l = p + 1;
}
return DP(ans) - ans * k;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:113:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int i = 0; i < tmp.size(); i++)
| ~~^~~~~~~~~~~~
# | 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... |