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("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define xx int
//#define endl '\n'
#include "aliens.h"
int mi[1000001];
llo dp[100001];
llo cnt[100001];
pair<llo,llo> sect(pair<llo,llo> aa,pair<llo,llo> bb){
pair<llo,llo> cc={bb.b-aa.b,aa.a-bb.a};
if(cc.b<0){
cc.b=-cc.b;
cc.a=-cc.a;
}
return cc;
}
llo eval(pair<llo,llo> aa,llo bb){
return aa.a*bb+aa.b;
}
vector<pair<llo,llo>> ss;
llo kk;
pair<llo,llo> check(llo mid){
vector<pair<llo,llo>> ee;
llo ind=0;
map<llo,llo> xz;
for(llo j=0;j<ss.size();j++){
dp[j]=(ss[j].b-ss[0].a+1);
dp[j]*=dp[j];
cnt[j]=1;
/*if(i==1){
continue;
}*/
if(ee.size()){
while(ind+1<ee.size()){
if(eval(ee[ind],ss[j].b)>=eval(ee[ind+1],ss[j].b)){
ind++;
}
else{
break;
}
}
llo ac=eval(ee[ind],ss[j].b)+ss[j].b*ss[j].b;
if(ac<dp[j]){
dp[j]=ac;
cnt[j]=1+cnt[xz[ee[ind].a]];
}
}
/* if(mid==1023 and j==2){
cout<<ind<<",,"<<endl;
for(auto j:ee){
cout<<j.a<<"::"<<j.b<<endl;
}
}*/
dp[j]+=mid;
if(j+1<ss.size()){
llo a2=max(ss[j].b+1-ss[j+1].a,(llo)0);
a2*=a2;
a2=-a2;
a2+=(ss[j+1].a-1)*(ss[j+1].a-1);
a2+=dp[j];
llo mm=-2*(ss[j+1].a-1);
pair<llo,llo> ne={mm,a2};
while(ee.size()>=2){
pair<llo,llo> ac=sect(ee.back(),ee[ee.size()-2]);
pair<llo,llo> bc=sect(ee[ee.size()-2],ne);
//bc<ac
if(bc.a*ac.b<bc.b*ac.a){
ee.pop_back();
}
else{
break;
}
}
ee.pb({mm,a2});
xz[ne.a]=j;
}
}
/* cout<<mid<<"::";
for(int i=0;i<ss.size();i++){
cout<<dp[i]<<",";
}
cout<<endl;*/
return {cnt[ss.size()-1],dp[ss.size()-1]-mid*kk};
}
long long take_photos(xx n, xx m, xx k,vector<xx> aa,vector<xx> bb) {
ss.clear();
for(llo i=0;i<m;i++){
mi[i]=-1;
}
for(llo i=0;i<n;i++){
mi[min(aa[i],bb[i])]=max(mi[min(aa[i],bb[i])],max(aa[i],bb[i]));
}
llo cur=-1;
for(llo i=0;i<m;i++){
if(mi[i]!=-1){
if(mi[i]>cur){
ss.pb({i,mi[i]});
cur=mi[i];
}
}
}
/* for(auto j:ss){
cout<<j.a<<"::"<<j.b<<endl;
}*/
k=min(k,(int)ss.size());
kk=k;
/*for(int i=0;i<ss.size();i++){
for(int j=1;j<=k+1;j++){
dp[i].pb(0);
}
}*/
llo low=0;
for(llo i=39;i>=0;i--){
/*if(i>0){
if((1LL<<(i-1))>m*m){
continue;
}
}*/
//if(low-(1LL<<i)>=0){
if(check(low+(1LL<<i)).a>=k){
low+=(1LL<<i);
}
//}
}
// assert(check(low).a==k);
// cout<<low<<"::"<<endl;
// cout<<check(low).a<<":"<<check(low).b<<endl;
//cout<<check(low).a<<"::"<<endl;
return check(low).b;
/* for(llo i=1;i<=k;i++){
vector<pair<llo,llo>> ee;
llo ind=0;
for(llo j=0;j<ss.size();j++){
dp[j][i]=(ss[j].b-ss[0].a+1);
dp[j][i]*=dp[j][i];
if(i==1){
continue;
}
if(ee.size()){
while(ind+1<ee.size()){
if(eval(ee[ind],ss[j].b)>=eval(ee[ind+1],ss[j].b)){
ind++;
}
else{
break;
}
}
dp[j][i]=min(dp[j][i],eval(ee[ind],ss[j].b)+ss[j].b*ss[j].b);
}
if(j+1<ss.size()){
llo a2=max(ss[j].b+1-ss[j+1].a,(llo)0);
a2*=a2;
a2=-a2;
a2+=(ss[j+1].a-1)*(ss[j+1].a-1);
a2+=dp[j][i-1];
llo mm=-2*(ss[j+1].a-1);
pair<llo,llo> ne={mm,a2};
while(ee.size()>=2){
pair<llo,llo> ac=sect(ee.back(),ee[ee.size()-2]);
pair<llo,llo> bc=sect(ee[ee.size()-2],ne);
//bc<ac
if(bc.a*ac.b<bc.b*ac.a){
ee.pop_back();
}
else{
break;
}
}
ee.pb({mm,a2});
}
}
}*/
/* for(llo i=1;i<=k;i++){
for(llo j=0;j<ss.size();j++){
cout<<dp[j][i]<<",";
}
cout<<endl;
}*/
/*cout<<dp[0][1]<<"::"<<dp[1][1]<<endl;
for(auto j:ss){
cout<<j.a<<":"<<j.b<<endl;
}
cout<<endl;
*/
// return dp[ss.size()-1][k];
}
Compilation message (stderr)
aliens.cpp: In function 'std::pair<long long int, long long int> check(llo)':
aliens.cpp:35:20: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(llo j=0;j<ss.size();j++){
| ~^~~~~~~~~~
aliens.cpp:43:24: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | while(ind+1<ee.size()){
| ~~~~~^~~~~~~~~~
aliens.cpp:64:16: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | if(j+1<ss.size()){
| ~~~^~~~~~~~~~
# | 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... |