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 <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double f;
typedef pair<ll,ll> pll;
typedef pair<f,ll> pfl;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<pfl> vp;
typedef vector<int> vi;
#define rep(a,b,c) for(ll a = b; a < c; a++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define sz(a) a.size()
pfl operator+ (pfl a, pfl b) {return mp(a.first+b.first,a.second+b.second);}
pfl _dp(f lambda);
ll n;
ll k;
vpll p;
vll h;
vp dp;
long long take_photos(int N, int M, int K, vi R, vi C) {
k = K;
p.clear();
rep(i,0,N) {if (R[i] > C[i]) {swap(R[i],C[i]);}} // Flip over diagonal
rep(i,0,N) {p.pb(mp(C[i],-R[i]));}
sort(all(p)); // Sort in a way that makes worse points come before better points
rep(i,0,N) {
C[i] = p[i].first;
R[i] = -p[i].second;
}
/*cout << "C[i] R[i]" << endl;
rep(i,0,N) {cout << C[i] << " " << R[i] << endl;}*/
// C[i] is in increasing order
// R[i] is in decreasing order
// The first point is the optimal
p.clear();
p.pb(mp(-1,-1)); // thing....
rep(i,0,N) {
while ((C[i] >= p[sz(p)-1].first) && (R[i] <= p[sz(p)-1].second)) { // Prune bad points
p.pop_back();
}
p.pb(mp(C[i],R[i])); // Add points in (x,-y) format to p
}
/*cout << "points:" << endl;
rep(i,0,sz(p)) {cout << p[i].first << " " << p[i].second << endl;}*/
n = sz(p);
h.resize(n,0);
rep(i,0,n-1) {h[i] = p[i].first - p[i+1].second;}
/*cout << "h:" << endl;
rep(i,0,n) {cout << h[i] << " ";}
cout << endl;*/
/*rep(i,0,10) {cout << _dp(f(i)).first << endl;}*/
ll ans = 1000000000000;
f lo = f(0.0);
f hi = f(10000000001);
f mid;
pfl curr;
rep(i,0,1024) {
mid = (lo)/f(2) + (hi)/f(2);
curr = _dp(mid);
//cout << curr.first << " " << curr.second << " " << mid << endl;
if (curr.second == min(k,n-1)) {
ans = round(curr.first - mid*(f(curr.second)));
break;
} else if (curr.second > min(k,n-1)) {
lo = mid;
} else {
hi = mid;
/*ll tmp = round(curr.first - mid*(f(curr.second))); // Hotfix
ans = min(tmp, ans);*/
}
}
//if (ans==-1) {ans = round(curr.first - mid*(f(curr.second)));}
//cout << n-1 << endl;
return ans;
}
pfl _dp(f lambda) {
dp.clear();
dp.resize(n,mp(f(10000000000000000),0));
//dp[0] = mp(f( (p[0].first-p[0].second+1)*(p[0].first-p[0].second+1) ),1);
dp[0] = mp(f(0),0);
rep(i,0,n) {
rep(j,0,i) {
ll x_i = p[i].first;
ll x_j = p[j].first;
if (h[j] < 0) {
dp[i] = min(dp[i], dp[j] + mp(f( (x_i-x_j+h[j]+1)*(x_i-x_j+h[j]+1) ),1) + mp(lambda,0));
} else {
dp[i] = min(dp[i], dp[j] + mp(f( (x_i-x_j+h[j]+1)*(x_i-x_j+h[j]+1) - (h[j]+1)*(h[j]+1) ),1) + mp(lambda,0));
}
}
}
/*cout << "dp: ";
rep(i,0,n) {cout << dp[i].first - lambda*dp[i].second << "," << dp[i].second << " ";}
cout << endl;*/
return dp[n-1];
}
# | 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... |