This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "aliens.h"
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1e5+5;
const ll inf=1e18;
vector<pair<ll,ll>> dp(mxn);
vector<int> x,y;
ll c(int l,int r){
if(l>r) return 0;
ll a=(y[r]-x[l]+1);
ll b=(l==1?0:max(0,y[l-1]-x[l]+1));
return a*a-b*b;
}
int search(int i,int j,int l,int r){
if(l==r) return l;
else{
int mid=(l+r+1)/2;
if(make_pair(dp[i].f+c(i+1,mid),dp[i].s)<make_pair(dp[j].f+c(j+1,mid),dp[j].s)){
return search(i,j,mid,r);
}
else{
return search(i,j,l,mid-1);
}
}
}
ll check(int n,ll cost){
deque<pair<int,int>> dq;
dq.push_back({0,n+1});
dp[0]={0,0};
for(int i=1;i<=n;i++){
while(dq.front().s<i) dq.pop_front();
int j=dq.front().f;
dp[i].f=dp[j].f+c(j+1,i)+cost;
dp[i].s=dp[j].s+1;
while(dq.size()>1 and make_pair(search(j,i,i,n),-dp[j].s)<=make_pair(dq[dq.size()-2].s,-dp[dq[dq.size()-2].f].s)){
dq.pop_back();
j=dq.back().f;
}
dq.back().s=search(j,i,i,n);
dq.push_back({i,n+1});
}
return dp[n].s;
}
ll solve(int n,int m,int k){
k=min(k,n);
ll l=0,r=(ll)1e12+1;
while(l<r){
ll mid=(l+r)/2;
if(check(n,mid)<=k){
r=mid;
}
else{
l=mid+1;
}
}
check(n,l);
return dp[n].f-k*l;
}
ll take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c){
vector<pair<int,int>> vec(n);
for(int i=0;i<n;i++){
if(r[i]>c[i]) swap(r[i],c[i]);
vec[i]={c[i],r[i]};
}
sort(all(vec));
vector<pair<int,int>> st;
for(int i=0;i<n;i++){
while(!st.empty() and st.back().s>=vec[i].s){
st.pop_back();
}
st.push_back(vec[i]);
}
x.push_back(-1);
y.push_back(-1);
for(auto v:st){
x.push_back(v.s);
y.push_back(v.f);
}
return solve(st.size(),m,k);
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
aliens.cpp: In function 'void setIO(std::string)':
aliens.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aliens.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |