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;
struct line {
long long m, c, z;
};
struct CHT {
long long la = -1 , ma = 0;
deque <line> lines;
CHT(){
la = -1;
ma = 0;
}
bool cmp(const pair <long long, long long> &x, const pair <long long,long long> &y) {
return (x.first) * (y.second) < (x.second) * (y.first);
}
bool equ(const pair <long long, long long> &x, const pair <long long,long long> &y) {
return (x.first) * (y.second) == (x.second) * (y.first);
}
void push(line x) {
if(lines.size() && lines.back().m == x.m) {
if(x.c == lines.back().c){
if(x.z<lines.back().z)return ;
else lines.pop_back();
}else{
if(x.c < lines.back().c) lines.pop_back();
else return;
}
}
while(lines.size() >= 2 && cmp({lines.back().c - x.c, x.m - lines.back().m}, {lines[lines.size() - 2].c - lines.back().c, lines.back().m - lines[lines.size() - 2].m})) lines.pop_back();
lines.push_back(x);
}
pair<long long,long long> query(long long x) {
if(x!=la){
la = x;
ma = 0;
}
while(lines.size() >= 2 && !(cmp({x,1},{lines[1].c - lines[0].c, lines[0].m - lines[1].m}))){
if(equ({x,1},{lines[1].c - lines[0].c, lines[0].m - lines[1].m})){
ma = max(ma,lines[0].z);
}
lines.pop_front();
}
return {lines[0].m * x + lines[0].c, max(ma,lines[0].z)};
}
};
long long take_photos(int n, int m, int k, vector<int> r,vector<int> c){
vector<pair<long long,long long>> seg1,seg;
for(int i = 0;i<n;i++){
seg1.push_back({min(c[i],r[i]),max(c[i],r[i])});
}
sort(seg1.begin(),seg1.end());
long long ma = -1e18;
for(int i = 0;i<n;i++){
if(seg1[i].second>ma){
seg.push_back(seg1[i]);
ma = seg1[i].second;
}
}
long long L = 0 , R = 1e12 , all = 0;
k = min(k,(int)seg.size());
while(L<=R){
long long mid = (L+R)/2;
pair<long long,long long> dp[seg.size()+1];
dp[0] = {0,0};
CHT cht;
for(int i = 1;i<=seg.size();i++){
int e = i-1;
line pu = {(-2)*seg[e].first,dp[e].first+seg[e].first*seg[e].first-2*seg[e].first+1-(e==0?0:max(0ll,seg[e-1].second-seg[e].first+1)*max(0ll,seg[e-1].second-seg[e].first+1)),dp[e].second};
cht.push(pu);
dp[i] = cht.query(seg[i-1].second);
dp[i].first+=seg[i-1].second*seg[i-1].second+2*seg[i-1].second+mid;
dp[i].second++;
}
if(dp[seg.size()].second>=k){
all = dp[seg.size()].first-(mid*k);
L = mid+1;
}else R = mid-1;
}
return all;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:68:24: 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]
68 | for(int i = 1;i<=seg.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... |