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 <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
const int mod = 1e9 + 7;
const ld inf = 1e18;
const ld EPS = 1e-15;
const int N = 1e5 + 15;
pair <ld, int> dp[N];
int l[N], r[N];
int ord[N];
vector <pii> tmp;
ll val[N];
inline ll sq(ll x) {
return x * x;
}
inline void Min(pair <ld, int> &a, pair <ld, int> b) {
if(abs(a.f - b.f) < EPS)
a.se = min(a.se, b.se);
else if(a.f - b.f > EPS)
a = b;
}
struct line {
ll k;
ld b;
int t;
line(ll k, ld b, int t) : k(k), b(b), t(t) { }
inline pair <ld, int> get(int x) {
return mp(k * x + b, t + 1);
}
};
vector <line> hull;
int ptr;
inline bool bad(line a, line b, line c) {
return (a.b - c.b) * (c.k - b.k) >= (c.k - a.k) * (b.b - c.b);
}
inline void add(line nw) {
while(hull.size() > 1 && bad(hull[hull.size() - 2], hull.back(), nw))
hull.ppb();
hull.pb(nw);
}
inline pair <ld, int> get(int x) {
ptr = min(ptr, (int)hull.size() - 1);
while(ptr + 1 < hull.size() && hull[ptr].get(x) >= hull[ptr + 1].get(x))
++ptr;
return hull[ptr].get(x);
}
inline int check(ld C, int n) {
hull.clear();
ptr = 0;
for(int i = 1; i < N; ++i)
dp[i] = mp(inf, 0);
dp[0] = mp(0, 0);
for(int i = 1; i <= n; ++i) {
add(*new line(-2 * l[i], sq(l[i]) + dp[i-1].f - val[i] + C, dp[i-1].se));
pair <ld, int> valnw = get(r[i] + 1);
valnw.f += sq(r[i] + 1);
Min(dp[i], valnw);
}
return dp[n].se;
}
ll take_photos(int n, int m, int k, std::vector<int> row, std::vector<int> col) {
for(int i = 0; i < n; ++i) {
l[i+1] = min(row[i], col[i]) + 1;
r[i+1] = max(row[i], col[i]) + 1;
ord[i+1] = i+1;
}
sort(ord + 1, ord + 1 + n, [&](int x, int y) {
return mp(l[x], -r[x]) < mp(l[y], -r[y]);
});
for(int i = 1; i <= n; ++i)
if(tmp.empty() || !(tmp.back().f <= l[ord[i]] && tmp.back().se >= r[ord[i]]))
tmp.pb(mp(l[ord[i]], r[ord[i]]));
n = tmp.size();
for(int i = 1; i <= n; ++i) {
l[i] = tmp[i-1].f, r[i] = tmp[i-1].se;
val[i] = sq(max(0, r[i-1] - l[i] + 1));
}
ld l = 0, r = (ll)m * m, res = 0;
for(int iter = 0; iter < 50; ++iter) {
ld mid = (l + r) / 2;
int val = check(mid, n);
if(val == k) {
res = mid;
break;
}
if(val <= k) {
r = mid;
res = mid;
}
else
l = mid;
}
check(res, n);
return round(dp[n].f - res * dp[n].se);
}
Compilation message (stderr)
aliens.cpp: In function 'std::pair<long double, int> get(int)':
aliens.cpp:85:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr + 1 < hull.size() && hull[ptr].get(x) >= hull[ptr + 1].get(x))
~~~~~~~~^~~~~~~~~~~~~
# | 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... |