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 "aliens.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define X first
#define Y second
const int maxn = 1e3 + 5;
struct line {
ll m,c,val;
line(ll _m = 0, ll _c = 0, ll _val = 0) {
m = _m; c = _c; val = _val;
}
};
int n,m,k,sz;
pii p[maxn];
pii dp[maxn];
line cht[maxn];
ll getpow(ll x) {
return x*x;
}
double cut(int x, int y) {
if(x==0 || y==0) return -2e16;
return (double)(cht[y].c-cht[x].c)/(cht[x].m-cht[y].m);
}
void solve(ll cost) {
int len = 0;
// printf("solve %lld\n",cost);
dp[sz] = {0,0};
for(int x=sz-1;x>=0;x--) {
//update
line t = line(-2*p[x].Y, dp[x+1].X - (x+1<sz ? getpow(max(0LL,p[x].Y-p[x+1].X+1)):0) + p[x].Y*p[x].Y, dp[x+1].Y+1);
cht[++len] = t;
// while(len>=3) {
// if(cut(len-1,len)>=cut(len-2,len)) cht[len-1] = cht[len], len--;
// else break;
// }
// for(int i=1;i<=len;i++) printf("{%lld, %lld} ",cht[i].m,cht[i].c);
// printf("\n");
//query
// int l = 2, r = len, mid, pos = 1;
// while(l<=r) {
// mid = (l+r)/2;
// if(cut(mid-1,mid)<=p[x].X-1) {
// pos = mid;
// l = mid+1;
// }
// else r = mid-1;
// }
int pos = 1;
for(int i=1;i<=len;i++) {
if(cht[i].m*(p[x].X-1)+cht[i].c < cht[pos].m*(p[x].X-1)+cht[pos].c) pos = i;
}
dp[x] = {cht[pos].m * (p[x].X-1) + cht[pos].c + (p[x].X-1)*(p[x].X-1) + cost, cht[pos].val};
// printf("dp %d = {%lld, %lld}\n",x,dp[x].X,dp[x].Y);
}
}
bool cmp(pii x, pii y) {
if(x.X!=y.X) return x.X<y.X;
return x.Y>y.Y;
}
long long take_photos(int N, int M, int K, std::vector<int> R, std::vector<int> C) {
n = N; m = M; k = K;
//prep
for(int i=0;i<n;i++) {
p[i] = {R[i],C[i]};
if(p[i].X>p[i].Y) swap(p[i].X,p[i].Y);
}
sort(p,p+n,cmp);
ll cur = -1;
for(int i=0;i<n;i++) {
if(p[i].Y<=cur) continue;
p[sz++] = p[i];
cur = p[i].Y;
}
//solve
ll l = 0, r = 1e13, mid;
ll ans;
while(l<r) {
mid = (l+r)/2;
solve(mid);
if(dp[0].Y<=k) r = mid;
else l = mid+1;
}
solve(l);
return dp[0].X - l*dp[0].Y;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:87:5: warning: unused variable 'ans' [-Wunused-variable]
ll ans;
^
# | 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... |