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>
using namespace std;
long long take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
vector<pair<int,int>> a;
for (int i = 0; i < n; i++) {
if (r[i] < c[i]) swap(r[i], c[i]);
a.emplace_back(r[i], c[i]);
}
sort(a.begin(), a.end());
vector<pair<int,int>> b;
for (int i = 0; i < n; i++) {
while (!b.empty() && b.back().second >= a[i].second) b.pop_back();
b.push_back(a[i]);
}
swap(a, b);
n = (int) a.size();
auto test = [&](long long c) {
const long long inf = 1e18;
vector<pair<long long, int>> dp(n + 1, make_pair(inf, 0));
dp[0] = {0, 0};
for (int j = 1; j <= n; j++) {
for (int from = j; from > 0; from--) {
dp[j] = min(dp[j], {dp[from - 1].first + 1ll * (a[j - 1].first - a[from - 1].second + 1) * (a[j - 1].first - a[from - 1].second + 1) + c, dp[from - 1].second + 1});
}
}
return dp[n];
};
long long l = 0, R = 1ll * m * m, ans = -1;
while (l <= R) {
long long mid = l + R >> 1;
if (test(mid).second <= k) {
ans = mid;
R = mid - 1;
}
else l = mid + 1;
}
assert(ans !=- 1);
return test(ans).first - ans * k;
}
/*
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << take_photos(5, 7, 2, {0, 4, 4, 4, 4}, {3, 4, 6, 5, 6});
}*/
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:32:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | long long mid = l + R >> 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... |