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"
using namespace std;
using ll = long long;
void remove_redundancies(vector<pair<ll, ll>> & v) {
vector<pair<ll, ll>> tmp1;
int last = -1;
for(auto [x, y] : v) {
if(x == last) tmp1.pop_back();
last = x;
tmp1.push_back({x, y});
}
int n = tmp1.size();
vector<bool> redundant(n, false);
vector<ll> right_point(n, 0);
vector<ll> left_point(n, 0);
for(int i = 0; i < n; i++) {
right_point[i] = tmp1[i].first + tmp1[i].second;
left_point[i] = tmp1[i].first - tmp1[i].second;
}
for(int i = 1; i < n; i++) {
if(right_point[i - 1] >= right_point[i]) {
redundant[i] = true;
right_point[i] = right_point[i - 1];
}
}
for(int i = n - 2; i >= 0; i--) {
if(left_point[i + 1] <= left_point[i]) {
redundant[i] = true;
left_point[i] = left_point[i + 1];
}
}
v.clear();
for(int i = 0; i < n; i++) {
if(!redundant[i]) {
ll x = (tmp1[i].first + tmp1[i].second) / 2;
ll y = tmp1[i].first - x;
v.push_back({y, x});
}
}
}
ll cost(pair<ll, ll> a) {
return (a.second - a.first + 1) * (a.second - a.first + 1);
}
ll pair_cost(pair<ll, ll> a, pair<ll, ll> b) {
if(a.second < b.first) return cost(a) + cost(b);
else return cost(a) + cost(b) - cost({b.first, a.second});
}
ll merge_cost(pair<ll, ll> a, pair<ll, ll> b) {
return cost({a.first, b.second}) - pair_cost(a, b);
}
void reduce(vector<pair<ll, ll>> & v) {
vector<pair<ll, ll>> tmp;
pair<ll, int> best = {1e18, -1};
int n = v.size();
for(int i = 0; i < n - 1; i++) {
best = min(best, {merge_cost(v[i], v[i + 1]), i});
}
int idx = best.second;
for(int i = 0; i < idx; i++) {
tmp.push_back(v[i]);
}
tmp.push_back({v[idx].first, v[idx + 1].second});
for(int i = idx + 2; i < n; i++) tmp.push_back(v[i]);
swap(v, tmp);
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
if(n > 50) return 0;
vector<pair<ll, ll>> coords(n);
for(int i = 0; i < n; i++) coords[i] = {r[i] + c[i], abs(r[i] - c[i])};
sort(coords.begin(), coords.end());
remove_redundancies(coords);
//for(auto [x, y] : coords) cout << x << " " << y << endl;
while(coords.size() > k) {
reduce(coords);
}
ll ans = cost(coords[0]);
for(int i = 0; i < coords.size() - 1; i++) {
ans += pair_cost(coords[i], coords[i + 1]) - cost(coords[i]);
}
return ans;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:74:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
74 | while(coords.size() > k) {
| ~~~~~~~~~~~~~~^~~
aliens.cpp:78:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i < coords.size() - 1; 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... |