#include "aliens.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF=1e17;
const ll MAXN=1e5+6;
struct line
{
ll a,b;
ll c;
ll eval(ll x)
{
return x*a+b;
}
ll operator+(const line& other)
{
if(a==other.a)assert(false);
return (other.b-b)/(a-other.a);
}
};
struct CHT
{
deque<line>dq;
bool bad(line ezdp)
{
if(dq.size()==1)return 0;
line l1=dq.end()[-2];
line l2=dq.end()[-1];
return l1+ezdp<l1+l2;
}
void push(line ezdp)
{
while(dq.size()>1&&bad(ezdp))
{
dq.pop_back();
}
dq.push_back(ezdp);
}
pair<ll,ll> query(ll x)
{
while(dq.size()>1&&dq[0].eval(x)>dq[1].eval(x))
{
dq.pop_front();
}
return {dq[0].eval(x),dq[0].c};
}
void init()
{
dq.clear();
}
}t;
struct point
{
ll x,y;
bool operator<(point const& other)const
{
return make_pair(x,y)<make_pair(other.x,other.y);
}
};
vector<point>v;
vector<point>aux;
pair<ll,ll> dp[MAXN];
pair<ll,ll> calc(vector<point>v,ll pen)
{
t.init();
for(int i=0;i<MAXN;i++)
{
dp[i]={INF,INF};
}
/*
const - x[i]*x[i]+pen+1+2*x[i]
a - -2*y[j+1]
b - max(x[j]-y[j+1]+1,0)*max(x[j]-y[j+1]+1,0)+dp[j]+y[j+1]*y[j+1]-2*y[j+1]
c - dp[j].second
*/
dp[0]={(v[0].x-v[0].y+1)*(v[0].x-v[0].y+1),1};
t.push({-2*v[1].y,max(v[0].x-v[1].y+1,0ll)*max(v[0].x-v[1].y+1,0ll)+dp[0].first+v[1].y*v[1].y-2*v[1].y,1});
for(int i=1;i<v.size();i++)
{
pair<ll,ll> gg=t.query(v[i].x);
dp[i]={v[i].x*v[i].x+pen+1+2*v[i].x+gg.first,gg.second+1};
if(i+1<v.size())t.push({-2*v[i+1].y,max(v[i].x-v[i+1].y+1,0ll)*max(v[i].x-v[i+1].y+1,0ll)+dp[i].first+v[i+1].y*v[i+1].y-2*v[i+1].y,dp[i].second});
}
return dp[v.size()-1];
}
long long take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
for(int i=0;i<r.size();i++)
{
if(r[i]<c[i])swap(r[i],c[i]);
aux.push_back({r[i],c[i]});
}
sort(aux.begin(),aux.end());
for(auto xd:aux)
{
while(v.size()>0&&v.back().y>=xd.y)
{
v.pop_back();
}
v.push_back(xd);
}
ll low=0,high=INF,mid,ans;
while(low<=high)
{
mid=(low+high)/2;
ll t=calc(v,mid).second;
if(t<=k)
{
ans=mid;
high=mid-1;
}
else
{
low=mid+1;
}
}
pair<ll,ll> f=calc(v,ans);
return f.first-k*ans;
return 0;
}
Compilation message
aliens.cpp: In function 'std::pair<long long int, long long int> calc(std::vector<point>, long long int)':
aliens.cpp:78:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i=1;i<v.size();i++)
| ~^~~~~~~~~
aliens.cpp:82:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | if(i+1<v.size())t.push({-2*v[i+1].y,max(v[i].x-v[i+1].y+1,0ll)*max(v[i].x-v[i+1].y+1,0ll)+dp[i].first+v[i+1].y*v[i+1].y-2*v[i+1].y,dp[i].second});
| ~~~^~~~~~~~~
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:87:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int i=0;i<r.size();i++)
| ~^~~~~~~~~
aliens.cpp:117:18: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
117 | return f.first-k*ans;
| ~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
2 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
3 |
Incorrect |
7 ms |
1920 KB |
Wrong answer: output = 6, expected = 4 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 1 |
2 |
Incorrect |
7 ms |
1900 KB |
Wrong answer: output = 2, expected = 4 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
2 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
3 |
Incorrect |
7 ms |
1920 KB |
Wrong answer: output = 6, expected = 4 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
2 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
3 |
Incorrect |
7 ms |
1920 KB |
Wrong answer: output = 6, expected = 4 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
2 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
3 |
Incorrect |
7 ms |
1920 KB |
Wrong answer: output = 6, expected = 4 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
2 |
Correct |
7 ms |
1900 KB |
Correct answer: answer = 4 |
3 |
Incorrect |
7 ms |
1920 KB |
Wrong answer: output = 6, expected = 4 |
4 |
Halted |
0 ms |
0 KB |
- |