This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "aliens.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 7;
const int MAXN = 1e5 + 5;
const double eps = 1e-9;
pii a[MAXN];
struct Line {
ll m, n;
int cnt;
};
struct Hull {
vector<Line> lines;
int pointer = 0;
bool sect(int i, int j, int k) {
ll s1 = (lines[j].n - lines[i].n) * (lines[i].m - lines[k].m);
ll s2 = (lines[k].n - lines[i].n) * (lines[i].m - lines[j].m);
if (s1 == s2)
return lines[j].cnt > lines[k].cnt;
return s1 > s2;
}
void add(ll m, ll n, int cnt) {
if (!lines.empty() && lines.back().m == m)
return;
lines.pb({m, n, cnt});
while (lines.size() > 2 && sect(lines.size() - 3, lines.size() - 2, lines.size() - 1))
lines.erase(lines.end() - 2);
}
pair<ll,int> qry(ll x) {
while (pointer < lines.size() - 1) {
ll fl = (lines[pointer+1].m*x+lines[pointer+1].n) - (lines[pointer].m*x+lines[pointer].n);
if (fl < 0)
pointer++;
else if (fl == 0 && lines[pointer+1].cnt < lines[pointer].cnt)
pointer++;
else
break;
}
return {lines[pointer].m*x+lines[pointer].n, lines[pointer].cnt};
}
};
pair<ll,int> solve(ll c, int n) {
Hull h;
pair<ll,int> cur = {0, 0};
for (int i = 1; i <= n; i++) {
ll v1 = a[i].fi, v2 = 0;
if (i > 1)
v2 = max(0, a[i - 1].se - a[i].fi + 1);
h.add(-2 * v1, cur.fi - v2 * v2 + v1 * v1, cur.se);
v1 = a[i].se + 1;
cur = h.qry(v1);
cur.fi += v1 * v1 + c;
cur.se++;
}
return cur;
}
ll take_photos(int n, int m, int k, vi r, vi c) {
vii tmp;
for (int i = 0; i < n; i++)
tmp.pb({min(r[i], c[i]), max(r[i], c[i])});
sort(tmp.begin(), tmp.end(), [](pii lhs, pii rhs) -> bool {
if (lhs.fi == rhs.fi)
return lhs.se > rhs.se;
return lhs.fi < rhs.fi;
});
int nn = 0;
a[0] = {-1, -1};
for (int i = 0; i < n; i++)
if (tmp[i].se > a[nn].se)
a[++nn] = tmp[i];
ll lo = 0, hi = 1ll * (ll) m * (ll) m, ans = -1;
while (lo <= hi) {
ll mi = (lo + hi) / 2;
pair<ll,int> cur = solve(mi, nn);
if (cur.se <= k)
hi = mi - 1, ans = cur.fi;
else
lo = mi + 1;
}
return ans - lo * 1ll * (ll) k;
}
Compilation message (stderr)
aliens.cpp: In member function 'std::pair<long long int, int> Hull::qry(ll)':
aliens.cpp:59:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (pointer < lines.size() - 1) {
~~~~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |